Skip to content

Commit 5d14bee

Browse files
committed
Fix IIFE AutoNames
1 parent a3587ba commit 5d14bee

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

lib/impl/NollupCodeGenerator.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ function getExportAllFrom (namespace) {
9090
return `for(let __k__ in ${namespace}){__k__ !== "default" && (__e__(__k__, function () { return ${namespace}[__k__] }))};`
9191
}
9292

93+
/**
94+
* @param {string} source
95+
* @return {string}
96+
*/
97+
function getIIFEName(source) {
98+
return source.replace(/\-([\w])/g, (m) => m[1].toUpperCase()).replace(/[^a-zA-Z0-9$_]/g, '_');
99+
}
100+
93101
/**
94102
* @param {RollupRenderedChunk} chunk
95103
* @param {RollupOutputOptions} outputOptions
@@ -112,7 +120,7 @@ function createExternalImports (chunk, outputOptions) {
112120
}
113121

114122
return specifiers.map(s => {
115-
let iifeName = globals[source] || name;
123+
let iifeName = format === 'iife'? (globals[source] || getIIFEName(source)) : '';
116124

117125
if (s === '*') {
118126
if (format === 'es')

test/cases/Externals.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ let EXTERNAL_MODULES = {
8282
'DefaultFallbackModule': {
8383
prop: true
8484
},
85-
'_IIFE_Special_Characters_': {
85+
'IIFESpecial__CharactersTest$__': {
8686
NamedExport1: 123,
8787
NamedExport2: 456
8888
},
@@ -387,15 +387,15 @@ describe('External', () => {
387387
describe('IIFE Name Conversion', () => {
388388
it ('should convert special characters to underscore', async () => {
389389
fs.stub('./src/main.js', () => `
390-
import { NamedExport1, NamedExport2 } from "+IIFE-Special-Characters$";
390+
import { NamedExport1, NamedExport2 } from "-iIFE-special+_Characters-Test$)(";
391391
if (NamedExport1 === 123 && NamedExport2 === 456) {
392392
self.result = true;
393393
}
394394
`);
395395

396396
let bundle = await nollup({
397397
input: './src/main.js',
398-
external: ['+IIFE-Special-Characters$']
398+
external: ['-iIFE-special+_Characters-Test$)(']
399399
});
400400

401401
let { output } = await bundle.generate({ format: 'iife' });
@@ -406,18 +406,20 @@ describe('External', () => {
406406

407407
it ('should use global object to determine variable name', async () => {
408408
fs.stub('./src/main.js', () => `
409-
import { NamedExport1, NamedExport2 } from "+IIFE-Special-Characters$";
409+
import { NamedExport1, NamedExport2 } from "-iIFE-special+_Characters-Test$)(";
410410
if (NamedExport1 === 123 && NamedExport2 === 456) {
411-
self.result = true;
411+
if (__globalModule.NamedExport1 && __globalModule.NamedExport2) {
412+
self.result = true;
413+
}
412414
}
413415
`);
414416

415417
let bundle = await nollup({
416418
input: './src/main.js',
417-
external: ['+IIFE-Special-Characters$'],
419+
external: ['-iIFE-special+_Characters-Test$)('],
418420
output: {
419421
globals: {
420-
'+IIFE-Special-Characters$': '__globalModule'
422+
'-iIFE-special+_Characters-Test$)(': '__globalModule'
421423
}
422424
}
423425
});

0 commit comments

Comments
 (0)