|
|
@ -1,18 +1,20 @@
|
|
|
|
import deindent from '../../src/utils/deindent.js';
|
|
|
|
import deindent from "../../src/utils/deindent.js";
|
|
|
|
import assert from 'assert';
|
|
|
|
import assert from "assert";
|
|
|
|
import { svelte, env, setupHtmlEqual } from '../helpers.js';
|
|
|
|
import { svelte, env, setupHtmlEqual } from "../helpers.js";
|
|
|
|
|
|
|
|
|
|
|
|
function testAmd(code, expectedId, dependencies, html) {
|
|
|
|
function testAmd(code, expectedId, dependencies, html) {
|
|
|
|
const fn = new Function( 'define', code );
|
|
|
|
const fn = new Function("define", code);
|
|
|
|
|
|
|
|
|
|
|
|
return env().then(window => {
|
|
|
|
return env().then(window => {
|
|
|
|
function define(id, deps, factory) {
|
|
|
|
function define(id, deps, factory) {
|
|
|
|
assert.equal(id, expectedId);
|
|
|
|
assert.equal(id, expectedId);
|
|
|
|
assert.deepEqual(deps, Object.keys(dependencies));
|
|
|
|
assert.deepEqual(deps, Object.keys(dependencies));
|
|
|
|
|
|
|
|
|
|
|
|
const SvelteComponent = factory( ...Object.keys( dependencies ).map( key => dependencies[ key ] ) );
|
|
|
|
const SvelteComponent = factory(
|
|
|
|
|
|
|
|
...Object.keys(dependencies).map(key => dependencies[key])
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const main = window.document.body.querySelector( 'main' );
|
|
|
|
const main = window.document.body.querySelector("main");
|
|
|
|
const component = new SvelteComponent({ target: main });
|
|
|
|
const component = new SvelteComponent({ target: main });
|
|
|
|
|
|
|
|
|
|
|
|
assert.htmlEqual(main.innerHTML, html);
|
|
|
|
assert.htmlEqual(main.innerHTML, html);
|
|
|
@ -27,7 +29,7 @@ function testAmd ( code, expectedId, dependencies, html ) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testCjs(code, dependencyById, html) {
|
|
|
|
function testCjs(code, dependencyById, html) {
|
|
|
|
const fn = new Function( 'module', 'exports', 'require', code );
|
|
|
|
const fn = new Function("module", "exports", "require", code);
|
|
|
|
|
|
|
|
|
|
|
|
return env().then(window => {
|
|
|
|
return env().then(window => {
|
|
|
|
const module = { exports: {} };
|
|
|
|
const module = { exports: {} };
|
|
|
@ -39,7 +41,7 @@ function testCjs ( code, dependencyById, html ) {
|
|
|
|
|
|
|
|
|
|
|
|
const SvelteComponent = module.exports;
|
|
|
|
const SvelteComponent = module.exports;
|
|
|
|
|
|
|
|
|
|
|
|
const main = window.document.body.querySelector( 'main' );
|
|
|
|
const main = window.document.body.querySelector("main");
|
|
|
|
const component = new SvelteComponent({ target: main });
|
|
|
|
const component = new SvelteComponent({ target: main });
|
|
|
|
|
|
|
|
|
|
|
|
assert.htmlEqual(main.innerHTML, html);
|
|
|
|
assert.htmlEqual(main.innerHTML, html);
|
|
|
@ -52,9 +54,11 @@ function testIife ( code, name, globals, html ) {
|
|
|
|
const fn = new Function(Object.keys(globals), `${code}\n\nreturn ${name};`);
|
|
|
|
const fn = new Function(Object.keys(globals), `${code}\n\nreturn ${name};`);
|
|
|
|
|
|
|
|
|
|
|
|
return env().then(window => {
|
|
|
|
return env().then(window => {
|
|
|
|
const SvelteComponent = fn( ...Object.keys( globals ).map( key => globals[ key ] ) );
|
|
|
|
const SvelteComponent = fn(
|
|
|
|
|
|
|
|
...Object.keys(globals).map(key => globals[key])
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const main = window.document.body.querySelector( 'main' );
|
|
|
|
const main = window.document.body.querySelector("main");
|
|
|
|
const component = new SvelteComponent({ target: main });
|
|
|
|
const component = new SvelteComponent({ target: main });
|
|
|
|
|
|
|
|
|
|
|
|
assert.htmlEqual(main.innerHTML, html);
|
|
|
|
assert.htmlEqual(main.innerHTML, html);
|
|
|
@ -67,9 +71,11 @@ function testEval ( code, name, globals, html ) {
|
|
|
|
const fn = new Function(Object.keys(globals), `return ${code};`);
|
|
|
|
const fn = new Function(Object.keys(globals), `return ${code};`);
|
|
|
|
|
|
|
|
|
|
|
|
return env().then(window => {
|
|
|
|
return env().then(window => {
|
|
|
|
const SvelteComponent = fn( ...Object.keys( globals ).map( key => globals[ key ] ) );
|
|
|
|
const SvelteComponent = fn(
|
|
|
|
|
|
|
|
...Object.keys(globals).map(key => globals[key])
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const main = window.document.body.querySelector( 'main' );
|
|
|
|
const main = window.document.body.querySelector("main");
|
|
|
|
const component = new SvelteComponent({ target: main });
|
|
|
|
const component = new SvelteComponent({ target: main });
|
|
|
|
|
|
|
|
|
|
|
|
assert.htmlEqual(main.innerHTML, html);
|
|
|
|
assert.htmlEqual(main.innerHTML, html);
|
|
|
@ -78,11 +84,11 @@ function testEval ( code, name, globals, html ) {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'formats', () => {
|
|
|
|
describe("formats", () => {
|
|
|
|
before(setupHtmlEqual);
|
|
|
|
before(setupHtmlEqual);
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'amd', () => {
|
|
|
|
describe("amd", () => {
|
|
|
|
it( 'generates an AMD module', () => {
|
|
|
|
it("generates an AMD module", () => {
|
|
|
|
const source = deindent`
|
|
|
|
const source = deindent`
|
|
|
|
<div>{{answer}}</div>
|
|
|
|
<div>{{answer}}</div>
|
|
|
|
|
|
|
|
|
|
|
@ -98,16 +104,16 @@ describe( 'formats', () => {
|
|
|
|
`;
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const { code } = svelte.compile(source, {
|
|
|
|
const { code } = svelte.compile(source, {
|
|
|
|
format: 'amd',
|
|
|
|
format: "amd",
|
|
|
|
amd: { id: 'foo' }
|
|
|
|
amd: { id: "foo" }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return testAmd( code, 'foo', { answer: 42 }, `<div>42</div>` );
|
|
|
|
return testAmd(code, "foo", { answer: 42 }, `<div>42</div>`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'cjs', () => {
|
|
|
|
describe("cjs", () => {
|
|
|
|
it( 'generates a CommonJS module', () => {
|
|
|
|
it("generates a CommonJS module", () => {
|
|
|
|
const source = deindent`
|
|
|
|
const source = deindent`
|
|
|
|
<div>{{answer}}</div>
|
|
|
|
<div>{{answer}}</div>
|
|
|
|
|
|
|
|
|
|
|
@ -123,15 +129,15 @@ describe( 'formats', () => {
|
|
|
|
`;
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const { code } = svelte.compile(source, {
|
|
|
|
const { code } = svelte.compile(source, {
|
|
|
|
format: 'cjs'
|
|
|
|
format: "cjs"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return testCjs(code, { answer: 42 }, `<div>42</div>`);
|
|
|
|
return testCjs(code, { answer: 42 }, `<div>42</div>`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'iife', () => {
|
|
|
|
describe("iife", () => {
|
|
|
|
it( 'generates a self-executing script', () => {
|
|
|
|
it("generates a self-executing script", () => {
|
|
|
|
const source = deindent`
|
|
|
|
const source = deindent`
|
|
|
|
<div>{{answer}}</div>
|
|
|
|
<div>{{answer}}</div>
|
|
|
|
|
|
|
|
|
|
|
@ -147,19 +153,19 @@ describe( 'formats', () => {
|
|
|
|
`;
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const { code } = svelte.compile(source, {
|
|
|
|
const { code } = svelte.compile(source, {
|
|
|
|
format: 'iife',
|
|
|
|
format: "iife",
|
|
|
|
name: 'Foo',
|
|
|
|
name: "Foo",
|
|
|
|
globals: {
|
|
|
|
globals: {
|
|
|
|
answer: 'answer'
|
|
|
|
answer: "answer"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return testIife( code, 'Foo', { answer: 42 }, `<div>42</div>` );
|
|
|
|
return testIife(code, "Foo", { answer: 42 }, `<div>42</div>`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'umd', () => {
|
|
|
|
describe("umd", () => {
|
|
|
|
it( 'generates a UMD build', () => {
|
|
|
|
it("generates a UMD build", () => {
|
|
|
|
const source = deindent`
|
|
|
|
const source = deindent`
|
|
|
|
<div>{{answer}}</div>
|
|
|
|
<div>{{answer}}</div>
|
|
|
|
|
|
|
|
|
|
|
@ -175,24 +181,24 @@ describe( 'formats', () => {
|
|
|
|
`;
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const { code } = svelte.compile(source, {
|
|
|
|
const { code } = svelte.compile(source, {
|
|
|
|
format: 'umd',
|
|
|
|
format: "umd",
|
|
|
|
name: 'Foo',
|
|
|
|
name: "Foo",
|
|
|
|
globals: {
|
|
|
|
globals: {
|
|
|
|
answer: 'answer'
|
|
|
|
answer: "answer"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
amd: {
|
|
|
|
amd: {
|
|
|
|
id: 'foo'
|
|
|
|
id: "foo"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return testAmd( code, 'foo', { answer: 42 }, `<div>42</div>` )
|
|
|
|
return testAmd(code, "foo", { answer: 42 }, `<div>42</div>`)
|
|
|
|
.then(() => testCjs(code, { answer: 42 }, `<div>42</div>`))
|
|
|
|
.then(() => testCjs(code, { answer: 42 }, `<div>42</div>`))
|
|
|
|
.then( () => testIife( code, 'Foo', { answer: 42 }, `<div>42</div>` ) );
|
|
|
|
.then(() => testIife(code, "Foo", { answer: 42 }, `<div>42</div>`));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'eval', () => {
|
|
|
|
describe("eval", () => {
|
|
|
|
it( 'generates a self-executing script that returns the component on eval', () => {
|
|
|
|
it("generates a self-executing script that returns the component on eval", () => {
|
|
|
|
const source = deindent`
|
|
|
|
const source = deindent`
|
|
|
|
<div>{{answer}}</div>
|
|
|
|
<div>{{answer}}</div>
|
|
|
|
|
|
|
|
|
|
|
@ -208,13 +214,13 @@ describe( 'formats', () => {
|
|
|
|
`;
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const { code } = svelte.compile(source, {
|
|
|
|
const { code } = svelte.compile(source, {
|
|
|
|
format: 'eval',
|
|
|
|
format: "eval",
|
|
|
|
globals: {
|
|
|
|
globals: {
|
|
|
|
answer: 'answer'
|
|
|
|
answer: "answer"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return testEval( code, 'Foo', { answer: 42 }, `<div>42</div>` );
|
|
|
|
return testEval(code, "Foo", { answer: 42 }, `<div>42</div>`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|