Skip to content

Commit b5d8dbb

Browse files
committed
Prevent imported names collision
1 parent b88d76b commit b5d8dbb

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ const getAliasName = (name, index) =>
1515
`i__value_${name.replace(/\W/g, '_')}_${index}`
1616

1717
let importIndex = 0
18-
const createImportedName = importName => getAliasName(importName, importIndex++)
1918

2019
module.exports = postcss.plugin('postcss-modules-values', () => (
2120
css,
2221
result
2322
) => {
2423
const imports = extractICSSImports(css)
2524
const exports = extractICSSExports(css)
25+
const createImportedName = (path, name) => {
26+
const importedName = getAliasName(name, importIndex)
27+
if (imports[path] && imports[path][importedName]) {
28+
importIndex += 1
29+
return createImportedName(path, name)
30+
}
31+
importIndex += 1
32+
return importedName
33+
}
2634

2735
const addDefinition = atRule => {
2836
let matches
@@ -47,7 +55,7 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
4755
let tokens = matchImport.exec(alias)
4856
if (tokens) {
4957
let [, theirName, myName = theirName] = tokens
50-
let importedName = createImportedName(myName)
58+
let importedName = createImportedName(path, myName)
5159
exports[myName] = importedName
5260
return { theirName, importedName }
5361
} else {

test/test.js

+21
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,24 @@ test('reuse existing :import with same name and :export', () => {
373373
`)
374374
)
375375
})
376+
377+
test('prevent imported names collision', () => {
378+
return expect(
379+
runCSS(`
380+
:import(colors) {
381+
i__value_a_13: a;
382+
}
383+
@value a from colors;
384+
`)
385+
).resolves.toEqual(
386+
strip(`
387+
:import(colors) {
388+
i__value_a_13: a;
389+
i__value_a_14: a
390+
}
391+
:export {
392+
a: i__value_a_14
393+
}
394+
`)
395+
)
396+
})

0 commit comments

Comments
 (0)