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: order multiple inputs correctly #462

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 29 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,28 @@ function inferOption(option, defaultValue) {
* Recursively get the correct import order from rollup
* We only process a file once
*
* @param {string} id
* @param {Array<string>} ids
* @param {Function} getModuleInfo
* @param {Set<string>} seen
*/
function getRecursiveImportOrder(id, getModuleInfo, seen = new Set()) {
function getRecursiveImportOrder(ids, getModuleInfo, seen = new Set()) {
return ids.flatMap((id) => {
return getRecursiveImportOrderForModule(id, getModuleInfo, seen)
})
}

function getRecursiveImportOrderForModule(id, getModuleInfo, seen = new Set()) {
if (seen.has(id)) {
return []
}

seen.add(id)

const result = [id]
getModuleInfo(id).importedIds.forEach(importFile => {
result.push(...getRecursiveImportOrder(importFile, getModuleInfo, seen))
getModuleInfo(id).importedIds.forEach((importFile) => {
result.push(
...getRecursiveImportOrderForModule(importFile, getModuleInfo, seen)
)
})

return result
Expand Down Expand Up @@ -163,6 +171,11 @@ export default (options = {}) => {
options_.dir,
Object.keys(bundle).find(fileName => bundle[fileName].isEntry)
)
const entryFiles = options_.file
? [options_.file]
: Object.keys(bundle)
.filter((fileName) => bundle[fileName].isEntry)
.map((fileName) => path.join(options_.dir, fileName))
const getExtracted = () => {
let fileName = `${path.basename(file, path.extname(file))}.css`
if (typeof postcssLoaderOptions.extract === 'string') {
Expand All @@ -171,13 +184,19 @@ export default (options = {}) => {

const concat = new Concat(true, fileName, '\n')
const entries = [...extracted.values()]
const { modules, facadeModuleId } = bundle[
normalizePath(path.relative(dir, file))
]

if (modules) {
const facadeModuleIds = entryFiles
.map((entryFile) => {
const { modules, facadeModuleId } = bundle[
normalizePath(path.relative(dir, entryFile))
]
if (modules) return facadeModuleId
return false
})
.filter((n) => n)

if (facadeModuleIds.length > 0) {
const moduleIds = getRecursiveImportOrder(
facadeModuleId,
facadeModuleIds,
this.getModuleInfo
)
entries.sort(
Expand Down
46 changes: 46 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,52 @@ console.log(foo, bar);
"
`;

exports[`extract multiple-inputs-resolve: css code 1`] = `
".should-be-first {
color: red;
}

.from-index {
color: red;
}

.from-other {
color: blue;
}

/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZpcnN0QW5kU2hhcmVkLmNzcyIsImJ1bmRsZS5jc3MiLCJvdGhlci5jc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7RUFDRSxVQUFVO0FBQ1o7O0FDRkE7RUFDRSxVQUFVO0FBQ1o7O0FDRkE7RUFDRSxXQUFXO0FBQ2IiLCJmaWxlIjoiaW5kZXguY3NzIiwic291cmNlc0NvbnRlbnQiOlsiLnNob3VsZC1iZS1maXJzdCB7XG4gIGNvbG9yOiByZWQ7XG59XG4iLCIuZnJvbS1pbmRleCB7XG4gIGNvbG9yOiByZWQ7XG59XG4iLCIuZnJvbS1vdGhlciB7XG4gIGNvbG9yOiBibHVlO1xufVxuIl19*/"
`;

exports[`extract multiple-inputs-resolve: js code 1`] = `
"'use strict';

console.log('here to prevent warnings');
"
`;

exports[`extract multiple-inputs-resolve-reverse: css code 1`] = `
".should-be-first {
color: red;
}

.from-other {
color: blue;
}

.from-index {
color: red;
}

/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZpcnN0QW5kU2hhcmVkLmNzcyIsIm90aGVyLmNzcyIsImJ1bmRsZS5jc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7RUFDRSxVQUFVO0FBQ1o7O0FDRkE7RUFDRSxXQUFXO0FBQ2I7O0FDRkE7RUFDRSxVQUFVO0FBQ1oiLCJmaWxlIjoiaW5kZXguY3NzIiwic291cmNlc0NvbnRlbnQiOlsiLnNob3VsZC1iZS1maXJzdCB7XG4gIGNvbG9yOiByZWQ7XG59XG4iLCIuZnJvbS1vdGhlciB7XG4gIGNvbG9yOiBibHVlO1xufVxuIiwiLmZyb20taW5kZXgge1xuICBjb2xvcjogcmVkO1xufVxuIl19*/"
`;

exports[`extract multiple-inputs-resolve-reverse: js code 1`] = `
"'use strict';

console.log('here to prevent warnings');
"
`;

exports[`extract nested: css code 1`] = `
"body {
color: red;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/multiple-inputs/bundle.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.from-index {
color: red;
}
4 changes: 4 additions & 0 deletions test/fixtures/multiple-inputs/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import './firstAndShared.css'
import './bundle.css'

console.log('here to prevent warnings')
3 changes: 3 additions & 0 deletions test/fixtures/multiple-inputs/firstAndShared.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.should-be-first {
color: red;
}
3 changes: 3 additions & 0 deletions test/fixtures/multiple-inputs/other.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.from-other {
color: blue;
}
4 changes: 4 additions & 0 deletions test/fixtures/multiple-inputs/other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import './firstAndShared.css'
import './other.css'

console.log('here to prevent warnings')
32 changes: 27 additions & 5 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,20 @@ async function write({
}

outDir = fixture('dist', outDir)
const multipleInputs = Array.isArray(input)
const formattedInput = multipleInputs
? input.map((i) => fixture(i))
: fixture(input)
const bundle = await rollup({
input: fixture(input),
input: formattedInput,
plugins: [postcss(postCssOptions), delayResolve && lateResolve].filter(
Boolean
)
})
await bundle.write({
format: 'cjs',
file: path.join(outDir, 'bundle.js')
})
const writeOptions = multipleInputs
? { format: 'cjs', dir: outDir }
: { format: 'cjs', file: path.join(outDir, 'bundle.js') }
await bundle.write(writeOptions)
let cssCodePath = path.join(outDir, 'bundle.css')
if (typeof options.extract === 'string') {
cssCodePath = path.isAbsolute(options.extract) ? options.extract : path.join(outDir, options.extract)
Expand Down Expand Up @@ -336,6 +340,24 @@ snapshotMany('extract', [
extract: true,
delayResolve: true
}
},
{
title: 'multiple-inputs-resolve',
input: ['multiple-inputs/bundle.js', 'multiple-inputs/other.js'],
options: {
sourceMap: 'inline',
extract: 'index.css',
delayResolve: true,
},
},
{
title: 'multiple-inputs-resolve-reverse',
input: ['multiple-inputs/other.js', 'multiple-inputs/bundle.js'],
options: {
sourceMap: 'inline',
extract: 'index.css',
delayResolve: true,
},
}
])

Expand Down