Skip to content

Commit 8aa3853

Browse files
committed
Init imported name index locally
1 parent b5d8dbb commit 8aa3853

File tree

2 files changed

+36
-37
lines changed

2 files changed

+36
-37
lines changed

src/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/
1414
const getAliasName = (name, index) =>
1515
`i__value_${name.replace(/\W/g, '_')}_${index}`
1616

17-
let importIndex = 0
18-
1917
module.exports = postcss.plugin('postcss-modules-values', () => (
2018
css,
2119
result
2220
) => {
2321
const imports = extractICSSImports(css)
2422
const exports = extractICSSExports(css)
23+
let importIndex = 0
2524
const createImportedName = (path, name) => {
2625
const importedName = getAliasName(name, importIndex)
2726
if (imports[path] && imports[path][importedName]) {

test/test.js

+35-35
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ test('should import a simple constant and replace usages', () => {
9494
).resolves.toEqual(
9595
strip(`
9696
:import("./colors.css") {
97-
i__value_red_1: red;
97+
i__value_red_0: red;
9898
}
9999
:export {
100-
red: i__value_red_1;
100+
red: i__value_red_0;
101101
}
102-
.foo { color: i__value_red_1; }
102+
.foo { color: i__value_red_0; }
103103
`)
104104
)
105105
})
@@ -113,12 +113,12 @@ test('should import and alias a constant and replace usages', () => {
113113
).resolves.toEqual(
114114
strip(`
115115
:import("./colors.css") {
116-
i__value_red_2: blue;
116+
i__value_red_0: blue;
117117
}
118118
:export {
119-
red: i__value_red_2;
119+
red: i__value_red_0;
120120
}
121-
.foo { color: i__value_red_2; }
121+
.foo { color: i__value_red_0; }
122122
`)
123123
)
124124
})
@@ -133,15 +133,15 @@ test('should import multiple from a single file', () => {
133133
).resolves.toEqual(
134134
strip(`
135135
:import("./colors.css") {
136-
i__value_blue_3: blue;
137-
i__value_red_4: red;
136+
i__value_blue_0: blue;
137+
i__value_red_1: red;
138138
}
139139
:export {
140-
blue: i__value_blue_3;
141-
red: i__value_red_4;
140+
blue: i__value_blue_0;
141+
red: i__value_red_1;
142142
}
143-
.foo { color: i__value_red_4; }
144-
.bar { color: i__value_blue_3 }
143+
.foo { color: i__value_red_1; }
144+
.bar { color: i__value_blue_0 }
145145
`)
146146
)
147147
})
@@ -154,11 +154,11 @@ test('should import from a definition', () => {
154154
).resolves.toEqual(
155155
strip(`
156156
:import("./colors.css") {
157-
i__value_red_5: red
157+
i__value_red_0: red
158158
}
159159
:export {
160160
colors: "./colors.css";
161-
red: i__value_red_5
161+
red: i__value_red_0
162162
}
163163
`)
164164
)
@@ -172,10 +172,10 @@ test('should only allow values for paths if defined in the right order', () => {
172172
).resolves.toEqual(
173173
strip(`
174174
:import(colors) {
175-
i__value_red_6: red
175+
i__value_red_0: red
176176
}
177177
:export {
178-
red: i__value_red_6;
178+
red: i__value_red_0;
179179
colors: "./colors.css"
180180
}
181181
`)
@@ -226,14 +226,14 @@ test('should preserve import order', () => {
226226
).resolves.toEqual(
227227
strip(`
228228
:import("./a.css") {
229-
i__value_a_7: a
229+
i__value_a_0: a
230230
}
231231
:import("./b.css") {
232-
i__value_b_8: b
232+
i__value_b_1: b
233233
}
234234
:export {
235-
a: i__value_a_7;
236-
b: i__value_b_8
235+
a: i__value_a_0;
236+
b: i__value_b_1
237237
}
238238
`)
239239
)
@@ -247,12 +247,12 @@ test('should allow custom-property-style names', () => {
247247
).resolves.toEqual(
248248
strip(`
249249
:import("./colors.css") {
250-
i__value___red_9: --red;
250+
i__value___red_0: --red;
251251
}
252252
:export {
253-
--red: i__value___red_9;
253+
--red: i__value___red_0;
254254
}
255-
.foo { color: i__value___red_9; }
255+
.foo { color: i__value___red_0; }
256256
`)
257257
)
258258
})
@@ -306,15 +306,15 @@ test('should import multiple from a single file on multiple lines', () => {
306306
).resolves.toEqual(
307307
strip(`
308308
:import("./colors.css") {
309-
i__value_blue_10: blue;
310-
i__value_red_11: red;
309+
i__value_blue_0: blue;
310+
i__value_red_1: red;
311311
}
312312
:export {
313-
blue: i__value_blue_10;
314-
red: i__value_red_11;
313+
blue: i__value_blue_0;
314+
red: i__value_red_1;
315315
}
316-
.foo { color: i__value_red_11; }
317-
.bar { color: i__value_blue_10 }
316+
.foo { color: i__value_red_1; }
317+
.bar { color: i__value_blue_0 }
318318
`)
319319
)
320320
})
@@ -364,11 +364,11 @@ test('reuse existing :import with same name and :export', () => {
364364
strip(`
365365
:import('./colors.css') {
366366
i__some_import: blue;
367-
i__value_a_12: a
367+
i__value_a_0: a
368368
}
369369
:export {
370370
b: i__c;
371-
a: i__value_a_12
371+
a: i__value_a_0
372372
}
373373
`)
374374
)
@@ -378,18 +378,18 @@ test('prevent imported names collision', () => {
378378
return expect(
379379
runCSS(`
380380
:import(colors) {
381-
i__value_a_13: a;
381+
i__value_a_0: a;
382382
}
383383
@value a from colors;
384384
`)
385385
).resolves.toEqual(
386386
strip(`
387387
:import(colors) {
388-
i__value_a_13: a;
389-
i__value_a_14: a
388+
i__value_a_0: a;
389+
i__value_a_1: a
390390
}
391391
:export {
392-
a: i__value_a_14
392+
a: i__value_a_1
393393
}
394394
`)
395395
)

0 commit comments

Comments
 (0)