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

Always recreate paths for external modules #2706

Merged
merged 2 commits into from Feb 19, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'))
])
);
});
});