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(rewriter): don't explode on external chunks #749

Merged
merged 1 commit into from
Apr 21, 2020
Merged
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
4 changes: 4 additions & 0 deletions packages/rollup-rewriter/rewriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ module.exports = (opts = {}) => {
...(file in chunks ? chunks[file].imports : []),
file,
].reduce((out, curr) => {
if(!chunks[curr]) {
return out;
}

const { assets = [] } = chunks[curr];

assets.forEach((asset) => out.add(asset));
Expand Down
200 changes: 200 additions & 0 deletions packages/rollup-rewriter/test/__snapshots__/rewriter.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,206 @@

exports[`rollup-rewriter should error on unsupported formats ("cjs") 1`] = `"Unsupported format: cjs. Supported formats are [\\"amd\\",\\"es\\",\\"esm\\",\\"system\\"]"`;

exports[`rollup-rewriter should ignore unknown imports 1`] = `
Object {
"a.js": "
define(['require', 'external'], function (require, external) { 'use strict';

external = external && Object.prototype.hasOwnProperty.call(external, 'default') ? external['default'] : external;

var css = {
\\"a\\": \\"a\\"
};

function a() {
console.log(css, external);

new Promise(function (resolve, reject) { Promise.all([
lazyload(\\"./assets/c.css\\"),
new Promise(function (resolve, reject) { require(['./c'], resolve, reject) })
])
.then((results) => resolve(results[results.length - 1]))
.catch(reject) }).then(console.log);
}

return a;

});
",
"assets/a.css": "
/* packages/rollup-rewriter/test/specimens/external-import/a.css */
.a { color: aqua; }
",
"assets/c.css": "
/* packages/rollup-rewriter/test/specimens/external-import/c.css */
.c {
color: cyan;
}
",
"c.js": "
define(['exports'], function (exports) { 'use strict';

var css = {
\\"c\\": \\"c\\"
};

console.log(css);

exports.default = css;

});
",
}
`;

exports[`rollup-rewriter should ignore unknown imports 2`] = `
Object {
"a.js": "
import external from 'external';

var css = {
\\"a\\": \\"a\\"
};

function a() {
console.log(css, external);

Promise.all([
lazyload(\\"./assets/c.css\\"),
import('./c.js')
])
.then((results) => results[results.length - 1]).then(console.log);
}

export default a;
",
"assets/a.css": "
/* packages/rollup-rewriter/test/specimens/external-import/a.css */
.a { color: aqua; }
",
"assets/c.css": "
/* packages/rollup-rewriter/test/specimens/external-import/c.css */
.c {
color: cyan;
}
",
"c.js": "
var css = {
\\"c\\": \\"c\\"
};

console.log(css);

export default css;
",
}
`;

exports[`rollup-rewriter should ignore unknown imports 3`] = `
Object {
"a.js": "
import external from 'external';

var css = {
\\"a\\": \\"a\\"
};

function a() {
console.log(css, external);

Promise.all([
lazyload(\\"./assets/c.css\\"),
import('./c.js')
])
.then((results) => results[results.length - 1]).then(console.log);
}

export default a;
",
"assets/a.css": "
/* packages/rollup-rewriter/test/specimens/external-import/a.css */
.a { color: aqua; }
",
"assets/c.css": "
/* packages/rollup-rewriter/test/specimens/external-import/c.css */
.c {
color: cyan;
}
",
"c.js": "
var css = {
\\"c\\": \\"c\\"
};

console.log(css);

export default css;
",
}
`;

exports[`rollup-rewriter should ignore unknown imports 4`] = `
Object {
"a.js": "
System.register(['external'], function (exports, module) {
'use strict';
var external;
return {
setters: [function (module) {
external = module.default;
}],
execute: function () {

exports('default', a);

var css = {
\\"a\\": \\"a\\"
};

function a() {
console.log(css, external);

Promise.all([
lazyload(\\"./assets/c.css\\"),
module.import('./c.js')
])
.then((results) => results[results.length - 1]).then(console.log);
}

}
};
});
",
"assets/a.css": "
/* packages/rollup-rewriter/test/specimens/external-import/a.css */
.a { color: aqua; }
",
"assets/c.css": "
/* packages/rollup-rewriter/test/specimens/external-import/c.css */
.c {
color: cyan;
}
",
"c.js": "
System.register([], function (exports) {
'use strict';
return {
execute: function () {

var css = {
\\"c\\": \\"c\\"
};

console.log(css);
exports('default', css);

}
};
});
",
}
`;

exports[`rollup-rewriter should include css for static imports used by a dynamic import ("amd") 1`] = `
Object {
"assets/dynamic1.css": "
Expand Down
31 changes: 29 additions & 2 deletions packages/rollup-rewriter/test/rewriter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const chunkFileNames = "[name].js";
const map = false;
const sourcemap = false;

const formats = [[ "amd" ], [ "es" ], [ "esm" ], [ "system" ]];
const formats = [ "amd", "es", "esm", "system" ];

describe("rollup-rewriter", () => {
beforeAll(() => shell.rm("-rf", prefix("./output/*")));
Expand All @@ -28,7 +28,7 @@ describe("rollup-rewriter", () => {
});

it.each([
[ "cjs" ],
"cjs",
])("should error on unsupported formats (%p)", async (format) => {
const bundle = await rollup({
input : [
Expand Down Expand Up @@ -169,6 +169,32 @@ describe("rollup-rewriter", () => {
expect(result).toMatchRollupSnapshot();
});

it.each(formats)("should ignore unknown imports", async (format) => {
const bundle = await rollup({
input : require.resolve("./specimens/external-import/a.js"),
external : [ "external" ],
plugins : [
css({
namer,
map,
}),
rewriter({
loadfn : "lazyload",
}),
],
});

const result = await bundle.generate({
format,
sourcemap,

assetFileNames,
chunkFileNames,
});

expect(result).toMatchRollupSnapshot();
});

it.each(formats)("should include css for static imports used by a dynamic import (%p)", async (format) => {
const bundle = await rollup({
input : [
Expand Down Expand Up @@ -197,6 +223,7 @@ describe("rollup-rewriter", () => {
expect(result).toMatchRollupSnapshot();
});

// eslint-disable-next-line jest/expect-expect
it("should log details in verbose mode", async () => {
const { logSnapshot } = logs();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.a { color: aqua; }
8 changes: 8 additions & 0 deletions packages/rollup-rewriter/test/specimens/external-import/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import external from "external";
import css from "./a.css";

export default function() {
console.log(css, external);

import("./c.js").then(console.log);
}
3 changes: 3 additions & 0 deletions packages/rollup-rewriter/test/specimens/external-import/c.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.c {
color: cyan;
}
5 changes: 5 additions & 0 deletions packages/rollup-rewriter/test/specimens/external-import/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import css from "./c.css";

console.log(css);

export default css;