Skip to content
Permalink
Browse files

fix: tighten up rollup bundles (#450)

BREAKING CHANGE: Common chunks will only be created if necessary now.
  • Loading branch information...
tivac committed Jul 13, 2018
1 parent 1d83e3c commit e7170d16f21d1da76034495e1de839661b5ea988
@@ -134,9 +134,9 @@ module.exports = function(opts) {
Object.keys(bundles).forEach((entry) => {
const file = {
entry,
base : extensionless(entry),

css : [ ],
base : extensionless(entry),
css : [],
};

// Get CSS files being used by each entry point
@@ -153,45 +153,51 @@ module.exports = function(opts) {
start,
];

file.css = file.css.concat(used);
file.css.push(...used);

used.forEach((dep) => {
usage.set(dep, usage.has(dep) ? usage.get(dep) + 1 : 1);
usage.set(dep, (usage.get(dep) || 0) + 1);
});
});

files.push(file);
});

// Second pass removes any dependencies appearing in multiple bundles
files.forEach((file) => {
const { css } = file;
if(files.length === 1) {
// Only one entry file means we only need one bundle.
files[0].css = processor.dependencies();
} else {
// Multiple bundles means ref-counting to find the shared deps
// Second pass removes any dependencies appearing in multiple bundles
files.forEach((file) => {
const { css } = file;

file.css = css.filter((dep) => {
if(usage.get(dep) > 1) {
common.set(dep, true);
file.css = css.filter((dep) => {
if(usage.get(dep) > 1) {
common.set(dep, true);

return false;
}
return false;
}

return true;
return true;
});
});
});

// Add any other files that weren't part of a bundle to the common chunk
Object.keys(processor.files).forEach((file) => {
if(!usage.has(file)) {
common.set(file, true);
}
});

// Common chunk only emitted if necessary
if(common.size) {
files.push({
entry : options.common,
base : extensionless(options.common),
css : [ ...common.keys() ],
// Add any other files that weren't part of a bundle to the common chunk
Object.keys(processor.files).forEach((file) => {
if(!usage.has(file)) {
common.set(file, true);
}
});

// Common chunk only emitted if necessary
if(common.size) {
files.push({
entry : options.common,
base : extensionless(options.common),
css : [ ...common.keys() ],
});
}
}

await Promise.all(
@@ -89,7 +89,11 @@ exports[`/rollup.js watch mode should update when a dependency changes 2`] = `
`;

exports[`/rollup.js watch mode should update when a shared dependency changes 1`] = `
"/* packages/rollup/test/output/watch/shared-deps/one.css */
"/* packages/rollup/test/output/watch/shared-deps/two.css */
.mc5067d789_two {
color: green;
}
/* packages/rollup/test/output/watch/shared-deps/one.css */
.mca79168b9_one {
color: red;
}
@@ -102,28 +106,18 @@ exports[`/rollup.js watch mode should update when a shared dependency changes 1`
exports[`/rollup.js watch mode should update when a shared dependency changes 2`] = `
"/* packages/rollup/test/output/watch/shared-deps/two.css */
.mc5067d789_two {
color: green;
}"
`;

exports[`/rollup.js watch mode should update when a shared dependency changes 3`] = `
"/* packages/rollup/test/output/watch/shared-deps/one.css */
color: yellow;
}
/* packages/rollup/test/output/watch/shared-deps/one.css */
.mca79168b9_one {
color: blue;
color: red;
}
/* packages/rollup/test/output/watch/shared-deps/three.css */
.mc4ec9318b_three {
color: teal;
}"
`;

exports[`/rollup.js watch mode should update when a shared dependency changes 4`] = `
"/* packages/rollup/test/output/watch/shared-deps/two.css */
.mc5067d789_two {
color: green;
}"
`;

exports[`/rollup.js watch mode should update when adding new css files 1`] = `
"/* packages/rollup/test/output/watch/new-file/one.css */
.mc54f12712_one {
@@ -80,17 +80,13 @@ exports[`/rollup.js code splitting should support splitting up CSS files 2`] = `
`;

exports[`/rollup.js should accept an existing processor instance 1`] = `
"/* packages/rollup/test/specimens/simple.css */
.fooga {
color: red;
}
"
`;

exports[`/rollup.js should accept an existing processor instance 2`] = `
"/* packages/rollup/test/specimens/fake.css */
.fake {
color: yellow;
}
/* packages/rollup/test/specimens/simple.css */
.fooga {
color: red;
}"
`;

@@ -269,20 +265,6 @@ console.log(css, css$1);
`;

exports[`/rollup.js shouldn't over-remove files from an existing processor instance 2`] = `
"/* packages/rollup/test/specimens/repeated-references/a.css */
.a {

color: red;
}
/* packages/rollup/test/specimens/repeated-references/b.css */
.b {

color: blue;
}
"
`;

exports[`/rollup.js shouldn't over-remove files from an existing processor instance 3`] = `
"/* packages/rollup/test/specimens/repeated-references/d.css */
.d {
color: darkblue;
@@ -291,5 +273,15 @@ exports[`/rollup.js shouldn't over-remove files from an existing processor insta
.c {
color: cadetblue;
}
/* packages/rollup/test/specimens/repeated-references/b.css */
.b {

color: blue;
}
/* packages/rollup/test/specimens/repeated-references/a.css */
.a {

color: red;
}
"
`;
@@ -298,24 +298,21 @@ describe("/rollup.js", () => {
});

// Create v2 of the file after a bit
setTimeout(() => write(`./watch/shared-deps/one.css`, dedent(`
.one {
composes: two from "./two.css";
color: blue;
setTimeout(() => write(`./watch/shared-deps/two.css`, dedent(`
.two {
color: yellow;
}
`)), 200);

watcher.on("event", watching((builds) => {
if(builds === 1) {
expect(read("./watch/shared-deps/assets/watch-output.css")).toMatchSnapshot();
expect(read("./watch/shared-deps/assets/common.css")).toMatchSnapshot();

// continue watching
return;
}

expect(read("./watch/shared-deps/assets/watch-output.css")).toMatchSnapshot();
expect(read("./watch/shared-deps/assets/common.css")).toMatchSnapshot();

return done();
}));
@@ -346,7 +346,6 @@ describe("/rollup.js", () => {
});

expect(read("./rollup/existing-processor/assets/existing-processor.css")).toMatchSnapshot();
expect(read("./rollup/existing-processor/assets/common.css")).toMatchSnapshot();
});

it("shouldn't over-remove files from an existing processor instance", async () => {
@@ -376,7 +375,6 @@ describe("/rollup.js", () => {

expect(read("./rollup/repeated-references/repeated-references.js")).toMatchSnapshot();
expect(read("./rollup/repeated-references/assets/repeated-references.css")).toMatchSnapshot();
expect(read("./rollup/repeated-references/assets/common.css")).toMatchSnapshot();
});

describe("errors", () => {

0 comments on commit e7170d1

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