|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as fs from "fs";
|
|
|
|
|
import * as path from "path";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
showOutput,
|
|
|
|
@ -10,31 +10,31 @@ import {
|
|
|
|
|
cleanRequireCache,
|
|
|
|
|
shouldUpdateExpected,
|
|
|
|
|
mkdirp,
|
|
|
|
|
} from '../helpers';
|
|
|
|
|
import { glob } from '../tiny-glob';
|
|
|
|
|
import { assert } from '../test';
|
|
|
|
|
} from "../helpers";
|
|
|
|
|
import { glob } from "../tiny-glob";
|
|
|
|
|
import { assert } from "../test";
|
|
|
|
|
|
|
|
|
|
function tryToReadFile(file) {
|
|
|
|
|
try {
|
|
|
|
|
return fs.readFileSync(file, 'utf-8');
|
|
|
|
|
return fs.readFileSync(file, "utf-8");
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err.code !== 'ENOENT') throw err;
|
|
|
|
|
if (err.code !== "ENOENT") throw err;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sveltePath = process.cwd().split('\\').join('/');
|
|
|
|
|
const sveltePath = process.cwd().split("\\").join("/");
|
|
|
|
|
let compile = null;
|
|
|
|
|
|
|
|
|
|
describe('ssr', () => {
|
|
|
|
|
describe("ssr", () => {
|
|
|
|
|
before(() => {
|
|
|
|
|
compile = loadSvelte(true).compile;
|
|
|
|
|
|
|
|
|
|
return setupHtmlEqual();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fs.readdirSync(`${__dirname}/samples`).forEach((dir) => {
|
|
|
|
|
if (dir[0] === '.') return;
|
|
|
|
|
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
|
|
|
|
|
if (dir[0] === ".") return;
|
|
|
|
|
|
|
|
|
|
const config = loadConfig(`${__dirname}/samples/${dir}/_config.js`);
|
|
|
|
|
|
|
|
|
@ -45,7 +45,7 @@ describe('ssr', () => {
|
|
|
|
|
const show = /\.show/.test(dir);
|
|
|
|
|
|
|
|
|
|
if (solo && process.env.CI) {
|
|
|
|
|
throw new Error('Forgot to remove `solo: true` from test');
|
|
|
|
|
throw new Error("Forgot to remove `solo: true` from test");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(skip ? it.skip : solo ? it.only : it)(dir, () => {
|
|
|
|
@ -56,17 +56,17 @@ describe('ssr', () => {
|
|
|
|
|
const compileOptions = {
|
|
|
|
|
sveltePath,
|
|
|
|
|
...config.compileOptions,
|
|
|
|
|
generate: 'ssr',
|
|
|
|
|
format: 'cjs',
|
|
|
|
|
generate: "ssr",
|
|
|
|
|
format: "cjs",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
require('../../register')(compileOptions);
|
|
|
|
|
require("../../register")(compileOptions);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const Component = require(`${dir}/main.svelte`).default;
|
|
|
|
|
|
|
|
|
|
const expectedHtml = tryToReadFile(`${dir}/_expected.html`);
|
|
|
|
|
const expectedCss = tryToReadFile(`${dir}/_expected.css`) || '';
|
|
|
|
|
const expectedCss = tryToReadFile(`${dir}/_expected.css`) || "";
|
|
|
|
|
|
|
|
|
|
const props = tryToLoadJson(`${dir}/data.json`) || undefined;
|
|
|
|
|
|
|
|
|
@ -88,7 +88,10 @@ describe('ssr', () => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
assert.equal(css.code.replace(/^\s+/gm, '').replace(/[\r\n]/, ''), expectedCss.replace(/^\s+/gm, '').replace(/[\r\n]/, ''));
|
|
|
|
|
assert.equal(
|
|
|
|
|
css.code.replace(/^\s+/gm, "").replace(/[\r\n]/, ""),
|
|
|
|
|
expectedCss.replace(/^\s+/gm, "").replace(/[\r\n]/, "")
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (shouldUpdateExpected()) {
|
|
|
|
|
fs.writeFileSync(`${dir}/_expected.css`, css.code);
|
|
|
|
@ -102,7 +105,10 @@ describe('ssr', () => {
|
|
|
|
|
fs.writeFileSync(`${dir}/_actual-head.html`, head);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
assert.htmlEqual(head, fs.readFileSync(`${dir}/_expected-head.html`, 'utf-8'));
|
|
|
|
|
assert.htmlEqual(
|
|
|
|
|
head,
|
|
|
|
|
fs.readFileSync(`${dir}/_expected-head.html`, 'utf-8')
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (shouldUpdateExpected()) {
|
|
|
|
|
fs.writeFileSync(`${dir}/_expected-head.html`, head);
|
|
|
|
@ -113,9 +119,9 @@ describe('ssr', () => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (show) showOutput(dir, { generate: 'ssr', format: 'cjs' });
|
|
|
|
|
if (show) showOutput(dir, { generate: "ssr", format: "cjs" });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
showOutput(dir, { generate: 'ssr', format: 'cjs' });
|
|
|
|
|
showOutput(dir, { generate: "ssr", format: "cjs" });
|
|
|
|
|
err.stack += `\n\ncmd-click: ${path.relative(process.cwd(), dir)}/main.svelte`;
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
@ -123,21 +129,21 @@ describe('ssr', () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// duplicate client-side tests, as far as possible
|
|
|
|
|
fs.readdirSync('test/runtime/samples').forEach((dir) => {
|
|
|
|
|
if (dir[0] === '.') return;
|
|
|
|
|
fs.readdirSync("test/runtime/samples").forEach(dir => {
|
|
|
|
|
if (dir[0] === ".") return;
|
|
|
|
|
|
|
|
|
|
const config = loadConfig(`./runtime/samples/${dir}/_config.js`);
|
|
|
|
|
const solo = config.solo || /\.solo/.test(dir);
|
|
|
|
|
const skip = config.skip || /\.skip/.test(dir);
|
|
|
|
|
|
|
|
|
|
if (solo && process.env.CI) {
|
|
|
|
|
throw new Error('Forgot to remove `solo: true` from test');
|
|
|
|
|
throw new Error("Forgot to remove `solo: true` from test");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config.skip_if_ssr) return;
|
|
|
|
|
|
|
|
|
|
(skip ? it.skip : solo ? it.only : it)(dir, () => {
|
|
|
|
|
const cwd = path.resolve('test/runtime/samples', dir);
|
|
|
|
|
const cwd = path.resolve("test/runtime/samples", dir);
|
|
|
|
|
|
|
|
|
|
cleanRequireCache();
|
|
|
|
|
|
|
|
|
@ -146,17 +152,17 @@ describe('ssr', () => {
|
|
|
|
|
const compileOptions = {
|
|
|
|
|
sveltePath,
|
|
|
|
|
...config.compileOptions,
|
|
|
|
|
generate: 'ssr',
|
|
|
|
|
format: 'cjs',
|
|
|
|
|
generate: "ssr",
|
|
|
|
|
format: "cjs"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
require('../../register')(compileOptions);
|
|
|
|
|
require("../../register")(compileOptions);
|
|
|
|
|
|
|
|
|
|
glob('**/*.svelte', { cwd }).forEach((file) => {
|
|
|
|
|
if (file[0] === '_') return;
|
|
|
|
|
glob("**/*.svelte", { cwd }).forEach((file) => {
|
|
|
|
|
if (file[0] === "_") return;
|
|
|
|
|
|
|
|
|
|
const dir = `${cwd}/_output/ssr`;
|
|
|
|
|
const out = `${dir}/${file.replace(/\.svelte$/, '.js')}`;
|
|
|
|
|
const out = `${dir}/${file.replace(/\.svelte$/, ".js")}`;
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync(out)) {
|
|
|
|
|
fs.unlinkSync(out);
|
|
|
|
@ -165,10 +171,13 @@ describe('ssr', () => {
|
|
|
|
|
mkdirp(dir);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const { js } = compile(fs.readFileSync(`${cwd}/${file}`, 'utf-8'), {
|
|
|
|
|
const { js } = compile(
|
|
|
|
|
fs.readFileSync(`${cwd}/${file}`, 'utf-8'),
|
|
|
|
|
{
|
|
|
|
|
...compileOptions,
|
|
|
|
|
filename: file,
|
|
|
|
|
});
|
|
|
|
|
filename: file
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(out, js.code);
|
|
|
|
|
} catch (err) {
|
|
|
|
@ -181,7 +190,7 @@ describe('ssr', () => {
|
|
|
|
|
|
|
|
|
|
const Component = require(`../runtime/samples/${dir}/main.svelte`).default;
|
|
|
|
|
const { html } = Component.render(config.props, {
|
|
|
|
|
store: config.store !== true && config.store,
|
|
|
|
|
store: (config.store !== true) && config.store
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (config.ssrHtml) {
|
|
|
|
@ -199,7 +208,7 @@ describe('ssr', () => {
|
|
|
|
|
err.stack += `\n\ncmd-click: ${path.relative(process.cwd(), cwd)}/main.svelte`;
|
|
|
|
|
|
|
|
|
|
if (config.error) {
|
|
|
|
|
if (typeof config.error === 'function') {
|
|
|
|
|
if (typeof config.error === "function") {
|
|
|
|
|
config.error(assert, err);
|
|
|
|
|
} else {
|
|
|
|
|
assert.equal(err.message, config.error);
|
|
|
|
|