wrap ES modules in an IIFE

pull/859/head
Rich Harris 7 years ago
parent 240291604b
commit abea37af71

@ -302,7 +302,7 @@ export default class Generator {
return (expression._dependencies = dependencies); return (expression._dependencies = dependencies);
} }
generate(result: string, options: CompileOptions, { name, format }: GenerateOptions ) { generate(result: string, options: CompileOptions, { banner = '', name, format }: GenerateOptions ) {
const pattern = /\[✂(\d+)-(\d+)$/; const pattern = /\[✂(\d+)-(\d+)$/;
const parts = result.split('✂]'); const parts = result.split('✂]');
@ -318,7 +318,7 @@ export default class Generator {
const { intro, outro } = getModuleWrapper(format, name, options, this.imports, this.source); const { intro, outro } = getModuleWrapper(format, name, options, this.imports, this.source);
addString(intro + '\n\n'); addString(banner + intro + '\n\n');
const { filename } = options; const { filename } = options;

@ -326,6 +326,8 @@ export default function dom(
return generator.alias(name); return generator.alias(name);
}); });
let banner = `/* ${options.filename ? `${options.filename} ` : ``}generated by Svelte v${version} */\n`;
if (sharedPath) { if (sharedPath) {
const used = Array.from(usedHelpers).sort(); const used = Array.from(usedHelpers).sort();
if (format === 'es') { if (format === 'es') {
@ -334,9 +336,7 @@ export default function dom(
return name !== alias ? `${name} as ${alias}` : name; return name !== alias ? `${name} as ${alias}` : name;
}); });
result = banner += `import { ${names.join(', ')} } from ${JSON.stringify(sharedPath)};\n\n`;
`import { ${names.join(', ')} } from ${JSON.stringify(sharedPath)};\n\n` +
result;
} }
else if (format === 'cjs') { else if (format === 'cjs') {
@ -347,7 +347,7 @@ export default function dom(
requires += `\nvar ${alias} = ${SHARED}.${name};`; requires += `\nvar ${alias} = ${SHARED}.${name};`;
}); });
result = `${requires}\n\n${result}`; banner += requires + '\n\n';
} }
else { else {
@ -391,20 +391,19 @@ export default function dom(
// special case // special case
const global = `_svelteTransitionManager`; const global = `_svelteTransitionManager`;
result += `\n\nvar ${generator.alias('transitionManager')} = window.${global} || (window.${global} = ${code});`; banner += `\n\nvar ${generator.alias('transitionManager')} = window.${global} || (window.${global} = ${code});`;
} else { } else {
const alias = generator.alias(expression.id.name); const alias = generator.alias(expression.id.name);
if (alias !== expression.id.name) if (alias !== expression.id.name)
code.overwrite(expression.id.start, expression.id.end, alias); code.overwrite(expression.id.start, expression.id.end, alias);
result += `\n\n${code}`; banner += `\n\n${code}`;
} }
}); });
} }
result = `/* ${options.filename ? `${options.filename} ` : ``}generated by Svelte v${version} */\n\n${result}`;
return generator.generate(result, options, { return generator.generate(result, options, {
banner,
name, name,
format, format,
}); });

@ -68,8 +68,14 @@ function getEsWrapper(name: string, options: CompileOptions, imports: Node[], so
.join('\n'); .join('\n');
return { return {
intro: importBlock ? importBlock + '\n\n' : '', intro: deindent`
outro: `export default ${name};` ${importBlock}
export default (function() {
`,
outro: deindent`
return ${name};
}());`
}; };
} }

@ -64,6 +64,7 @@ export interface CompileOptions {
export interface GenerateOptions { export interface GenerateOptions {
name: string; name: string;
format: ModuleFormat; format: ModuleFormat;
banner?: string;
} }
export interface Visitor { export interface Visitor {

@ -73,11 +73,12 @@ describe("runtime", () => {
); );
const { code } = svelte.compile(source, compileOptions); const { code } = svelte.compile(source, compileOptions);
const startIndex = code.indexOf("function create_main_fragment"); // may change! const startIndex = code.indexOf("function create_main_fragment"); // may change!
if (startIndex === -1) if (startIndex === -1) throw new Error("missing create_main_fragment");
throw new Error("missing create_main_fragment"); const endIndex = code.lastIndexOf("return");
const es5 = const es5 =
code.slice(0, startIndex).split('\n').map(x => spaces(x.length)).join('\n') + code.slice(0, startIndex).split('\n').map(x => spaces(x.length)).join('\n') +
code.slice(startIndex).replace(/export default .+/, ""); code.slice(startIndex, endIndex);
acorn.parse(es5, { ecmaVersion: 5 }); acorn.parse(es5, { ecmaVersion: 5 });
if (/Object\.assign/.test(es5)) { if (/Object\.assign/.test(es5)) {

Loading…
Cancel
Save