rewrite imports from svelte when testing

pull/1839/head
Rich Harris 7 years ago
parent 72b9254436
commit 4e5fd7fdfd

@ -225,7 +225,7 @@ export default class Component {
name, name,
options, options,
banner, banner,
options.sveltePath ? `${options.sveltePath}/internal.js` : `svelte/internal.js`, options.sveltePath,
importedHelpers, importedHelpers,
this.imports, this.imports,
this.module_exports, this.module_exports,

@ -21,17 +21,19 @@ export default function wrapModule(
name: string, name: string,
options: CompileOptions, options: CompileOptions,
banner: string, banner: string,
sharedPath: string, sveltePath = 'svelte',
helpers: { name: string, alias: string }[], helpers: { name: string, alias: string }[],
imports: Node[], imports: Node[],
module_exports: Export[], module_exports: Export[],
source: string source: string
): string { ): string {
const internalPath = `${sveltePath}/internal.js`;
if (format === 'esm') { if (format === 'esm') {
return esm(code, name, options, banner, sharedPath, helpers, imports, module_exports, source); return esm(code, name, options, banner, sveltePath, internalPath, helpers, imports, module_exports, source);
} }
if (format === 'cjs') return cjs(code, name, banner, sharedPath, helpers, imports, module_exports); if (format === 'cjs') return cjs(code, name, banner, sveltePath, internalPath, helpers, imports, module_exports);
if (format === 'eval') return expr(code, name, options, banner, imports); if (format === 'eval') return expr(code, name, options, banner, imports);
throw new Error(`options.format is invalid (must be ${list(Object.keys(wrappers))})`); throw new Error(`options.format is invalid (must be ${list(Object.keys(wrappers))})`);
@ -42,19 +44,28 @@ function esm(
name: string, name: string,
options: CompileOptions, options: CompileOptions,
banner: string, banner: string,
sharedPath: string, sveltePath: string,
internalPath: string,
helpers: { name: string, alias: string }[], helpers: { name: string, alias: string }[],
imports: Node[], imports: Node[],
module_exports: Export[], module_exports: Export[],
source: string source: string
) { ) {
const importHelpers = helpers.length > 0 && ( const importHelpers = helpers.length > 0 && (
`import { ${helpers.map(h => h.name === h.alias ? h.name : `${h.name} as ${h.alias}`).join(', ')} } from ${JSON.stringify(sharedPath)};` `import { ${helpers.map(h => h.name === h.alias ? h.name : `${h.name} as ${h.alias}`).join(', ')} } from ${JSON.stringify(internalPath)};`
); );
const importBlock = imports.length > 0 && ( const importBlock = imports.length > 0 && (
imports imports
.map((declaration: Node) => source.slice(declaration.start, declaration.end)) .map((declaration: Node) => {
const import_source = declaration.source.value === 'svelte' ? sveltePath : declaration.source.value;
return (
source.slice(declaration.start, declaration.source.start) +
JSON.stringify(import_source) +
source.slice(declaration.source.end, declaration.end)
);
})
.join('\n') .join('\n')
); );
@ -72,7 +83,8 @@ function cjs(
code: string, code: string,
name: string, name: string,
banner: string, banner: string,
sharedPath: string, sveltePath: string,
internalPath: string,
helpers: { name: string, alias: string }[], helpers: { name: string, alias: string }[],
imports: Node[], imports: Node[],
module_exports: Export[] module_exports: Export[]
@ -80,7 +92,7 @@ function cjs(
const helperDeclarations = helpers.map(h => `${h.alias === h.name ? h.name : `${h.name}: ${h.alias}`}`).join(', '); const helperDeclarations = helpers.map(h => `${h.alias === h.name ? h.name : `${h.name}: ${h.alias}`}`).join(', ');
const helperBlock = helpers.length > 0 && ( const helperBlock = helpers.length > 0 && (
`const { ${helperDeclarations} } = require(${JSON.stringify(sharedPath)});\n` `const { ${helperDeclarations} } = require(${JSON.stringify(internalPath)});\n`
); );
const requires = imports.map(node => { const requires = imports.map(node => {
@ -102,7 +114,11 @@ function cjs(
lhs = `{ ${properties.join(', ')} }`; lhs = `{ ${properties.join(', ')} }`;
} }
return `const ${lhs} = require("${node.source.value}");` const source = node.source.value === 'svelte'
? sveltePath
: node.source.value;
return `const ${lhs} = require("${source}");`
}); });
const exports = [`exports.default = ${name};`].concat( const exports = [`exports.default = ${name};`].concat(

@ -21,7 +21,7 @@ function tryToReadFile(file) {
const sveltePath = process.cwd(); const sveltePath = process.cwd();
describe("ssr", () => { describe.only("ssr", () => {
before(() => { before(() => {
require("../../register")({ require("../../register")({
extensions: ['.svelte', '.html'], extensions: ['.svelte', '.html'],

Loading…
Cancel
Save