mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-19 13:55:21 +00:00
fix tests, add WIP dev task
This commit is contained in:
@@ -44,6 +44,7 @@
|
|||||||
"@types/glob": "^5.0.34",
|
"@types/glob": "^5.0.34",
|
||||||
"@types/mkdirp": "^0.5.2",
|
"@types/mkdirp": "^0.5.2",
|
||||||
"@types/rimraf": "^2.0.2",
|
"@types/rimraf": "^2.0.2",
|
||||||
|
"compression": "^1.7.1",
|
||||||
"css-loader": "^0.28.7",
|
"css-loader": "^0.28.7",
|
||||||
"electron": "^1.8.2",
|
"electron": "^1.8.2",
|
||||||
"eslint": "^4.13.1",
|
"eslint": "^4.13.1",
|
||||||
|
|||||||
106
src/cli/dev.ts
Normal file
106
src/cli/dev.ts
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
|
import * as chalk from 'chalk';
|
||||||
|
import * as child_process from 'child_process';
|
||||||
|
import { create_compilers, create_app, create_routes, create_serviceworker, create_template } from 'sapper/core.js';
|
||||||
|
|
||||||
|
type Deferred = {
|
||||||
|
promise?: Promise<any>;
|
||||||
|
fulfil?: (value: any) => void;
|
||||||
|
reject?: (err: Error) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
function deferred() {
|
||||||
|
const d: Deferred = {};
|
||||||
|
|
||||||
|
d.promise = new Promise((fulfil, reject) => {
|
||||||
|
d.fulfil = fulfil;
|
||||||
|
d.reject = reject;
|
||||||
|
});
|
||||||
|
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function create_watcher(src: string, dir: string) {
|
||||||
|
// initial build
|
||||||
|
const routes = create_routes({ src });
|
||||||
|
create_app({ routes, src, dev: true });
|
||||||
|
|
||||||
|
const compilers = create_compilers();
|
||||||
|
|
||||||
|
const deferreds = {
|
||||||
|
client: deferred(),
|
||||||
|
server: deferred()
|
||||||
|
};
|
||||||
|
|
||||||
|
const invalidate = () => Promise.all([
|
||||||
|
deferreds.client.promise,
|
||||||
|
deferreds.server.promise
|
||||||
|
]).then(([client_stats, server_stats]) => {
|
||||||
|
const client_info = client_stats.toJson();
|
||||||
|
fs.writeFileSync(path.join(dir, 'stats.client.json'), JSON.stringify(client_info, null, ' '));
|
||||||
|
|
||||||
|
const server_info = server_stats.toJson();
|
||||||
|
fs.writeFileSync(path.join(dir, 'stats.server.json'), JSON.stringify(server_info, null, ' '));
|
||||||
|
|
||||||
|
const client_files = client_info.assets.map((chunk: { name: string }) => `/client/${chunk.name}`);
|
||||||
|
|
||||||
|
return create_serviceworker({
|
||||||
|
routes: create_routes({ src }),
|
||||||
|
client_files,
|
||||||
|
src
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function watch_compiler(type: 'client' | 'server', callback: (err: Error) => void) {
|
||||||
|
const compiler = compilers[type];
|
||||||
|
|
||||||
|
compiler.plugin('invalid', (filename: string) => {
|
||||||
|
console.log(chalk.cyan(`${type} bundle invalidated, file changed: ${chalk.bold(filename)}`));
|
||||||
|
deferreds[type] = deferred();
|
||||||
|
watcher.ready = invalidate();
|
||||||
|
});
|
||||||
|
|
||||||
|
compiler.plugin('failed', (err: Error) => {
|
||||||
|
deferreds[type].reject(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
return compiler.watch({}, (err: Error, stats: any) => {
|
||||||
|
if (stats.hasErrors()) {
|
||||||
|
deferreds[type].reject(stats.toJson().errors[0]);
|
||||||
|
} else {
|
||||||
|
deferreds[type].fulfil(stats);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const chokidar = require('chokidar');
|
||||||
|
|
||||||
|
function watch_files(pattern: string, callback: () => void) {
|
||||||
|
const watcher = chokidar.watch(pattern, {
|
||||||
|
persistent: false
|
||||||
|
});
|
||||||
|
|
||||||
|
watcher.on('add', callback);
|
||||||
|
watcher.on('change', callback);
|
||||||
|
watcher.on('unlink', callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
watch_files('routes/**/*.+(html|js|mjs)', () => {
|
||||||
|
const routes = create_routes({ src });
|
||||||
|
create_app({ routes, src, dev: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
watch_files('app/template.html', () => {
|
||||||
|
const template = create_template();
|
||||||
|
// TODO reload current page?
|
||||||
|
});
|
||||||
|
|
||||||
|
watch_compiler('client', () => {
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
watch_compiler('server', () => {
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import * as sander from 'sander';
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import cheerio from 'cheerio';
|
import cheerio from 'cheerio';
|
||||||
import URL from 'url-parse';
|
import URL from 'url-parse';
|
||||||
|
import fetch from 'node-fetch';
|
||||||
|
|
||||||
const { OUTPUT_DIR = 'dist' } = process.env;
|
const { OUTPUT_DIR = 'dist' } = process.env;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import chalk from 'chalk';
|
|||||||
import help from './help.md';
|
import help from './help.md';
|
||||||
import build from './build';
|
import build from './build';
|
||||||
import exporter from './export';
|
import exporter from './export';
|
||||||
|
import dev from './dev';
|
||||||
import upgrade from './upgrade';
|
import upgrade from './upgrade';
|
||||||
import { dest, entry, src } from '../config';
|
import { dest, entry, src } from '../config';
|
||||||
import * as pkg from '../../package.json';
|
import * as pkg from '../../package.json';
|
||||||
@@ -39,7 +40,7 @@ if (cmd === 'build') {
|
|||||||
});
|
});
|
||||||
} else if (cmd === 'export') {
|
} else if (cmd === 'export') {
|
||||||
build({ dest, dev: false, entry, src })
|
build({ dest, dev: false, entry, src })
|
||||||
.then(() => exporter({ src, dest }))
|
.then(() => exporter(dest))
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const elapsed = Date.now() - start;
|
const elapsed = Date.now() - start;
|
||||||
console.error(`extracted in ${elapsed}ms`); // TODO beautify this, e.g. 'built in 4.7 seconds'
|
console.error(`extracted in ${elapsed}ms`); // TODO beautify this, e.g. 'built in 4.7 seconds'
|
||||||
@@ -47,6 +48,8 @@ if (cmd === 'build') {
|
|||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
||||||
});
|
});
|
||||||
|
} else if (cmd === 'dev') {
|
||||||
|
dev(src, dest);
|
||||||
} else if (cmd === 'upgrade') {
|
} else if (cmd === 'upgrade') {
|
||||||
upgrade();
|
upgrade();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user