From abbd10f56f589c9814dd8cfcfecf6d80d957d2d4 Mon Sep 17 00:00:00 2001 From: Pat Cavit Date: Tue, 21 Apr 2020 13:16:28 -0700 Subject: [PATCH] fix(rewriter): don't explode on external chunks Fix #688 --- packages/rollup-rewriter/rewriter.js | 4 + .../test/__snapshots__/rewriter.test.js.snap | 200 ++++++++++++++++++ .../rollup-rewriter/test/rewriter.test.js | 31 ++- .../test/specimens/external-import/a.css | 1 + .../test/specimens/external-import/a.js | 8 + .../test/specimens/external-import/c.css | 3 + .../test/specimens/external-import/c.js | 5 + 7 files changed, 250 insertions(+), 2 deletions(-) create mode 100644 packages/rollup-rewriter/test/specimens/external-import/a.css create mode 100644 packages/rollup-rewriter/test/specimens/external-import/a.js create mode 100644 packages/rollup-rewriter/test/specimens/external-import/c.css create mode 100644 packages/rollup-rewriter/test/specimens/external-import/c.js diff --git a/packages/rollup-rewriter/rewriter.js b/packages/rollup-rewriter/rewriter.js index 66d14498c..1072c4991 100644 --- a/packages/rollup-rewriter/rewriter.js +++ b/packages/rollup-rewriter/rewriter.js @@ -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)); diff --git a/packages/rollup-rewriter/test/__snapshots__/rewriter.test.js.snap b/packages/rollup-rewriter/test/__snapshots__/rewriter.test.js.snap index ae6a96e42..857a68de7 100644 --- a/packages/rollup-rewriter/test/__snapshots__/rewriter.test.js.snap +++ b/packages/rollup-rewriter/test/__snapshots__/rewriter.test.js.snap @@ -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": " diff --git a/packages/rollup-rewriter/test/rewriter.test.js b/packages/rollup-rewriter/test/rewriter.test.js index c8e16b7d5..c80600013 100644 --- a/packages/rollup-rewriter/test/rewriter.test.js +++ b/packages/rollup-rewriter/test/rewriter.test.js @@ -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/*"))); @@ -28,7 +28,7 @@ describe("rollup-rewriter", () => { }); it.each([ - [ "cjs" ], + "cjs", ])("should error on unsupported formats (%p)", async (format) => { const bundle = await rollup({ input : [ @@ -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 : [ @@ -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(); diff --git a/packages/rollup-rewriter/test/specimens/external-import/a.css b/packages/rollup-rewriter/test/specimens/external-import/a.css new file mode 100644 index 000000000..eb93c954b --- /dev/null +++ b/packages/rollup-rewriter/test/specimens/external-import/a.css @@ -0,0 +1 @@ +.a { color: aqua; } diff --git a/packages/rollup-rewriter/test/specimens/external-import/a.js b/packages/rollup-rewriter/test/specimens/external-import/a.js new file mode 100644 index 000000000..a14b61b3f --- /dev/null +++ b/packages/rollup-rewriter/test/specimens/external-import/a.js @@ -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); +} diff --git a/packages/rollup-rewriter/test/specimens/external-import/c.css b/packages/rollup-rewriter/test/specimens/external-import/c.css new file mode 100644 index 000000000..6ffbc8359 --- /dev/null +++ b/packages/rollup-rewriter/test/specimens/external-import/c.css @@ -0,0 +1,3 @@ +.c { + color: cyan; +} diff --git a/packages/rollup-rewriter/test/specimens/external-import/c.js b/packages/rollup-rewriter/test/specimens/external-import/c.js new file mode 100644 index 000000000..13c3c44db --- /dev/null +++ b/packages/rollup-rewriter/test/specimens/external-import/c.js @@ -0,0 +1,5 @@ +import css from "./c.css"; + +console.log(css); + +export default css;