diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index 9c81546b64..d90cfaab38 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -8,8 +8,7 @@ import globalWhitelist from '../utils/globalWhitelist'; import reservedNames from '../utils/reservedNames'; import namespaces from '../utils/namespaces'; import { removeNode, removeObjectKey } from '../utils/removeNode'; -import getIntro from './shared/utils/getIntro'; -import getOutro from './shared/utils/getOutro'; +import wrapModule from './shared/utils/wrapModule'; import annotateWithScopes from '../utils/annotateWithScopes'; import clone from '../utils/clone'; import DomBlock from './dom/Block'; @@ -303,55 +302,12 @@ export default class Generator { return (expression._dependencies = dependencies); } - generate(result: string, options: CompileOptions, { name, format }: GenerateOptions ) { - if (this.imports.length) { - const statements: string[] = []; - - this.imports.forEach((declaration, i) => { - if (format === 'es') { - statements.push( - this.source.slice(declaration.start, declaration.end) - ); - return; - } - - const defaultImport = declaration.specifiers.find( - (x: Node) => - x.type === 'ImportDefaultSpecifier' || - (x.type === 'ImportSpecifier' && x.imported.name === 'default') - ); - const namespaceImport = declaration.specifiers.find( - (x: Node) => x.type === 'ImportNamespaceSpecifier' - ); - const namedImports = declaration.specifiers.filter( - (x: Node) => - x.type === 'ImportSpecifier' && x.imported.name !== 'default' - ); - - const name = defaultImport || namespaceImport - ? (defaultImport || namespaceImport).local.name - : `__import${i}`; - declaration.name = name; // hacky but makes life a bit easier later - - namedImports.forEach((specifier: Node) => { - statements.push( - `var ${specifier.local.name} = ${name}.${specifier.imported.name}` - ); - }); - - if (defaultImport) { - statements.push( - `${name} = (${name} && ${name}.__esModule) ? ${name}['default'] : ${name};` - ); - } - }); - - result = `${statements.join('\n')}\n\n${result}`; - } - + generate(result: string, options: CompileOptions, { banner = '', sharedPath, helpers, name, format }: GenerateOptions ) { const pattern = /\[✂(\d+)-(\d+)$/; - const parts = result.split('✂]'); + const module = wrapModule(result, format, name, options, banner, sharedPath, helpers, this.imports, this.source); + + const parts = module.split('✂]'); const finalChunk = parts.pop(); const compiled = new Bundle({ separator: '' }); @@ -362,9 +318,6 @@ export default class Generator { }); } - const intro = getIntro(format, options, this.imports); - if (intro) addString(intro); - const { filename } = options; // special case — the source file doesn't actually get used anywhere. we need @@ -391,7 +344,6 @@ export default class Generator { }); addString(finalChunk); - addString('\n\n' + getOutro(format, name, options, this.imports)); const { css, cssMap } = this.customElement ? { css: null, cssMap: null } : diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 9633eb34a2..22371c71bc 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -356,34 +356,20 @@ export default function dom( return generator.alias(name); }); - if (sharedPath) { - const used = Array.from(usedHelpers).sort(); - if (format === 'es') { - const names = used.map(name => { - const alias = generator.alias(name); - return name !== alias ? `${name} as ${alias}` : name; - }); - - result = - `import { ${names.join(', ')} } from ${JSON.stringify(sharedPath)};\n\n` + - result; - } - - else if (format === 'cjs') { - const SHARED = '__shared'; - let requires = `var ${SHARED} = require(${JSON.stringify(sharedPath)});`; - used.forEach(name => { - const alias = generator.alias(name); - requires += `\nvar ${alias} = ${SHARED}.${name};`; - }); + let helpers; - result = `${requires}\n\n${result}`; - } - - else { + if (sharedPath) { + if (format !== 'es' && format !== 'cjs') { throw new Error(`Components with shared helpers must be compiled with \`format: 'es'\` or \`format: 'cjs'\``); } + const used = Array.from(usedHelpers).sort(); + helpers = used.map(name => { + const alias = generator.alias(name); + return { name, alias }; + }); } else { + let inlineHelpers = ''; + usedHelpers.forEach(key => { const str = shared[key]; const code = new MagicString(str); @@ -421,20 +407,27 @@ export default function dom( // special case const global = `_svelteTransitionManager`; - result += `\n\nvar ${generator.alias('transitionManager')} = window.${global} || (window.${global} = ${code});`; + inlineHelpers += `\n\nvar ${generator.alias('transitionManager')} = window.${global} || (window.${global} = ${code});\n\n`; } 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}`; + inlineHelpers += `\n\n${code}`; } }); + + result += inlineHelpers; } - result = `/* ${options.filename ? `${options.filename} ` : ``}generated by Svelte v${version} */\n\n${result}`; + const filename = options.filename && ( + typeof process !== 'undefined' ? options.filename.replace(process.cwd(), '').replace(/^[\/\\]/, '') : options.filename + ); return generator.generate(result, options, { + banner: `/* ${filename ? `${filename} ` : ``}generated by Svelte v${version} */`, + sharedPath, + helpers, name, format, }); diff --git a/src/generators/shared/utils/getGlobals.ts b/src/generators/shared/utils/getGlobals.ts deleted file mode 100644 index eb0db21276..0000000000 --- a/src/generators/shared/utils/getGlobals.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { CompileOptions, Node } from '../../../interfaces'; - - -export default function getGlobals(imports: Node[], options: CompileOptions) { - const { globals, onerror, onwarn } = options; - const globalFn = getGlobalFn(globals); - - return imports.map(x => { - let name = globalFn(x.source.value); - - if (!name) { - if (x.name.startsWith('__import')) { - const error = new Error( - `Could not determine name for imported module '${x.source.value}' – use options.globals` - ); - if (onerror) { - onerror(error); - } else { - throw error; - } - } else { - const warning = { - message: `No name was supplied for imported module '${x.source.value}'. Guessing '${x.name}', but you should use options.globals`, - }; - - if (onwarn) { - onwarn(warning); - } else { - console.warn(warning); // eslint-disable-line no-console - } - } - - name = x.name; - } - - return name; - }); -} - -function getGlobalFn(globals: any): (id: string) => string { - if (typeof globals === 'function') return globals; - if (typeof globals === 'object') { - return id => globals[id]; - } - - return () => undefined; -} diff --git a/src/generators/shared/utils/getIntro.ts b/src/generators/shared/utils/getIntro.ts deleted file mode 100644 index 599bc5f7b3..0000000000 --- a/src/generators/shared/utils/getIntro.ts +++ /dev/null @@ -1,95 +0,0 @@ -import deindent from '../../../utils/deindent'; -import getGlobals from './getGlobals'; -import { CompileOptions, ModuleFormat, Node } from '../../../interfaces'; - -export default function getIntro( - format: ModuleFormat, - options: CompileOptions, - imports: Node[] -) { - if (format === 'es') return ''; - if (format === 'amd') return getAmdIntro(options, imports); - if (format === 'cjs') return getCjsIntro(options, imports); - if (format === 'iife') return getIifeIntro(options, imports); - if (format === 'umd') return getUmdIntro(options, imports); - if (format === 'eval') return getEvalIntro(options, imports); - - throw new Error(`Not implemented: ${format}`); -} - -function getAmdIntro(options: CompileOptions, imports: Node[]) { - const sourceString = imports.length - ? `[${imports - .map(declaration => `'${removeExtension(declaration.source.value)}'`) - .join(', ')}], ` - : ''; - - const id = options.amd && options.amd.id; - - return `define(${id - ? `"${id}", ` - : ''}${sourceString}function(${paramString(imports)}) { 'use strict';\n\n`; -} - -function getCjsIntro(options: CompileOptions, imports: Node[]) { - const requireBlock = imports - .map( - declaration => - `var ${declaration.name} = require('${declaration.source.value}');` - ) - .join('\n\n'); - - if (requireBlock) { - return `'use strict';\n\n${requireBlock}\n\n`; - } - - return `'use strict';\n\n`; -} - -function getIifeIntro(options: CompileOptions, imports: Node[]) { - if (!options.name) { - throw new Error(`Missing required 'name' option for IIFE export`); - } - - return `var ${options.name} = (function(${paramString(imports)}) { 'use strict';\n\n`; -} - -function getUmdIntro(options: CompileOptions, imports: Node[]) { - if (!options.name) { - throw new Error(`Missing required 'name' option for UMD export`); - } - - const amdId = options.amd && options.amd.id ? `'${options.amd.id}', ` : ''; - - const amdDeps = imports.length - ? `[${imports - .map(declaration => `'${removeExtension(declaration.source.value)}'`) - .join(', ')}], ` - : ''; - const cjsDeps = imports - .map(declaration => `require('${declaration.source.value}')`) - .join(', '); - const globalDeps = getGlobals(imports, options); - - return ( - deindent` - (function(global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(${cjsDeps}) : - typeof define === 'function' && define.amd ? define(${amdId}${amdDeps}factory) : - (global.${options.name} = factory(${globalDeps})); - }(this, (function (${paramString(imports)}) { 'use strict';` + '\n\n' - ); -} - -function getEvalIntro(options: CompileOptions, imports: Node[]) { - return `(function (${paramString(imports)}) { 'use strict';\n\n`; -} - -function paramString(imports: Node[]) { - return imports.map(dep => dep.name).join(', '); -} - -function removeExtension(file: string) { - const index = file.lastIndexOf('.'); - return ~index ? file.slice(0, index) : file; -} diff --git a/src/generators/shared/utils/getOutro.ts b/src/generators/shared/utils/getOutro.ts deleted file mode 100644 index 682a3cc870..0000000000 --- a/src/generators/shared/utils/getOutro.ts +++ /dev/null @@ -1,37 +0,0 @@ -import getGlobals from './getGlobals'; -import { CompileOptions, Node } from '../../../interfaces'; - -export default function getOutro( - format: string, - name: string, - options: CompileOptions, - imports: Node[] -) { - if (format === 'es') { - return `export default ${name};`; - } - - if (format === 'amd') { - return `return ${name};\n\n});`; - } - - if (format === 'cjs') { - return `module.exports = ${name};`; - } - - if (format === 'iife') { - const globals = getGlobals(imports, options); - return `return ${name};\n\n}(${globals.join(', ')}));`; - } - - if (format === 'eval') { - const globals = getGlobals(imports, options); - return `return ${name};\n\n}(${globals.join(', ')}));`; - } - - if (format === 'umd') { - return `return ${name};\n\n})));`; - } - - throw new Error(`Not implemented: ${format}`); -} diff --git a/src/generators/shared/utils/wrapModule.ts b/src/generators/shared/utils/wrapModule.ts new file mode 100644 index 0000000000..77fea53446 --- /dev/null +++ b/src/generators/shared/utils/wrapModule.ts @@ -0,0 +1,306 @@ +import deindent from '../../../utils/deindent'; +import { CompileOptions, ModuleFormat, Node } from '../../../interfaces'; + +interface Dependency { + name: string; + statements: string[]; + source: string; +} + +const wrappers = { es, amd, cjs, iife, umd, eval: expr }; + +export default function wrapModule( + code: string, + format: ModuleFormat, + name: string, + options: CompileOptions, + banner: string, + sharedPath: string, + helpers: { name: string, alias: string }[], + imports: Node[], + source: string +): string { + if (format === 'es') return es(code, name, options, banner, sharedPath, helpers, imports, source); + + const dependencies = imports.map((declaration, i) => { + const defaultImport = declaration.specifiers.find( + (x: Node) => + x.type === 'ImportDefaultSpecifier' || + (x.type === 'ImportSpecifier' && x.imported.name === 'default') + ); + + const namespaceImport = declaration.specifiers.find( + (x: Node) => x.type === 'ImportNamespaceSpecifier' + ); + + const namedImports = declaration.specifiers.filter( + (x: Node) => + x.type === 'ImportSpecifier' && x.imported.name !== 'default' + ); + + const name = defaultImport || namespaceImport + ? (defaultImport || namespaceImport).local.name + : `__import${i}`; + + const statements: string[] = []; + + namedImports.forEach((specifier: Node) => { + statements.push( + `var ${specifier.local.name} = ${name}.${specifier.imported.name};` + ); + }); + + if (defaultImport) { + statements.push( + `${name} = (${name} && ${name}.__esModule) ? ${name}["default"] : ${name};` + ); + } + + return { name, statements, source: declaration.source.value }; + }); + + if (format === 'amd') return amd(code, name, options, banner, dependencies); + if (format === 'cjs') return cjs(code, name, options, banner, sharedPath, helpers, dependencies); + if (format === 'iife') return iife(code, name, options, banner, dependencies); + if (format === 'umd') return umd(code, name, options, banner, dependencies); + if (format === 'eval') return expr(code, name, options, banner, dependencies); + + throw new Error(`Not implemented: ${format}`); +} + +function es( + code: string, + name: string, + options: CompileOptions, + banner: string, + sharedPath: string, + helpers: { name: string, alias: string }[], + imports: Node[], + source: string +) { + const importHelpers = helpers && ( + `import { ${helpers.map(h => h.name === h.alias ? h.name : `${h.name} as ${h.alias}`).join(', ')} } from ${JSON.stringify(sharedPath)};` + ); + + const importBlock = imports.length > 0 && ( + imports + .map((declaration: Node) => source.slice(declaration.start, declaration.end)) + .join('\n') + ); + + return deindent` + ${banner} + ${importHelpers} + ${importBlock} + + ${code} + export default ${name};`; +} + +function amd( + code: string, + name: string, + options: CompileOptions, + banner: string, + dependencies: Dependency[] +) { + const sourceString = dependencies.length + ? `[${dependencies.map(d => `"${removeExtension(d.source)}"`).join(', ')}], ` + : ''; + + const id = options.amd && options.amd.id; + + return deindent` + define(${id ? `"${id}", ` : ''}${sourceString}function(${paramString(dependencies)}) { "use strict"; + ${getCompatibilityStatements(dependencies)} + + ${code} + return ${name}; + });`; +} + +function cjs( + code: string, + name: string, + options: CompileOptions, + banner: string, + sharedPath: string, + helpers: { name: string, alias: string }[], + dependencies: Dependency[] +) { + const SHARED = '__shared'; + const helperBlock = helpers && ( + `var ${SHARED} = require(${JSON.stringify(sharedPath)});\n` + + helpers.map(helper => { + return `var ${helper.alias} = ${SHARED}.${helper.name};`; + }).join('\n') + ); + + const requireBlock = dependencies.length > 0 && ( + dependencies + .map(d => `var ${d.name} = require("${d.source}");`) + .join('\n\n') + ); + + return deindent` + ${banner} + "use strict"; + + ${helperBlock} + ${requireBlock} + ${getCompatibilityStatements(dependencies)} + + ${code} + + module.exports = ${name};` +} + +function iife( + code: string, + name: string, + options: CompileOptions, + banner: string, + dependencies: Dependency[] +) { + if (!options.name) { + throw new Error(`Missing required 'name' option for IIFE export`); + } + + const globals = getGlobals(dependencies, options); + + return deindent` + ${banner} + var ${options.name} = (function(${paramString(dependencies)}) { "use strict"; + ${getCompatibilityStatements(dependencies)} + + ${code} + return ${name}; + }(${globals.join(', ')}));`; +} + +function umd( + code: string, + name: string, + options: CompileOptions, + banner: string, + dependencies: Dependency[] +) { + if (!options.name) { + throw new Error(`Missing required 'name' option for UMD export`); + } + + const amdId = options.amd && options.amd.id ? `'${options.amd.id}', ` : ''; + + const amdDeps = dependencies.length + ? `[${dependencies.map(d => `"${removeExtension(d.source)}"`).join(', ')}], ` + : ''; + + const cjsDeps = dependencies + .map(d => `require("${d.source}")`) + .join(', '); + + const globals = getGlobals(dependencies, options); + + return deindent` + ${banner} + (function(global, factory) { + typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory(${cjsDeps}) : + typeof define === "function" && define.amd ? define(${amdId}${amdDeps}factory) : + (global.${options.name} = factory(${globals})); + }(this, (function (${paramString(dependencies)}) { "use strict"; + + ${getCompatibilityStatements(dependencies)} + + ${code} + + return ${name}; + + })));`; +} + +function expr( + code: string, + name: string, + options: CompileOptions, + banner: string, + dependencies: Dependency[] +) { + const globals = getGlobals(dependencies, options); + + return deindent` + (function (${paramString(dependencies)}) { "use strict"; + ${banner} + + ${getCompatibilityStatements(dependencies)} + + ${code} + + return ${name}; + }(${globals.join(', ')}))`; +} + +function paramString(dependencies: Dependency[]) { + return dependencies.map(dep => dep.name).join(', '); +} + +function removeExtension(file: string) { + const index = file.lastIndexOf('.'); + return ~index ? file.slice(0, index) : file; +} + +function getCompatibilityStatements(dependencies: Dependency[]) { + if (!dependencies.length) return null; + + const statements: string[] = []; + + dependencies.forEach(dependency => { + statements.push(...dependency.statements); + }); + + return statements.join('\n'); +} + +function getGlobals(dependencies: Dependency[], options: CompileOptions) { + const { globals, onerror, onwarn } = options; + const globalFn = getGlobalFn(globals); + + return dependencies.map(d => { + let name = globalFn(d.source); + + if (!name) { + if (d.name.startsWith('__import')) { + const error = new Error( + `Could not determine name for imported module '${d.source}' – use options.globals` + ); + if (onerror) { + onerror(error); + } else { + throw error; + } + } else { + const warning = { + message: `No name was supplied for imported module '${d.source}'. Guessing '${d.name}', but you should use options.globals`, + }; + + if (onwarn) { + onwarn(warning); + } else { + console.warn(warning); // eslint-disable-line no-console + } + } + + name = d.name; + } + + return name; + }); +} + +function getGlobalFn(globals: any): (id: string) => string { + if (typeof globals === 'function') return globals; + if (typeof globals === 'object') { + return id => globals[id]; + } + + return () => undefined; +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index f2a2af1483..475cfcaceb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -61,7 +61,7 @@ export function create(source: string, _options: CompileOptions = {}) { } try { - return new Function('return ' + compiled.code)(); + return (0,eval)(compiled.code); } catch (err) { if (_options.onerror) { _options.onerror(err); diff --git a/src/interfaces.ts b/src/interfaces.ts index 93d1b608f2..2d876c6a9a 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -64,6 +64,9 @@ export interface CompileOptions { export interface GenerateOptions { name: string; format: ModuleFormat; + banner?: string; + sharedPath?: string | boolean; + helpers?: { name: string, alias: string }[]; } export interface Visitor { diff --git a/test/js/samples/collapses-text-around-comments/expected-bundle.js b/test/js/samples/collapses-text-around-comments/expected-bundle.js index dcc47255b3..5d44708a27 100644 --- a/test/js/samples/collapses-text-around-comments/expected-bundle.js +++ b/test/js/samples/collapses-text-around-comments/expected-bundle.js @@ -190,7 +190,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { data: function () { diff --git a/test/js/samples/collapses-text-around-comments/expected.js b/test/js/samples/collapses-text-around-comments/expected.js index caa1bc886b..83841b4eb8 100644 --- a/test/js/samples/collapses-text-around-comments/expected.js +++ b/test/js/samples/collapses-text-around-comments/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { appendNode, assign, createElement, createText, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js"; var template = (function() { @@ -69,5 +68,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/component-static/expected-bundle.js b/test/js/samples/component-static/expected-bundle.js index 2dbef4a1b1..a9769110cb 100644 --- a/test/js/samples/component-static/expected-bundle.js +++ b/test/js/samples/component-static/expected-bundle.js @@ -166,7 +166,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { components: { diff --git a/test/js/samples/component-static/expected.js b/test/js/samples/component-static/expected.js index e6f5d12eb7..8a22292037 100644 --- a/test/js/samples/component-static/expected.js +++ b/test/js/samples/component-static/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, callAll, init, noop, proto } from "svelte/shared.js"; var template = (function() { @@ -63,5 +62,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/computed-collapsed-if/expected-bundle.js b/test/js/samples/computed-collapsed-if/expected-bundle.js index 752455d407..e6e9152b1e 100644 --- a/test/js/samples/computed-collapsed-if/expected-bundle.js +++ b/test/js/samples/computed-collapsed-if/expected-bundle.js @@ -166,7 +166,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { computed: { diff --git a/test/js/samples/computed-collapsed-if/expected.js b/test/js/samples/computed-collapsed-if/expected.js index 097e5557c8..7b9207a955 100644 --- a/test/js/samples/computed-collapsed-if/expected.js +++ b/test/js/samples/computed-collapsed-if/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, differs, init, noop, proto } from "svelte/shared.js"; var template = (function() { @@ -47,5 +46,4 @@ SvelteComponent.prototype._recompute = function _recompute(changed, state) { if (differs(state.b, (state.b = template.computed.b(state.x)))) changed.b = true; } } - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/css-media-query/expected-bundle.js b/test/js/samples/css-media-query/expected-bundle.js index 8268700417..e37322f9e6 100644 --- a/test/js/samples/css-media-query/expected-bundle.js +++ b/test/js/samples/css-media-query/expected-bundle.js @@ -186,7 +186,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function encapsulateStyles(node) { setAttribute(node, "svelte-2363328337", ""); } diff --git a/test/js/samples/css-media-query/expected.js b/test/js/samples/css-media-query/expected.js index a87dfb878d..58d4b5e991 100644 --- a/test/js/samples/css-media-query/expected.js +++ b/test/js/samples/css-media-query/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { appendNode, assign, createElement, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js"; function encapsulateStyles(node) { @@ -55,5 +54,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js b/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js index 4761ffe62c..22bcb5f37b 100644 --- a/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js +++ b/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js @@ -186,7 +186,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div, text; diff --git a/test/js/samples/css-shadow-dom-keyframes/expected.js b/test/js/samples/css-shadow-dom-keyframes/expected.js index 4268c29877..6cea9a7856 100644 --- a/test/js/samples/css-shadow-dom-keyframes/expected.js +++ b/test/js/samples/css-shadow-dom-keyframes/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { appendNode, assign, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -62,5 +61,4 @@ assign(SvelteComponent.prototype, proto, { this.parentNode.removeChild(this); } }); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/each-block-changed-check/expected-bundle.js b/test/js/samples/each-block-changed-check/expected-bundle.js index fdb4100a92..882e55dfbb 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -198,7 +198,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var text, p, text_1; diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js index d7bc0e680b..613fea0bf5 100644 --- a/test/js/samples/each-block-changed-check/expected.js +++ b/test/js/samples/each-block-changed-check/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { appendNode, assign, createElement, createText, destroyEach, detachAfter, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -153,5 +152,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/event-handlers-custom/expected-bundle.js b/test/js/samples/event-handlers-custom/expected-bundle.js index 028e471852..4d5e595318 100644 --- a/test/js/samples/event-handlers-custom/expected-bundle.js +++ b/test/js/samples/event-handlers-custom/expected-bundle.js @@ -186,7 +186,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { methods: { diff --git a/test/js/samples/event-handlers-custom/expected.js b/test/js/samples/event-handlers-custom/expected.js index 1891835aa1..bf656c0411 100644 --- a/test/js/samples/event-handlers-custom/expected.js +++ b/test/js/samples/event-handlers-custom/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { appendNode, assign, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; var template = (function() { @@ -64,5 +63,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, template.methods, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/if-block-no-update/expected-bundle.js b/test/js/samples/if-block-no-update/expected-bundle.js index d68a947cdb..b413e274e0 100644 --- a/test/js/samples/if-block-no-update/expected-bundle.js +++ b/test/js/samples/if-block-no-update/expected-bundle.js @@ -190,7 +190,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var if_block_anchor; diff --git a/test/js/samples/if-block-no-update/expected.js b/test/js/samples/if-block-no-update/expected.js index 38600d30d4..a513519061 100644 --- a/test/js/samples/if-block-no-update/expected.js +++ b/test/js/samples/if-block-no-update/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { appendNode, assign, createComment, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -104,5 +103,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/if-block-simple/expected-bundle.js b/test/js/samples/if-block-simple/expected-bundle.js index 45ddba0cff..881aafd926 100644 --- a/test/js/samples/if-block-simple/expected-bundle.js +++ b/test/js/samples/if-block-simple/expected-bundle.js @@ -190,7 +190,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var if_block_anchor; diff --git a/test/js/samples/if-block-simple/expected.js b/test/js/samples/if-block-simple/expected.js index 81a5ffc975..aba8b65ee7 100644 --- a/test/js/samples/if-block-simple/expected.js +++ b/test/js/samples/if-block-simple/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { appendNode, assign, createComment, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -79,5 +78,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js index 3f794ba7ac..745f0a2f26 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js @@ -182,7 +182,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div; diff --git a/test/js/samples/inline-style-optimized-multiple/expected.js b/test/js/samples/inline-style-optimized-multiple/expected.js index 45f2a50c2a..35de49a17c 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected.js +++ b/test/js/samples/inline-style-optimized-multiple/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -51,5 +50,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized-url/expected-bundle.js b/test/js/samples/inline-style-optimized-url/expected-bundle.js index 18205758cc..2985f7cb15 100644 --- a/test/js/samples/inline-style-optimized-url/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-url/expected-bundle.js @@ -182,7 +182,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div; diff --git a/test/js/samples/inline-style-optimized-url/expected.js b/test/js/samples/inline-style-optimized-url/expected.js index 9452de7328..659bc4b20b 100644 --- a/test/js/samples/inline-style-optimized-url/expected.js +++ b/test/js/samples/inline-style-optimized-url/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -46,5 +45,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized/expected-bundle.js b/test/js/samples/inline-style-optimized/expected-bundle.js index db5b7c8a2c..fa4dfc93a9 100644 --- a/test/js/samples/inline-style-optimized/expected-bundle.js +++ b/test/js/samples/inline-style-optimized/expected-bundle.js @@ -182,7 +182,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div; diff --git a/test/js/samples/inline-style-optimized/expected.js b/test/js/samples/inline-style-optimized/expected.js index bc4dc0eeda..8b07a3eaad 100644 --- a/test/js/samples/inline-style-optimized/expected.js +++ b/test/js/samples/inline-style-optimized/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createElement, detachNode, init, insertNode, noop, proto, setStyle } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -46,5 +45,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-unoptimized/expected-bundle.js b/test/js/samples/inline-style-unoptimized/expected-bundle.js index 98235d2576..e0770b55c7 100644 --- a/test/js/samples/inline-style-unoptimized/expected-bundle.js +++ b/test/js/samples/inline-style-unoptimized/expected-bundle.js @@ -182,7 +182,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div, text, div_1, div_1_style_value; diff --git a/test/js/samples/inline-style-unoptimized/expected.js b/test/js/samples/inline-style-unoptimized/expected.js index c15408b3be..9dcb2296bb 100644 --- a/test/js/samples/inline-style-unoptimized/expected.js +++ b/test/js/samples/inline-style-unoptimized/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -57,5 +56,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/input-without-blowback-guard/expected-bundle.js b/test/js/samples/input-without-blowback-guard/expected-bundle.js index 948cd943b1..8a0c36accc 100644 --- a/test/js/samples/input-without-blowback-guard/expected-bundle.js +++ b/test/js/samples/input-without-blowback-guard/expected-bundle.js @@ -186,7 +186,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var input; diff --git a/test/js/samples/input-without-blowback-guard/expected.js b/test/js/samples/input-without-blowback-guard/expected.js index 86e92e2fc4..074a6126eb 100644 --- a/test/js/samples/input-without-blowback-guard/expected.js +++ b/test/js/samples/input-without-blowback-guard/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { addListener, assign, createElement, detachNode, init, insertNode, proto, removeListener } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -53,5 +52,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/legacy-input-type/expected-bundle.js b/test/js/samples/legacy-input-type/expected-bundle.js index 036c77f3c8..6475e22fa8 100644 --- a/test/js/samples/legacy-input-type/expected-bundle.js +++ b/test/js/samples/legacy-input-type/expected-bundle.js @@ -184,7 +184,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var input; diff --git a/test/js/samples/legacy-input-type/expected.js b/test/js/samples/legacy-input-type/expected.js index 9a4d4782cd..c7785c6ed4 100644 --- a/test/js/samples/legacy-input-type/expected.js +++ b/test/js/samples/legacy-input-type/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, createElement, detachNode, init, insertNode, noop, proto, setInputType } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -42,5 +41,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/legacy-quote-class/expected-bundle.js b/test/js/samples/legacy-quote-class/expected-bundle.js index 552f1c22bc..139958519b 100644 --- a/test/js/samples/legacy-quote-class/expected-bundle.js +++ b/test/js/samples/legacy-quote-class/expected-bundle.js @@ -201,7 +201,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div; diff --git a/test/js/samples/legacy-quote-class/expected.js b/test/js/samples/legacy-quote-class/expected.js index 93121a357a..dd10330458 100644 --- a/test/js/samples/legacy-quote-class/expected.js +++ b/test/js/samples/legacy-quote-class/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, children, claimElement, createElement, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -52,5 +51,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/media-bindings/expected-bundle.js b/test/js/samples/media-bindings/expected-bundle.js index 7ed611bf35..d784e6fabe 100644 --- a/test/js/samples/media-bindings/expected-bundle.js +++ b/test/js/samples/media-bindings/expected-bundle.js @@ -194,7 +194,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var audio, audio_updating = false, audio_animationframe, audio_paused_value = true; diff --git a/test/js/samples/media-bindings/expected.js b/test/js/samples/media-bindings/expected.js index 5e67ebfb1f..531a2a1b58 100644 --- a/test/js/samples/media-bindings/expected.js +++ b/test/js/samples/media-bindings/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { addListener, assign, callAll, createElement, detachNode, init, insertNode, proto, removeListener, timeRangesToArray } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -128,5 +127,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/non-imported-component/expected-bundle.js b/test/js/samples/non-imported-component/expected-bundle.js index 817ab2c4f8..2252eb1635 100644 --- a/test/js/samples/non-imported-component/expected-bundle.js +++ b/test/js/samples/non-imported-component/expected-bundle.js @@ -180,7 +180,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { components: { diff --git a/test/js/samples/non-imported-component/expected.js b/test/js/samples/non-imported-component/expected.js index b2f77857c4..e424bb6ffd 100644 --- a/test/js/samples/non-imported-component/expected.js +++ b/test/js/samples/non-imported-component/expected.js @@ -1,8 +1,6 @@ -import Imported from 'Imported.html'; - /* generated by Svelte vX.Y.Z */ - import { assign, callAll, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; +import Imported from 'Imported.html'; var template = (function() { return { @@ -76,5 +74,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js index 2bec66e3e1..bebd336b6c 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js @@ -166,7 +166,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { // this test should be removed in v2 diff --git a/test/js/samples/onrender-onteardown-rewritten/expected.js b/test/js/samples/onrender-onteardown-rewritten/expected.js index 6eb6b55a1e..d29f9fd752 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, callAll, init, noop, proto } from "svelte/shared.js"; var template = (function() { @@ -50,5 +49,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/setup-method/expected-bundle.js b/test/js/samples/setup-method/expected-bundle.js index 0752ea7839..85bc643281 100644 --- a/test/js/samples/setup-method/expected-bundle.js +++ b/test/js/samples/setup-method/expected-bundle.js @@ -166,7 +166,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - var template = (function() { return { methods: { diff --git a/test/js/samples/setup-method/expected.js b/test/js/samples/setup-method/expected.js index dcf0b20e8d..b3bd9fcc8e 100644 --- a/test/js/samples/setup-method/expected.js +++ b/test/js/samples/setup-method/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { assign, init, noop, proto } from "svelte/shared.js"; var template = (function() { @@ -51,5 +50,4 @@ function SvelteComponent(options) { assign(SvelteComponent.prototype, template.methods, proto); template.setup(SvelteComponent); - export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/use-elements-as-anchors/expected-bundle.js b/test/js/samples/use-elements-as-anchors/expected-bundle.js index 5a9b918376..977bdfe485 100644 --- a/test/js/samples/use-elements-as-anchors/expected-bundle.js +++ b/test/js/samples/use-elements-as-anchors/expected-bundle.js @@ -190,7 +190,6 @@ var proto = { }; /* generated by Svelte vX.Y.Z */ - function create_main_fragment(state, component) { var div, text, p, text_1, text_2, text_3, text_4, p_1, text_5, text_6, text_8, if_block_4_anchor; diff --git a/test/js/samples/use-elements-as-anchors/expected.js b/test/js/samples/use-elements-as-anchors/expected.js index 01d21caa1e..e92d7ba6b6 100644 --- a/test/js/samples/use-elements-as-anchors/expected.js +++ b/test/js/samples/use-elements-as-anchors/expected.js @@ -1,5 +1,4 @@ /* generated by Svelte vX.Y.Z */ - import { appendNode, assign, createComment, createElement, createText, detachNode, init, insertNode, noop, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { @@ -267,5 +266,4 @@ function SvelteComponent(options) { } assign(SvelteComponent.prototype, proto); - export default SvelteComponent; \ No newline at end of file diff --git a/test/runtime/index.js b/test/runtime/index.js index a31f9531f8..122fe645b6 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("export default"); 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)) { diff --git a/test/sourcemaps/samples/each-block/test.js b/test/sourcemaps/samples/each-block/test.js index add51d628b..d31c565ced 100644 --- a/test/sourcemaps/samples/each-block/test.js +++ b/test/sourcemaps/samples/each-block/test.js @@ -1,7 +1,8 @@ -export function test ({ assert, smc, locateInSource, locateInGenerated }) { - const expected = locateInSource( 'each' ); +export function test({ assert, code, smc, locateInSource, locateInGenerated }) { + const startIndex = code.indexOf('create_main_fragment'); - const loc = locateInGenerated( 'length' ); + const expected = locateInSource('each'); + const loc = locateInGenerated('length', startIndex ); const actual = smc.originalPositionFor({ line: loc.line + 1,