diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index e8d2ce47b0..aadd5a10f1 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -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; diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 3aa44d8f93..4455321b61 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -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, }); diff --git a/src/generators/shared/utils/getModuleWrapper.ts b/src/generators/shared/utils/getModuleWrapper.ts index ae9166122f..c535990f9f 100644 --- a/src/generators/shared/utils/getModuleWrapper.ts +++ b/src/generators/shared/utils/getModuleWrapper.ts @@ -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}; + }());` }; } diff --git a/src/interfaces.ts b/src/interfaces.ts index 93d1b608f2..97c7879ab6 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -64,6 +64,7 @@ export interface CompileOptions { export interface GenerateOptions { name: string; format: ModuleFormat; + banner?: string; } export interface Visitor { diff --git a/test/runtime/index.js b/test/runtime/index.js index 95950b6668..0d4fbfd81c 100644 --- a/test/runtime/index.js +++ b/test/runtime/index.js @@ -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)) {