Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v4' into v4
Browse files Browse the repository at this point in the history
  • Loading branch information
thgh committed Nov 3, 2022
2 parents 272443b + fc71b87 commit 0c8e903
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
"scripts": {
"build": "rollup -c",
"dev": "rollup -cw",
"test:circular": "cd test/circular && rm -rf output && rollup -c && cd ../..",
"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: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",
"test": "npm run test:simple && npm run test:nested && npm run test:empty && npm run test:circular",
"test:win": "npm run test:win:simple",
"lint": "prettier rollup.config.js src/**",
"prepare": "npm run build",
Expand Down
9 changes: 6 additions & 3 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ export default function css(options = {}) {
let fileName = options.fileName

// Get all CSS modules in the order that they were imported
const getCSSModules = (id, getModuleInfo, modules = new Set()) => {
if (modules.has(id)) {
const getCSSModules = (id, getModuleInfo, modules = new Set(), visitedModules = new Set()) => {
if (modules.has(id) || visitedModules.has(id)) {
return new Set()
}

if (filter(id)) modules.add(id)

// Prevent infinite recursion with circular dependencies
visitedModules.add(id);

// Recursively retrieve all of imported CSS modules
const info = getModuleInfo(id)
if (!info) return modules
Expand All @@ -23,7 +26,7 @@ export default function css(options = {}) {
modules = new Set(
[].concat(
Array.from(modules),
Array.from(getCSSModules(importId, getModuleInfo, modules))
Array.from(getCSSModules(importId, getModuleInfo, modules, visitedModules))
)
)
})
Expand Down
2 changes: 2 additions & 0 deletions test/circular/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import b from './b.js';
export default 42;
2 changes: 2 additions & 0 deletions test/circular/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import a from './a.js';
export default 42;
10 changes: 10 additions & 0 deletions test/circular/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import css from 'rollup-plugin-css-only'

export default {
input: 'a.js',
output: {
file: 'output/output.js',
format: 'esm'
},
plugins: [css({ output: 'output.css' })]
}

0 comments on commit 0c8e903

Please sign in to comment.