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

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

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

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

@ -73,11 +73,12 @@ describe("runtime", () => {
);
const { code } = svelte.compile(source, compileOptions);
const startIndex = code.indexOf("function create_main_fragment"); // may change!
if (startIndex === -1)
throw new Error("missing create_main_fragment");
if (startIndex === -1) throw new Error("missing create_main_fragment");
const endIndex = code.lastIndexOf("return");
const es5 =
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 });
if (/Object\.assign/.test(es5)) {

Loading…
Cancel
Save