Skip to content

Commit edf6545

Browse files
authored
Merge pull request #58 from thgh/dedupe
Fix duplicate styles in CSS output
2 parents c9fbea2 + af5649e commit edf6545

File tree

10 files changed

+32
-4
lines changed

10 files changed

+32
-4
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
"test:nested": "cd test/nested && rm -rf output && rollup -c && cmp output/output.js expected.js && cmp output/output.css expected.css && cd ../..",
1919
"test:empty": "cd test/empty && rm -rf output && rollup -c && cmp output/output.js expected.js && cmp output/output.css expected.css && cd ../..",
2020
"test:simple": "cd test/simple && rm -rf output && rollup -c && cmp output/output.js expected.js && cmp output/output.css expected.css && cd ../..",
21+
"test:unique": "cd test/unique && rm -rf output && rollup -c && cmp output/main.js expected.js && cmp output/output.css expected.css && cd ../..",
2122
"test:win:simple": "cd .\\test\\simple && del -f output.* && rollup -c && cd .. && ECHO n|comp simple\\output.js expected.js && ECHO n|comp simple\\output.css simple\\expected.css && cd ..",
22-
"test": "npm run test:simple && npm run test:nested && npm run test:empty && npm run test:circular",
23+
"test": "npm run test:simple && npm run test:nested && npm run test:empty && npm run test:circular && npm run test:unique",
2324
"test:win": "npm run test:win:simple",
2425
"lint": "prettier rollup.config.js src/**",
2526
"prepare": "npm run build",

src/index.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ export default function css(options = {}) {
5959
return ''
6060
},
6161
generateBundle(opts, bundle) {
62-
const ids = []
62+
const ids = new Set()
6363

6464
// Determine import order of files
6565
for (const file in bundle) {
6666
const root = bundle[file].facadeModuleId
6767
const modules = getCSSModules(root, this.getModuleInfo)
68-
ids.push(...Array.from(modules))
68+
modules.forEach(id => ids.add(id))
6969
}
7070

7171
// Combine all stylesheets, respecting import order
72-
const css = ids.map(id => styles[id]).join('\n')
72+
const css = Array.from(ids).map(id => styles[id]).join('\n')
7373

7474
// Emit styles through callback
7575
if (typeof options.output === 'function') {

test/unique/dependency-a.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './input.css'
2+
3+
console.log('dependency-a');

test/unique/expected.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.rollup {
2+
color: green;
3+
user-select: none;
4+
}

test/unique/expected.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import('./dependency-a.js');

test/unique/input.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.rollup {
2+
color: green;
3+
user-select: none;
4+
}

test/unique/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import './input.css'
2+
import('./dependency-a.js')

test/unique/output/dependency-a.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('dependency-a');

test/unique/output/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import('./dependency-a.js');

test/unique/rollup.config.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import css from '../../src/index.mjs'
2+
3+
export default {
4+
input: 'main.js',
5+
output: {
6+
chunkFileNames: '[name].js',
7+
dir: 'output',
8+
format: 'esm'
9+
},
10+
plugins: [css({ output: 'output.css' })]
11+
}

0 commit comments

Comments
 (0)