Skip to content

Commit

Permalink
Merge pull request #58 from thgh/dedupe
Browse files Browse the repository at this point in the history
Fix duplicate styles in CSS output
  • Loading branch information
thgh committed Nov 3, 2023
2 parents c9fbea2 + af5649e commit edf6545
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
"test:nested": "cd test/nested && rm -rf output && rollup -c && cmp output/output.js expected.js && cmp output/output.css expected.css && cd ../..",
"test:empty": "cd test/empty && rm -rf output && rollup -c && cmp output/output.js expected.js && cmp output/output.css expected.css && cd ../..",
"test:simple": "cd test/simple && rm -rf output && rollup -c && cmp output/output.js expected.js && cmp output/output.css expected.css && cd ../..",
"test:unique": "cd test/unique && rm -rf output && rollup -c && cmp output/main.js expected.js && cmp output/output.css expected.css && cd ../..",
"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 ..",
"test": "npm run test:simple && npm run test:nested && npm run test:empty && npm run test:circular",
"test": "npm run test:simple && npm run test:nested && npm run test:empty && npm run test:circular && npm run test:unique",
"test:win": "npm run test:win:simple",
"lint": "prettier rollup.config.js src/**",
"prepare": "npm run build",
Expand Down
6 changes: 3 additions & 3 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ export default function css(options = {}) {
return ''
},
generateBundle(opts, bundle) {
const ids = []
const ids = new Set()

// Determine import order of files
for (const file in bundle) {
const root = bundle[file].facadeModuleId
const modules = getCSSModules(root, this.getModuleInfo)
ids.push(...Array.from(modules))
modules.forEach(id => ids.add(id))
}

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

// Emit styles through callback
if (typeof options.output === 'function') {
Expand Down
3 changes: 3 additions & 0 deletions test/unique/dependency-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './input.css'

console.log('dependency-a');
4 changes: 4 additions & 0 deletions test/unique/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.rollup {
color: green;
user-select: none;
}
1 change: 1 addition & 0 deletions test/unique/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import('./dependency-a.js');
4 changes: 4 additions & 0 deletions test/unique/input.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.rollup {
color: green;
user-select: none;
}
2 changes: 2 additions & 0 deletions test/unique/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './input.css'
import('./dependency-a.js')
1 change: 1 addition & 0 deletions test/unique/output/dependency-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('dependency-a');
1 change: 1 addition & 0 deletions test/unique/output/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import('./dependency-a.js');
11 changes: 11 additions & 0 deletions test/unique/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import css from '../../src/index.mjs'

export default {
input: 'main.js',
output: {
chunkFileNames: '[name].js',
dir: 'output',
format: 'esm'
},
plugins: [css({ output: 'output.css' })]
}

0 comments on commit edf6545

Please sign in to comment.