From c7057a598288970b701272f55312d28933b7b50f Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 18 Feb 2019 07:18:08 +0100 Subject: [PATCH 1/2] Always recreate paths for external modules --- src/Chunk.ts | 2 +- src/ExternalModule.ts | 4 ++-- test/misc/misc.js | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Chunk.ts b/src/Chunk.ts index ca2f101b335..d423f1927db 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -514,7 +514,7 @@ export default class Chunk { let id: string; let globalName: string; if (dep instanceof ExternalModule) { - id = dep.renderPath || dep.setRenderPath(options, inputBase); + id = dep.setRenderPath(options, inputBase); if (options.format === 'umd' || options.format === 'iife') { globalName = getGlobalName( dep, diff --git a/src/ExternalModule.ts b/src/ExternalModule.ts index 29e182a4793..3788d594cde 100644 --- a/src/ExternalModule.ts +++ b/src/ExternalModule.ts @@ -37,10 +37,10 @@ export default class ExternalModule { } setRenderPath(options: OutputOptions, inputBase: string) { - if (options.paths) + if (options.paths) { this.renderPath = typeof options.paths === 'function' ? options.paths(this.id) : options.paths[this.id]; - if (!this.renderPath) { + } else { if (!isAbsolute(this.id)) { this.renderPath = this.id; } else { diff --git a/test/misc/misc.js b/test/misc/misc.js index e0f23574c09..2eb4d9de1e9 100644 --- a/test/misc/misc.js +++ b/test/misc/misc.js @@ -98,4 +98,28 @@ describe('misc', () => { plugins: [loader({ x: `console.log( 42 );` }), null, false, undefined] }); }); + + it('handles different import paths for different outputs', () => { + return rollup + .rollup({ + input: 'x', + external: ['the-answer'], + plugins: [loader({ x: `import 'the-answer'` })] + }) + .then(bundle => + Promise.all([ + bundle + .generate({ format: 'esm' }) + .then(generated => assert.equal(generated.output[0].code, "import 'the-answer';\n", 'no render path 1')), + bundle + .generate({ format: 'esm', paths: id => `//unpkg.com/${id}@?module` }) + .then(generated => + assert.equal(generated.output[0].code, "import '//unpkg.com/the-answer@?module';\n", 'with render path') + ), + bundle + .generate({ format: 'esm' }) + .then(generated => assert.equal(generated.output[0].code, "import 'the-answer';\n", 'no render path 2')) + ]) + ); + }); }); From e7de6a1cf519bcb9cbb1981f3663e10d09cb6965 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 18 Feb 2019 08:14:46 +0100 Subject: [PATCH 2/2] Make sure it is possible to both use the "paths" option and have other external dependencies --- src/ExternalModule.ts | 4 +++- .../samples/mixed-external-paths/_config.js | 22 +++++++++++++++++++ .../samples/mixed-external-paths/main.js | 5 +++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 test/function/samples/mixed-external-paths/_config.js create mode 100644 test/function/samples/mixed-external-paths/main.js diff --git a/src/ExternalModule.ts b/src/ExternalModule.ts index 3788d594cde..87f6a2d92c1 100644 --- a/src/ExternalModule.ts +++ b/src/ExternalModule.ts @@ -37,10 +37,12 @@ export default class ExternalModule { } setRenderPath(options: OutputOptions, inputBase: string) { + this.renderPath = ''; if (options.paths) { this.renderPath = typeof options.paths === 'function' ? options.paths(this.id) : options.paths[this.id]; - } else { + } + if (!this.renderPath) { if (!isAbsolute(this.id)) { this.renderPath = this.id; } else { diff --git a/test/function/samples/mixed-external-paths/_config.js b/test/function/samples/mixed-external-paths/_config.js new file mode 100644 index 00000000000..813b9de3e9d --- /dev/null +++ b/test/function/samples/mixed-external-paths/_config.js @@ -0,0 +1,22 @@ +module.exports = { + description: 'allows using the path option selectively', + options: { + external: ['dep-a', 'dep-b'], + output: { + paths: { + 'dep-a': 'secret-dep' + } + } + }, + context: { + require(id) { + if (id === 'secret-dep') { + return 'secret'; + } + if (id === 'dep-b') { + return 'b'; + } + throw new Error(`Unexpected dependency ${id}`); + } + } +}; diff --git a/test/function/samples/mixed-external-paths/main.js b/test/function/samples/mixed-external-paths/main.js new file mode 100644 index 00000000000..19cfa8c2ff4 --- /dev/null +++ b/test/function/samples/mixed-external-paths/main.js @@ -0,0 +1,5 @@ +import a from 'dep-a'; +import b from 'dep-b'; + +assert.equal(a, 'secret'); +assert.equal(b, 'b');