Skip to content
Permalink
Browse files

fix: move bundle asset tagging later (#546)

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 committed Jan 21, 2019
1 parent 32e58e6 commit 2fef78512548f7adad63be0f06dc7e456fa92203
Showing with 30 additions and 25 deletions.
  1. +25 −25 packages/rollup/rollup.js
  2. +5 −0 packages/rollup/test/__snapshots__/rollup.test.js.snap
@@ -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 : [],
});
}

@@ -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`);

@@ -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.
@@ -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";

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

meta[entry] = {
dependencies : assets,
};
@@ -266,6 +266,11 @@ Array [
"css output",
"simple.css",
],
Array [
"[rollup]",
"attaching assets",
"simple.js",
],
Array [
"[processor]",
"file()",

0 comments on commit 2fef785

Please sign in to comment.
You can’t perform that action at this time.