Skip to content

Commit

Permalink
fix: move bundle asset tagging later (#546)
Browse files Browse the repository at this point in the history
Allows for all filenames to be fully-resolved first, otherwise things get hairy. Also making sure that bundles are tagged even if they don't have any dependencies beyond their own CSS chunk
  • Loading branch information
tivac authored Jan 21, 2019
1 parent 32e58e6 commit 2fef785
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
50 changes: 25 additions & 25 deletions packages/rollup/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,25 +237,17 @@ module.exports = (opts) => {
});

// Figure out if there were any CSS files that the JS didn't reference
const unused = [];

processor.dependencies().forEach((css) => {
if(queued.has(css)) {
return;
}

queued.add(css);

unused.push(css);
});
const unused = processor
.dependencies()
.filter((css) => !queued.has(css));

// Shove any unreferenced CSS files onto the beginning of the first chunk
if(unused.length) {
out.set("unused", {
// Add .css automatically down below, so strip in case it was specified
// .css extension is added automatically down below, so strip in case it was specified
name : common.replace(path.extname(common), ""),
files : unused,
dependencies : false
dependencies : [],
});
}

Expand All @@ -275,7 +267,7 @@ module.exports = (opts) => {
const filenames = new Map();

for(const [ entry, value ] of out.entries()) {
const { name, files, dependencies } = value;
const { name, files } = value;

const id = this.emitAsset(`${name}.css`);

Expand All @@ -298,16 +290,6 @@ module.exports = (opts) => {

filenames.set(entry, dest);

// If this bundle has CSS dependencies, tag it with the filenames
if(dependencies) {
bundle[entry].assets = [
...dependencies
.filter((dep) => out.has(dep))
.map((dep) => filenames.get(dep)),
dest,
];
}

// Maps can't be written out via the asset APIs becuase they shouldn't ever be hashed.
// They shouldn't be hashed because they simply follow the name of their parent .css asset.
// So add them to the bundle directly.
Expand All @@ -332,6 +314,24 @@ module.exports = (opts) => {
}
}

// If this bundle has CSS dependencies, stick them on the object for other plugins to reference
// Has to happen in a second loop to ensure that all filenames are correctly resolved
for(const [ entry, { dependencies }] of out.entries()) {
// unused CSS doesn't correspond to a bundle, so don't bother
if(!bundle[entry]) {
continue;
}

log("attaching assets", entry);

bundle[entry].assets = [
...dependencies
.filter((dep) => out.has(dep))
.map((dep) => filenames.get(dep)),
filenames.get(entry),
];
}

if(options.json) {
const dest = typeof options.json === "string" ? options.json : "exports.json";

Expand All @@ -353,7 +353,7 @@ module.exports = (opts) => {
if(!assets) {
return;
}

meta[entry] = {
dependencies : assets,
};
Expand Down
5 changes: 5 additions & 0 deletions packages/rollup/test/__snapshots__/rollup.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ Array [
"css output",
"simple.css",
],
Array [
"[rollup]",
"attaching assets",
"simple.js",
],
Array [
"[processor]",
"file()",
Expand Down

0 comments on commit 2fef785

Please sign in to comment.