Skip to content

Commit

Permalink
Always recreate paths for external modules (#2706)
Browse files Browse the repository at this point in the history
* Always recreate paths for external modules

* Make sure it is possible to both use the "paths" option and have other
external dependencies
  • Loading branch information
lukastaegert committed Feb 19, 2019
1 parent b96f5bc commit 0e4fe6f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Chunk.ts
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/ExternalModule.ts
Expand Up @@ -37,9 +37,11 @@ export default class ExternalModule {
}

setRenderPath(options: OutputOptions, inputBase: string) {
if (options.paths)
this.renderPath = '';
if (options.paths) {
this.renderPath =
typeof options.paths === 'function' ? options.paths(this.id) : options.paths[this.id];
}
if (!this.renderPath) {
if (!isAbsolute(this.id)) {
this.renderPath = this.id;
Expand Down
22 changes: 22 additions & 0 deletions 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}`);
}
}
};
5 changes: 5 additions & 0 deletions 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');
24 changes: 24 additions & 0 deletions test/misc/misc.js
Expand Up @@ -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'))
])
);
});
});

0 comments on commit 0e4fe6f

Please sign in to comment.