diff --git a/src/index.js b/src/index.js index 7612fca5..495fc920 100644 --- a/src/index.js +++ b/src/index.js @@ -15,11 +15,24 @@ function inferOption(option, defaultValue) { return option ? {} : defaultValue } -function getRecursiveImportOrder(id, getModuleInfo) { - const result = [] +/** + * Recursivly get the correct import order from rollup + * We only process a file once + * + * @param {string} id + * @param {Function} getModuleInfo + * @param {Set} seen + */ +function getRecursiveImportOrder(id, getModuleInfo, seen = new Set()) { + if (seen.has(id)) { + return [] + } + + seen.add(id) + + const result = [id] getModuleInfo(id).importedIds.forEach(importFile => { - result.push(importFile) - result.push(...getRecursiveImportOrder(importFile, getModuleInfo)) + result.push(...getRecursiveImportOrder(importFile, getModuleInfo, seen)) }) return result diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index 16b6604d..d6b4092a 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -200,7 +200,7 @@ exports[`extract nested: css code 1`] = ` color: red; } -.bar { +.bar-module_bar { color: red; } @@ -208,13 +208,20 @@ a { font-weight: bold; } -/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5jc3MiLCJiYXIuY3NzIiwibmVzdGVkLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFVBQVU7QUFDWjs7QUNGQTtFQUNFLFVBQVU7QUFDWjs7QUNGQTtFQUNFLGlCQUFpQjtBQUNuQiIsImZpbGUiOiJidW5kbGUuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiYm9keSB7XG4gIGNvbG9yOiByZWQ7XG59XG4iLCIuYmFyIHtcbiAgY29sb3I6IHJlZDtcbn1cbiIsImEge1xuICBmb250LXdlaWdodDogYm9sZDtcbn1cbiJdfQ==*/" +.component-module_box { + color: blue; +} +/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5jc3MiLCJiYXIubW9kdWxlLmNzcyIsIm5lc3RlZC5jc3MiLCJjb21wb25lbnQubW9kdWxlLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFVBQVU7QUFDWjs7QUNGQTtFQUNFLFVBQVU7QUFDWjs7QUNGQTtFQUNFLGlCQUFpQjtBQUNuQjs7QUNGQTtFQUNFLFdBQVc7QUFDYiIsImZpbGUiOiJidW5kbGUuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiYm9keSB7XG4gIGNvbG9yOiByZWQ7XG59XG4iLCIuYmFyIHtcbiAgY29sb3I6IHJlZDtcbn1cbiIsImEge1xuICBmb250LXdlaWdodDogYm9sZDtcbn1cbiIsIi5ib3gge1xuICBjb2xvcjogYmx1ZTtcbn0iXX0=*/" `; exports[`extract nested: js code 1`] = ` "'use strict'; -console.log(undefined); +var bar = {\\"bar\\":\\"bar-module_bar\\"}; + +var component = {\\"box\\":\\"component-module_box\\"}; + +console.log(bar, component); " `; @@ -223,7 +230,7 @@ exports[`extract nested-delay-resolve: css code 1`] = ` color: red; } -.bar { +.bar-module_bar { color: red; } @@ -231,13 +238,20 @@ a { font-weight: bold; } -/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImJhci5jc3MiLCJuZXN0ZWQuY3NzIiwiZm9vLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFVBQVU7QUFDWjs7QUNGQTtFQUNFLGlCQUFpQjtBQUNuQjs7QUNGQTtFQUNFLFVBQVU7QUFDWiIsImZpbGUiOiJidW5kbGUuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiLmJhciB7XG4gIGNvbG9yOiByZWQ7XG59XG4iLCJhIHtcbiAgZm9udC13ZWlnaHQ6IGJvbGQ7XG59XG4iLCJib2R5IHtcbiAgY29sb3I6IHJlZDtcbn1cbiJdfQ==*/" +.component-module_box { + color: blue; +} +/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5jc3MiLCJiYXIubW9kdWxlLmNzcyIsIm5lc3RlZC5jc3MiLCJjb21wb25lbnQubW9kdWxlLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLFVBQVU7QUFDWjs7QUNGQTtFQUNFLFVBQVU7QUFDWjs7QUNGQTtFQUNFLGlCQUFpQjtBQUNuQjs7QUNGQTtFQUNFLFdBQVc7QUFDYiIsImZpbGUiOiJidW5kbGUuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiYm9keSB7XG4gIGNvbG9yOiByZWQ7XG59XG4iLCIuYmFyIHtcbiAgY29sb3I6IHJlZDtcbn1cbiIsImEge1xuICBmb250LXdlaWdodDogYm9sZDtcbn1cbiIsIi5ib3gge1xuICBjb2xvcjogYmx1ZTtcbn0iXX0=*/" `; exports[`extract nested-delay-resolve: js code 1`] = ` "'use strict'; -console.log(undefined); +var bar = {\\"bar\\":\\"bar-module_bar\\"}; + +var component = {\\"box\\":\\"component-module_box\\"}; + +console.log(bar, component); " `; diff --git a/test/fixtures/nested/bar.css b/test/fixtures/nested/bar.module.css similarity index 100% rename from test/fixtures/nested/bar.css rename to test/fixtures/nested/bar.module.css diff --git a/test/fixtures/nested/component.js b/test/fixtures/nested/component.js new file mode 100644 index 00000000..77e5072f --- /dev/null +++ b/test/fixtures/nested/component.js @@ -0,0 +1,3 @@ +import component from './component.module.css' + +export default component diff --git a/test/fixtures/nested/component.module.css b/test/fixtures/nested/component.module.css new file mode 100644 index 00000000..4adcaf2f --- /dev/null +++ b/test/fixtures/nested/component.module.css @@ -0,0 +1,3 @@ +.box { + color: blue; +} \ No newline at end of file diff --git a/test/fixtures/nested/index.js b/test/fixtures/nested/index.js index 81aeeda8..944a2051 100644 --- a/test/fixtures/nested/index.js +++ b/test/fixtures/nested/index.js @@ -1,5 +1,6 @@ import './foo.css' -import bar from './bar.css' +import bar from './bar.module.css' import './nested' +import component from './component' -console.log(bar) +console.log(bar, component) diff --git a/test/fixtures/nested/nested.js b/test/fixtures/nested/nested.js index 1d8dc22f..6f50c34e 100644 --- a/test/fixtures/nested/nested.js +++ b/test/fixtures/nested/nested.js @@ -1,3 +1,4 @@ import './nested.css' export default 'test' +export { default as component } from './component'