Skip to content

Commit

Permalink
resolve a file only once
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Aug 16, 2020
1 parent 0a8c00e commit bc94ff7
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 12 deletions.
21 changes: 17 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} 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
Expand Down
26 changes: 20 additions & 6 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,28 @@ exports[`extract nested: css code 1`] = `
color: red;
}
.bar {
.bar-module_bar {
color: red;
}
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);
"
`;

Expand All @@ -223,21 +230,28 @@ exports[`extract nested-delay-resolve: css code 1`] = `
color: red;
}
.bar {
.bar-module_bar {
color: red;
}
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);
"
`;

Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions test/fixtures/nested/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import component from './component.module.css'

export default component
3 changes: 3 additions & 0 deletions test/fixtures/nested/component.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.box {
color: blue;
}
5 changes: 3 additions & 2 deletions test/fixtures/nested/index.js
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions test/fixtures/nested/nested.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './nested.css'

export default 'test'
export { default as component } from './component'

0 comments on commit bc94ff7

Please sign in to comment.