|
|
|
@ -1,12 +1,18 @@
|
|
|
|
|
import spaces from '../../src/utils/spaces.js';
|
|
|
|
|
import assert from 'assert';
|
|
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import * as acorn from 'acorn';
|
|
|
|
|
import * as babel from 'babel-core';
|
|
|
|
|
import { transitionManager } from '../../shared.js';
|
|
|
|
|
|
|
|
|
|
import { addLineNumbers, loadConfig, loadSvelte, env, setupHtmlEqual } from '../helpers.js';
|
|
|
|
|
import spaces from "../../src/utils/spaces.js";
|
|
|
|
|
import assert from "assert";
|
|
|
|
|
import * as path from "path";
|
|
|
|
|
import * as fs from "fs";
|
|
|
|
|
import * as acorn from "acorn";
|
|
|
|
|
import * as babel from "babel-core";
|
|
|
|
|
import { transitionManager } from "../../shared.js";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
addLineNumbers,
|
|
|
|
|
loadConfig,
|
|
|
|
|
loadSvelte,
|
|
|
|
|
env,
|
|
|
|
|
setupHtmlEqual
|
|
|
|
|
} from "../helpers.js";
|
|
|
|
|
|
|
|
|
|
let svelte;
|
|
|
|
|
|
|
|
|
@ -14,17 +20,20 @@ let showCompiledCode = false;
|
|
|
|
|
let compileOptions = null;
|
|
|
|
|
|
|
|
|
|
function getName(filename) {
|
|
|
|
|
const base = path.basename( filename ).replace( '.html', '' );
|
|
|
|
|
const base = path.basename(filename).replace(".html", "");
|
|
|
|
|
return base[0].toUpperCase() + base.slice(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nodeVersionMatch = /^v(\d)/.exec(process.version);
|
|
|
|
|
const legacy = +nodeVersionMatch[1] < 6;
|
|
|
|
|
const babelrc = require( '../../package.json' ).babel;
|
|
|
|
|
const babelrc = require("../../package.json").babel;
|
|
|
|
|
|
|
|
|
|
require.extensions[ '.html' ] = function ( module, filename ) {
|
|
|
|
|
const options = Object.assign({ filename, name: getName( filename ) }, compileOptions );
|
|
|
|
|
let { code } = svelte.compile( fs.readFileSync( filename, 'utf-8' ), options );
|
|
|
|
|
require.extensions[".html"] = function(module, filename) {
|
|
|
|
|
const options = Object.assign(
|
|
|
|
|
{ filename, name: getName(filename) },
|
|
|
|
|
compileOptions
|
|
|
|
|
);
|
|
|
|
|
let { code } = svelte.compile(fs.readFileSync(filename, "utf-8"), options);
|
|
|
|
|
|
|
|
|
|
if (showCompiledCode) console.log(addLineNumbers(code)); // eslint-disable-line no-console
|
|
|
|
|
|
|
|
|
@ -35,19 +44,19 @@ require.extensions[ '.html' ] = function ( module, filename ) {
|
|
|
|
|
|
|
|
|
|
const Object_assign = Object.assign;
|
|
|
|
|
|
|
|
|
|
describe( 'runtime', () => {
|
|
|
|
|
describe("runtime", () => {
|
|
|
|
|
before(() => {
|
|
|
|
|
svelte = loadSvelte(true);
|
|
|
|
|
return setupHtmlEqual();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function runTest(dir, shared) {
|
|
|
|
|
if ( dir[0] === '.' ) return;
|
|
|
|
|
if (dir[0] === ".") return;
|
|
|
|
|
|
|
|
|
|
const config = loadConfig(`./runtime/samples/${dir}/_config.js`);
|
|
|
|
|
|
|
|
|
|
if (config.solo && process.env.CI) {
|
|
|
|
|
throw new Error( 'Forgot to remove `solo: true` from test' );
|
|
|
|
|
throw new Error("Forgot to remove `solo: true` from test");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(config.skip ? it.skip : config.solo ? it.only : it)(dir, () => {
|
|
|
|
@ -59,7 +68,10 @@ describe( 'runtime', () => {
|
|
|
|
|
compileOptions.dev = config.dev;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const source = fs.readFileSync( `test/runtime/samples/${dir}/main.html`, 'utf-8' );
|
|
|
|
|
const source = fs.readFileSync(
|
|
|
|
|
`test/runtime/samples/${dir}/main.html`,
|
|
|
|
|
"utf-8"
|
|
|
|
|
);
|
|
|
|
|
compiled = svelte.compile(source, compileOptions);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (config.compileError) {
|
|
|
|
@ -75,9 +87,12 @@ describe( 'runtime', () => {
|
|
|
|
|
// check that no ES2015+ syntax slipped in
|
|
|
|
|
if (!config.allowES2015) {
|
|
|
|
|
try {
|
|
|
|
|
const startIndex = code.indexOf( 'function create_main_fragment' ); // may change!
|
|
|
|
|
if ( startIndex === -1 ) throw new Error( 'missing create_main_fragment' );
|
|
|
|
|
const es5 = spaces( startIndex ) + code.slice( startIndex ).replace( /export default .+/, '' );
|
|
|
|
|
const startIndex = code.indexOf("function create_main_fragment"); // may change!
|
|
|
|
|
if (startIndex === -1)
|
|
|
|
|
throw new Error("missing create_main_fragment");
|
|
|
|
|
const es5 =
|
|
|
|
|
spaces(startIndex) +
|
|
|
|
|
code.slice(startIndex).replace(/export default .+/, "");
|
|
|
|
|
acorn.parse(es5, { ecmaVersion: 5 });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (!config.show) console.log(addLineNumbers(code)); // eslint-disable-line no-console
|
|
|
|
@ -85,7 +100,9 @@ describe( 'runtime', () => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Object.keys( require.cache ).filter( x => x.endsWith( '.html' ) ).forEach( file => {
|
|
|
|
|
Object.keys(require.cache)
|
|
|
|
|
.filter(x => x.endsWith(".html"))
|
|
|
|
|
.forEach(file => {
|
|
|
|
|
delete require.cache[file];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -128,7 +145,9 @@ describe( 'runtime', () => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Object.assign = () => {
|
|
|
|
|
throw new Error( 'cannot use Object.assign in generated code, as it is not supported everywhere' );
|
|
|
|
|
throw new Error(
|
|
|
|
|
"cannot use Object.assign in generated code, as it is not supported everywhere"
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
global.window = window;
|
|
|
|
@ -136,7 +155,7 @@ describe( 'runtime', () => {
|
|
|
|
|
// Put the constructor on window for testing
|
|
|
|
|
window.SvelteComponent = SvelteComponent;
|
|
|
|
|
|
|
|
|
|
const target = window.document.querySelector( 'main' );
|
|
|
|
|
const target = window.document.querySelector("main");
|
|
|
|
|
|
|
|
|
|
const warnings = [];
|
|
|
|
|
const warn = console.warn;
|
|
|
|
@ -155,14 +174,14 @@ describe( 'runtime', () => {
|
|
|
|
|
|
|
|
|
|
if (config.error) {
|
|
|
|
|
unintendedError = true;
|
|
|
|
|
throw new Error( 'Expected a runtime error' );
|
|
|
|
|
throw new Error("Expected a runtime error");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config.warnings) {
|
|
|
|
|
assert.deepEqual(warnings, config.warnings);
|
|
|
|
|
} else if (warnings.length) {
|
|
|
|
|
unintendedError = true;
|
|
|
|
|
throw new Error( 'Received unexpected warnings' );
|
|
|
|
|
throw new Error("Received unexpected warnings");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config.html) {
|
|
|
|
@ -175,7 +194,7 @@ describe( 'runtime', () => {
|
|
|
|
|
config.test(assert, component, target, window, raf);
|
|
|
|
|
} else {
|
|
|
|
|
component.destroy();
|
|
|
|
|
assert.equal( target.innerHTML, '' );
|
|
|
|
|
assert.equal(target.innerHTML, "");
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
@ -183,9 +202,7 @@ describe( 'runtime', () => {
|
|
|
|
|
|
|
|
|
|
if (config.error && !unintendedError) {
|
|
|
|
|
config.error(assert, err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
} else {
|
|
|
|
|
if (!config.show) console.log(addLineNumbers(code)); // eslint-disable-line no-console
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
@ -193,27 +210,29 @@ describe( 'runtime', () => {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe( 'inline helpers', () => {
|
|
|
|
|
fs.readdirSync( 'test/runtime/samples' ).forEach( dir => {
|
|
|
|
|
describe("inline helpers", () => {
|
|
|
|
|
fs.readdirSync("test/runtime/samples").forEach(dir => {
|
|
|
|
|
runTest(dir, null);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const shared = path.resolve( 'shared.js' );
|
|
|
|
|
describe( 'shared helpers', () => {
|
|
|
|
|
fs.readdirSync( 'test/runtime/samples' ).forEach( dir => {
|
|
|
|
|
const shared = path.resolve("shared.js");
|
|
|
|
|
describe("shared helpers", () => {
|
|
|
|
|
fs.readdirSync("test/runtime/samples").forEach(dir => {
|
|
|
|
|
runTest(dir, shared);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it( 'fails if options.target is missing in dev mode', () => {
|
|
|
|
|
it("fails if options.target is missing in dev mode", () => {
|
|
|
|
|
const { code } = svelte.compile(`<div></div>`, {
|
|
|
|
|
format: 'iife',
|
|
|
|
|
name: 'SvelteComponent',
|
|
|
|
|
format: "iife",
|
|
|
|
|
name: "SvelteComponent",
|
|
|
|
|
dev: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const SvelteComponent = eval( `(function () { ${code}; return SvelteComponent; }())` );
|
|
|
|
|
const SvelteComponent = eval(
|
|
|
|
|
`(function () { ${code}; return SvelteComponent; }())`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
|
new SvelteComponent();
|
|
|
|
|