Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate styles in CSS output #58

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' })]
}