From 60aa4a3ad2a718f6870a23892d594c54fa7043b2 Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 3 Feb 2021 10:31:33 +0100 Subject: [PATCH 01/16] feat: start with resolving css imports --- .gitignore | 1 + package.json | 3 ++- src/index.js | 32 ++++++++++++++++++++++++++++---- test/imports/css/a.css | 5 +++++ test/imports/css/b.css | 3 +++ test/imports/css/input.css | 5 +++++ test/imports/expected/bundle.css | 4 ++++ test/imports/expected/bundle.js | 1 + test/imports/input.js | 3 +++ test/imports/rollup.config.js | 12 ++++++++++++ 10 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 test/imports/css/a.css create mode 100644 test/imports/css/b.css create mode 100644 test/imports/css/input.css create mode 100644 test/imports/expected/bundle.css create mode 100644 test/imports/expected/bundle.js create mode 100644 test/imports/input.js create mode 100644 test/imports/rollup.config.js diff --git a/.gitignore b/.gitignore index 799453b..c860a7e 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ node_modules # Build files dist /test/**/output.* +/test/**/output/ \ No newline at end of file diff --git a/package.json b/package.json index d77a9b3..3d138b5 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "dev": "rollup -cw", "test:nested": "cd test/nested && rm -rf output && rollup -c && cmp output/bundle.js expected/bundle.js && cmp output/bundle.css expected/bundle.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": "npm run test:simple && npm run test:nested", + "test:imports": "cd test/imports && rm -rf output && rollup -c && cmp output/bundle.js expected/bundle.js && cmp output/bundle.css expected/bundle.css && cd ../..", + "test": "npm run test:simple && npm run test:nested && npm run test:imports", "lint": "prettier rollup.config.js src/**", "prepare": "npm run build", "prepublish": "npm run build" diff --git a/src/index.js b/src/index.js index 3180590..a866271 100644 --- a/src/index.js +++ b/src/index.js @@ -10,6 +10,18 @@ var arraysEqual = function(a, b) { return true } +function splitImports(code) { + const imports = []; + const codeWithoutImports = code.replace(/@import\s+(.*)\r?\n/gm, (_, group) => { + imports.push(group.replace(/(["'])~/, '$1')); + return ''; + }); + return { + imports, + codeWithoutImports + }; +} + export default function css(options = {}) { const filter = createFilter(options.include || ['**/*.css'], options.exclude) const styles = {} @@ -43,10 +55,21 @@ export default function css(options = {}) { return } + const { imports, codeWithoutImports } = splitImports(code); + // When output is disabled, the stylesheet is exported as a string if (options.output === false) { + if (imports.length === 0) { + return { + code: `export default ['${JSON.stringify(code)}'`, + map: { mappings: '' } + } + } + const importNamed = imports.map((d, i) => `import i${i} from ${d}`).join('\n'); return { - code: 'export default ' + JSON.stringify(code), + code: ` + ${importNamed} + export default ${imports.map((_, i) => `i${i}`).join(' + ')} + '${JSON.stringify(codeWithoutImports)}'`, map: { mappings: '' } } } @@ -54,12 +77,13 @@ export default function css(options = {}) { // Keep track of every stylesheet // Check if it changed since last render // NOTE: If we are in transform block, we can assume styles[id] !== code, right? - if (styles[id] !== code && (styles[id] || code)) { - styles[id] = code + if (styles[id] !== codeWithoutImports && (styles[id] || codeWithoutImports)) { + styles[id] = codeWithoutImports hasChanged = true } - return '' + // return a list of imports + return imports.map((d) => `import ${d}`).join('\n'); }, generateBundle(opts, bundle) { const ids = [] diff --git a/test/imports/css/a.css b/test/imports/css/a.css new file mode 100644 index 0000000..4f28f86 --- /dev/null +++ b/test/imports/css/a.css @@ -0,0 +1,5 @@ +@import './b.css'; + +.second { + color: green; +} diff --git a/test/imports/css/b.css b/test/imports/css/b.css new file mode 100644 index 0000000..33185d6 --- /dev/null +++ b/test/imports/css/b.css @@ -0,0 +1,3 @@ +.first { + color: blue; +} diff --git a/test/imports/css/input.css b/test/imports/css/input.css new file mode 100644 index 0000000..8444700 --- /dev/null +++ b/test/imports/css/input.css @@ -0,0 +1,5 @@ +@import './a.css'; + +.last { + color: green; +} diff --git a/test/imports/expected/bundle.css b/test/imports/expected/bundle.css new file mode 100644 index 0000000..0ce1a39 --- /dev/null +++ b/test/imports/expected/bundle.css @@ -0,0 +1,4 @@ +.rollup { + color: green; + user-select: none; +} diff --git a/test/imports/expected/bundle.js b/test/imports/expected/bundle.js new file mode 100644 index 0000000..dddc8cf --- /dev/null +++ b/test/imports/expected/bundle.js @@ -0,0 +1 @@ +console.log('css imported'); diff --git a/test/imports/input.js b/test/imports/input.js new file mode 100644 index 0000000..459af57 --- /dev/null +++ b/test/imports/input.js @@ -0,0 +1,3 @@ +import './css/input.css' + +console.log('css imported') diff --git a/test/imports/rollup.config.js b/test/imports/rollup.config.js new file mode 100644 index 0000000..0010cc2 --- /dev/null +++ b/test/imports/rollup.config.js @@ -0,0 +1,12 @@ +import css from '../../src/index.js' + +export default { + input: 'input.js', + output: { + file: 'output/bundle.js', + format: 'esm' + }, + plugins: [ + css({ output: 'bundle.css' }) + ] +} \ No newline at end of file From e5393e4b41fe6e0e7bab08f109536b339e16b8b5 Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 3 Feb 2021 10:59:58 +0100 Subject: [PATCH 02/16] fix: module tree traversal --- src/index.js | 55 +++++++++++++++++--------- test/imports/css/{b.css => first.css} | 0 test/imports/css/input.css | 2 +- test/imports/css/{a.css => second.css} | 2 +- test/imports/expected/bundle.css | 12 +++++- 5 files changed, 49 insertions(+), 22 deletions(-) rename test/imports/css/{b.css => first.css} (100%) rename test/imports/css/{a.css => second.css} (56%) diff --git a/src/index.js b/src/index.js index a866271..9594474 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,7 @@ var arraysEqual = function(a, b) { function splitImports(code) { const imports = []; - const codeWithoutImports = code.replace(/@import\s+(.*)\r?\n/gm, (_, group) => { + const codeWithoutImports = code.replace(/@import\s+(.*);(\r\n)+/gm, (_, group) => { imports.push(group.replace(/(["'])~/, '$1')); return ''; }); @@ -22,6 +22,41 @@ function splitImports(code) { }; } +// Get all CSS modules in the order that they were imported +function getCSSModules(id, getModuleInfo) { + const modules = []; + const visited = new Set(); + + // traversal logic + // 1. mark node as visited + // 2. add to list at the end + // 3. go down with imports but in reverse order + // 4. reverse full list + // example + // root + // 1 + // 11 + // 12 + // 2 + // 21 + // 22 + // will result in the list: root, 2, 22, 21, 1, 12, 11 + // revered: 11, 12, 1, 21, 22, 2, root + const visitModule = (id) => { + if (visited.has(id)) { + return; + } + visited.add(id); + if (filter(id)) { + modules.push(id); + } + const reverseChildren = getModuleInfo(id).importedIds.slice().reverse(); + reverseChildren.forEach(visitModule); + } + visitModule(id); + return modules.reverse(); +}; + export default function css(options = {}) { const filter = createFilter(options.include || ['**/*.css'], options.exclude) const styles = {} @@ -29,22 +64,6 @@ export default function css(options = {}) { let hasChanged = false let prevIds = [] - // Get all CSS modules in the order that they were imported - const getCSSModules = (id, getModuleInfo, modules = new Set()) => { - if (modules.has(id)) { - return new Set() - } - - if (filter(id)) modules.add(id) - - // Recursively retrieve all of imported CSS modules - getModuleInfo(id).importedIds.forEach(importId => { - modules = new Set([].concat(Array.from(modules), Array.from(getCSSModules(importId, getModuleInfo, modules)))) - }); - - return modules - }; - return { name: 'css', buildStart() { @@ -92,7 +111,7 @@ export default function css(options = {}) { for (const file in bundle) { const root = bundle[file].facadeModuleId const modules = getCSSModules(root, this.getModuleInfo) - ids.push(...Array.from(modules)) + ids.push(...modules) } // If the files are imported in the same order and there are no changes diff --git a/test/imports/css/b.css b/test/imports/css/first.css similarity index 100% rename from test/imports/css/b.css rename to test/imports/css/first.css diff --git a/test/imports/css/input.css b/test/imports/css/input.css index 8444700..1d21b15 100644 --- a/test/imports/css/input.css +++ b/test/imports/css/input.css @@ -1,4 +1,4 @@ -@import './a.css'; +@import './second.css'; .last { color: green; diff --git a/test/imports/css/a.css b/test/imports/css/second.css similarity index 56% rename from test/imports/css/a.css rename to test/imports/css/second.css index 4f28f86..5cfbf93 100644 --- a/test/imports/css/a.css +++ b/test/imports/css/second.css @@ -1,4 +1,4 @@ -@import './b.css'; +@import './fist.css'; .second { color: green; diff --git a/test/imports/expected/bundle.css b/test/imports/expected/bundle.css index 0ce1a39..7af10cd 100644 --- a/test/imports/expected/bundle.css +++ b/test/imports/expected/bundle.css @@ -1,4 +1,12 @@ -.rollup { +.first { + color: blue; +} + +.second { + color: green; +} + +.last { color: green; - user-select: none; } + From f7ddff3f1c19b6a7aad63c251d6adaf3496540ff Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 3 Feb 2021 11:11:17 +0100 Subject: [PATCH 03/16] fix: missing function --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 9594474..8f133bf 100644 --- a/src/index.js +++ b/src/index.js @@ -23,7 +23,7 @@ function splitImports(code) { } // Get all CSS modules in the order that they were imported -function getCSSModules(id, getModuleInfo) { +function getCSSModules(id, filter, getModuleInfo) { const modules = []; const visited = new Set(); @@ -110,7 +110,7 @@ export default function css(options = {}) { // Determine import order of files for (const file in bundle) { const root = bundle[file].facadeModuleId - const modules = getCSSModules(root, this.getModuleInfo) + const modules = getCSSModules(root, filter, this.getModuleInfo) ids.push(...modules) } From e204150f19ae58a39a786aa9af5a0492096a6fa1 Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 3 Feb 2021 11:12:34 +0100 Subject: [PATCH 04/16] refactor: move code --- src/index.js | 109 ++++++++++++++++++++++++--------------------------- 1 file changed, 52 insertions(+), 57 deletions(-) diff --git a/src/index.js b/src/index.js index 8f133bf..9a8da2a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,62 +1,5 @@ import { createFilter } from '@rollup/pluginutils' -var arraysEqual = function(a, b) { - if (a.length !== b.length) return false - - for (let i = a.length; i--;) { - if (a[i] !== b[i]) return false - } - - return true -} - -function splitImports(code) { - const imports = []; - const codeWithoutImports = code.replace(/@import\s+(.*);(\r\n)+/gm, (_, group) => { - imports.push(group.replace(/(["'])~/, '$1')); - return ''; - }); - return { - imports, - codeWithoutImports - }; -} - -// Get all CSS modules in the order that they were imported -function getCSSModules(id, filter, getModuleInfo) { - const modules = []; - const visited = new Set(); - - // traversal logic - // 1. mark node as visited - // 2. add to list at the end - // 3. go down with imports but in reverse order - // 4. reverse full list - // example - // root - // 1 - // 11 - // 12 - // 2 - // 21 - // 22 - // will result in the list: root, 2, 22, 21, 1, 12, 11 - // revered: 11, 12, 1, 21, 22, 2, root - const visitModule = (id) => { - if (visited.has(id)) { - return; - } - visited.add(id); - if (filter(id)) { - modules.push(id); - } - const reverseChildren = getModuleInfo(id).importedIds.slice().reverse(); - reverseChildren.forEach(visitModule); - } - visitModule(id); - return modules.reverse(); -}; - export default function css(options = {}) { const filter = createFilter(options.include || ['**/*.css'], options.exclude) const styles = {} @@ -158,3 +101,55 @@ export default function css(options = {}) { } } } + +function arraysEqual(a, b) { + if (a.length !== b.length) return false + return a.every((ai, i) => ai === b[i]); +} + +function splitImports(code) { + const imports = []; + const codeWithoutImports = code.replace(/@import\s+(.*);(\r\n)+/gm, (_, group) => { + imports.push(group.replace(/(["'])~/, '$1')); + return ''; + }); + return { + imports, + codeWithoutImports + }; +} + +// Get all CSS modules in the order that they were imported +function getCSSModules(id, filter, getModuleInfo) { + const modules = []; + const visited = new Set(); + + // traversal logic + // 1. mark node as visited + // 2. add to list at the end + // 3. go down with imports but in reverse order + // 4. reverse full list + // example + // root + // 1 + // 11 + // 12 + // 2 + // 21 + // 22 + // will result in the list: root, 2, 22, 21, 1, 12, 11 + // revered: 11, 12, 1, 21, 22, 2, root + const visitModule = (id) => { + if (visited.has(id)) { + return; + } + visited.add(id); + if (filter(id)) { + modules.push(id); + } + const reverseChildren = getModuleInfo(id).importedIds.slice().reverse(); + reverseChildren.forEach(visitModule); + } + visitModule(id); + return modules.reverse(); +}; From ed96302ea7a9effac1aee0a006e432ef7301e37f Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 3 Feb 2021 11:16:50 +0100 Subject: [PATCH 05/16] fix: typo --- test/imports/css/second.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/imports/css/second.css b/test/imports/css/second.css index 5cfbf93..8a4679f 100644 --- a/test/imports/css/second.css +++ b/test/imports/css/second.css @@ -1,4 +1,4 @@ -@import './fist.css'; +@import './first.css'; .second { color: green; From 31293297160812301a38aa3292659bbfbfd6695b Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 3 Feb 2021 11:25:44 +0100 Subject: [PATCH 06/16] fix: newline escaping --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 9a8da2a..d11553a 100644 --- a/src/index.js +++ b/src/index.js @@ -109,7 +109,7 @@ function arraysEqual(a, b) { function splitImports(code) { const imports = []; - const codeWithoutImports = code.replace(/@import\s+(.*);(\r\n)+/gm, (_, group) => { + const codeWithoutImports = code.replace(/@import\s+(.*);[\r\n]*/gm, (_, group) => { imports.push(group.replace(/(["'])~/, '$1')); return ''; }); From a8acb2a5715bf71005a0bdd99abac22b12a4619f Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 3 Feb 2021 16:45:14 +0100 Subject: [PATCH 07/16] Update src/index.js Co-authored-by: Thomas Ghysels --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index d11553a..76890c7 100644 --- a/src/index.js +++ b/src/index.js @@ -23,7 +23,7 @@ export default function css(options = {}) { if (options.output === false) { if (imports.length === 0) { return { - code: `export default ['${JSON.stringify(code)}'`, + code: `export default ${JSON.stringify(code)}`, map: { mappings: '' } } } From f4a0ed7e926acd8d7de1636d5c32fb0bf4aa48fe Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 3 Feb 2021 16:50:41 +0100 Subject: [PATCH 08/16] fix: integrate feedback --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 76890c7..940054a 100644 --- a/src/index.js +++ b/src/index.js @@ -31,7 +31,7 @@ export default function css(options = {}) { return { code: ` ${importNamed} - export default ${imports.map((_, i) => `i${i}`).join(' + ')} + '${JSON.stringify(codeWithoutImports)}'`, + export default ${imports.map((_, i) => `i${i}`).join(' + ')} + ${JSON.stringify(codeWithoutImports)}`, map: { mappings: '' } } } From feab91f4763319efdeb6edaca6dea5f5b02004db Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 1 Jun 2022 21:59:25 -0400 Subject: [PATCH 09/16] build: migrate to scoped package --- package.json | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 3d138b5..8cf773e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,9 @@ { - "name": "rollup-plugin-css-only", - "version": "3.1.0", + "name": "@sgratzl/rollup-plugin-css-only", + "publishConfig": { + "access": "public" + }, + "version": "3.2.0", "description": "Rollup plugin that bundles imported css", "main": "dist/index.cjs.js", "module": "dist/index.es.js", @@ -22,13 +25,16 @@ ], "license": "MIT", "author": "Thomas Ghysels ", - "homepage": "https://github.com/thgh/rollup-plugin-css-only", + "contributors": [ + "Samuel Gratzl " + ], + "homepage": "https://github.com/sgratzl/rollup-plugin-css-only", "bugs": { - "url": "https://github.com/thgh/rollup-plugin-css-only/issues" + "url": "https://github.com/sgratzl/rollup-plugin-css-only/issues" }, "repository": { "type": "git", - "url": "https://github.com/thgh/rollup-plugin-css-only" + "url": "https://github.com/sgratzl/rollup-plugin-css-only" }, "files": [ "dist" From 367aabff387c7720919d13e6bc8a024ed7b84a40 Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 18 Jan 2023 21:53:27 -0500 Subject: [PATCH 10/16] fix: setup readd tests --- package.json | 3 +- src/index.mjs | 49 ++----------------- .../{rollup.config.js => rollup.config.mjs} | 2 +- 3 files changed, 8 insertions(+), 46 deletions(-) rename test/imports/{rollup.config.js => rollup.config.mjs} (80%) diff --git a/package.json b/package.json index 292add7..cb46c9c 100644 --- a/package.json +++ b/package.json @@ -21,8 +21,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:imports": "cd test/imports && rm -rf output && rollup -c && cmp output/bundle.js expected/bundle.js && cmp output/bundle.css expected/bundle.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:imports", "test:win": "npm run test:win:simple", "lint": "prettier rollup.config.js src/**", "prepare": "npm run build", diff --git a/src/index.mjs b/src/index.mjs index ce9791e..e239a68 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -63,8 +63,8 @@ export default function css(options = {}) { // Keep track of every stylesheet // Check if it changed since last render // NOTE: If we are in transform block, we can assume styles[id] !== code, right? - if (styles[id] !== code && (styles[id] || code)) { - styles[id] = code + if (styles[id] !== codeWithoutImports && (styles[id] || codeWithoutImports)) { + styles[id] = codeWithoutImports } // return a list of imports @@ -76,8 +76,8 @@ export default function css(options = {}) { // Determine import order of files for (const file in bundle) { const root = bundle[file].facadeModuleId - const modules = getCSSModules(root, filter, this.getModuleInfo) - ids.push(...modules) + const modules = getCSSModules(root, this.getModuleInfo) + ids.push(...Array.from(modules)) } // Combine all stylesheets, respecting import order @@ -99,10 +99,6 @@ export default function css(options = {}) { } } -function arraysEqual(a, b) { - if (a.length !== b.length) return false - return a.every((ai, i) => ai === b[i]); -} function splitImports(code) { const imports = []; @@ -114,39 +110,4 @@ function splitImports(code) { imports, codeWithoutImports }; -} - -// Get all CSS modules in the order that they were imported -function getCSSModules(id, filter, getModuleInfo) { - const modules = []; - const visited = new Set(); - - // traversal logic - // 1. mark node as visited - // 2. add to list at the end - // 3. go down with imports but in reverse order - // 4. reverse full list - // example - // root - // 1 - // 11 - // 12 - // 2 - // 21 - // 22 - // will result in the list: root, 2, 22, 21, 1, 12, 11 - // revered: 11, 12, 1, 21, 22, 2, root - const visitModule = (id) => { - if (visited.has(id)) { - return; - } - visited.add(id); - if (filter(id)) { - modules.push(id); - } - const reverseChildren = getModuleInfo(id).importedIds.slice().reverse(); - reverseChildren.forEach(visitModule); - } - visitModule(id); - return modules.reverse(); -}; +} \ No newline at end of file diff --git a/test/imports/rollup.config.js b/test/imports/rollup.config.mjs similarity index 80% rename from test/imports/rollup.config.js rename to test/imports/rollup.config.mjs index 0010cc2..c753926 100644 --- a/test/imports/rollup.config.js +++ b/test/imports/rollup.config.mjs @@ -1,4 +1,4 @@ -import css from '../../src/index.js' +import css from '../../src/index.mjs' export default { input: 'input.js', From 0326729a3a5750fa171b0cf074658682c220446e Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 18 Jan 2023 21:54:03 -0500 Subject: [PATCH 11/16] ci: simplify --- .github/workflows/test.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 9db515d..aaa6fc5 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,9 +1,6 @@ name: Node.js CI on: push: - branches: [v3, v4] - pull_request: - branches: [v3, v4] jobs: build: runs-on: ubuntu-latest From 4c9ed849a9d783a72e51f69076a478da7fc3a640 Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 18 Jan 2023 21:55:17 -0500 Subject: [PATCH 12/16] fix: test import --- .github/workflows/test.yaml | 2 +- test/circular/rollup.config.mjs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index aaa6fc5..77e2dc1 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -6,7 +6,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node-version: [14.x, 16.x, 18.x, 19.x] + node-version: [16.x, 18.x, 19.x] steps: - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} diff --git a/test/circular/rollup.config.mjs b/test/circular/rollup.config.mjs index 5e80f51..7fd218e 100644 --- a/test/circular/rollup.config.mjs +++ b/test/circular/rollup.config.mjs @@ -1,4 +1,4 @@ -import css from 'rollup-plugin-css-only' +import css from '../../src/index.mjs' export default { input: 'a.js', From 351e1ebead1e37fdd663ca0c4a8d7e73859a806a Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 18 Jan 2023 21:59:04 -0500 Subject: [PATCH 13/16] fix: hopefully fix encoding issues --- package.json | 2 +- test/imports/{expected/bundle.css => expected.css} | 3 +-- test/imports/{expected/bundle.js => expected.js} | 0 test/imports/rollup.config.mjs | 4 ++-- 4 files changed, 4 insertions(+), 5 deletions(-) rename test/imports/{expected/bundle.css => expected.css} (96%) rename test/imports/{expected/bundle.js => expected.js} (100%) diff --git a/package.json b/package.json index cb46c9c..aa11428 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "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:imports": "cd test/imports && rm -rf output && rollup -c && cmp output/bundle.js expected/bundle.js && cmp output/bundle.css expected/bundle.css && cd ../..", + "test:imports": "cd test/imports && rm -rf output && rollup -c && cmp output/bundle.js expected.js && cmp output/bundle.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 && npm run test:imports", "test:win": "npm run test:win:simple", diff --git a/test/imports/expected/bundle.css b/test/imports/expected.css similarity index 96% rename from test/imports/expected/bundle.css rename to test/imports/expected.css index 7af10cd..5e388f2 100644 --- a/test/imports/expected/bundle.css +++ b/test/imports/expected.css @@ -8,5 +8,4 @@ .last { color: green; -} - +} \ No newline at end of file diff --git a/test/imports/expected/bundle.js b/test/imports/expected.js similarity index 100% rename from test/imports/expected/bundle.js rename to test/imports/expected.js diff --git a/test/imports/rollup.config.mjs b/test/imports/rollup.config.mjs index c753926..2b1ef9e 100644 --- a/test/imports/rollup.config.mjs +++ b/test/imports/rollup.config.mjs @@ -3,10 +3,10 @@ import css from '../../src/index.mjs' export default { input: 'input.js', output: { - file: 'output/bundle.js', + file: 'output/output.js', format: 'esm' }, plugins: [ - css({ output: 'bundle.css' }) + css({ output: 'output.css' }) ] } \ No newline at end of file From 8aac60451053ccc1474ac50664fe4d0333ed4495 Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 18 Jan 2023 22:00:02 -0500 Subject: [PATCH 14/16] test: fix compare setup --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index aa11428..16cd5a0 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "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:imports": "cd test/imports && rm -rf output && rollup -c && cmp output/bundle.js expected.js && cmp output/bundle.css expected.css && cd ../..", + "test:imports": "cd test/imports && 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 && npm run test:circular && npm run test:imports", "test:win": "npm run test:win:simple", From 74e7288304a8ecdfaef247561adebfa2f0249c69 Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 18 Jan 2023 22:03:20 -0500 Subject: [PATCH 15/16] fix: wrong output import order --- src/index.mjs | 54 +++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index e239a68..ca36bf5 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -8,30 +8,38 @@ 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(), visitedModules = new Set()) => { - if (modules.has(id) || visitedModules.has(id)) { - return new Set() + const getCSSModules = (id, getModuleInfo) => { + const modules = []; + const visited = new Set(); + + // traversal logic + // 1. mark node as visited + // 2. add to list at the end + // 3. go down with imports but in reverse order + // 4. reverse full list + // example + // root + // 1 + // 11 + // 12 + // 2 + // 21 + // 22 + // will result in the list: root, 2, 22, 21, 1, 12, 11 + // revered: 11, 12, 1, 21, 22, 2, root + const visitModule = (id) => { + if (visited.has(id)) { + return; + } + visited.add(id); + if (filter(id)) { + modules.push(id); + } + const reverseChildren = getModuleInfo(id).importedIds.slice().reverse(); + reverseChildren.forEach(visitModule); } - - 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 - - info.importedIds.forEach(importId => { - modules = new Set( - [].concat( - Array.from(modules), - Array.from(getCSSModules(importId, getModuleInfo, modules, visitedModules)) - ) - ) - }) - - return modules + visitModule(id); + return modules.reverse(); } return { From 0db8daf62b9e10c0e23f2739e56efc6d8f010bb2 Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Wed, 18 Jan 2023 22:05:08 -0500 Subject: [PATCH 16/16] test: fix missing newline --- test/imports/expected.css | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/imports/expected.css b/test/imports/expected.css index 5e388f2..7af10cd 100644 --- a/test/imports/expected.css +++ b/test/imports/expected.css @@ -8,4 +8,5 @@ .last { color: green; -} \ No newline at end of file +} +