mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-21 23:05:02 +00:00
run webpack in watch mode
This commit is contained in:
@@ -23,4 +23,8 @@ module.exports = function create_app(src, dest, routes, options) {
|
|||||||
const main = template.replace('__routes__', code);
|
const main = template.replace('__routes__', code);
|
||||||
|
|
||||||
fs.writeFileSync(main_built, main);
|
fs.writeFileSync(main_built, main);
|
||||||
|
|
||||||
|
// need to fudge the mtime, because webpack is soft in the head
|
||||||
|
const stats = fs.statSync(main_built);
|
||||||
|
fs.utimesSync(main_built, stats.atimeMs - 9999, stats.mtimeMs - 9999);
|
||||||
};
|
};
|
||||||
@@ -2,72 +2,52 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const glob = require('glob');
|
const glob = require('glob');
|
||||||
const webpack = require('webpack');
|
const webpack = require('webpack');
|
||||||
|
const { dev } = require('../config.js');
|
||||||
const templates = require('../templates.js');
|
const templates = require('../templates.js');
|
||||||
|
|
||||||
module.exports = function create_webpack_compiler(dest, routes, dev) {
|
module.exports = function create_webpack_compiler(dest, routes, dev) {
|
||||||
const compiler = {};
|
const compiler = {};
|
||||||
|
|
||||||
const client = webpack(
|
function go() {
|
||||||
require(path.resolve('webpack.client.config.js'))
|
const client = webpack(
|
||||||
);
|
require(path.resolve('webpack.client.config.js'))
|
||||||
|
);
|
||||||
|
|
||||||
const server = webpack(
|
const server = webpack(
|
||||||
require(path.resolve('webpack.server.config.js'))
|
require(path.resolve('webpack.server.config.js'))
|
||||||
);
|
);
|
||||||
|
|
||||||
if (false) { // TODO watch in dev
|
function client_updated(err, stats, reject) {
|
||||||
// TODO how can we invalidate compiler.client_main when watcher restarts?
|
console.log(stats.toString({ colors: true }));
|
||||||
compiler.client_main = new Promise((fulfil, reject) => {
|
|
||||||
client.watch({}, (err, stats) => {
|
|
||||||
if (err || stats.hasErrors()) {
|
|
||||||
// TODO handle errors
|
|
||||||
}
|
|
||||||
|
|
||||||
const filename = stats.toJson().assetsByChunkName.main;
|
const info = stats.toJson();
|
||||||
fulfil(`/client/${filename}`);
|
|
||||||
|
if (err || stats.hasErrors()) {
|
||||||
|
reject(err || info.errors[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
compiler.client_main = `/client/${info.assetsByChunkName.main}`;
|
||||||
|
compiler.assets = info.assets.map(asset => `/client/${asset.name}`);
|
||||||
|
|
||||||
|
compiler.asset_cache = {};
|
||||||
|
compiler.assets.forEach(file => {
|
||||||
|
compiler.asset_cache[file] = fs.readFileSync(path.join(dest, file), 'utf-8');
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
// TODO server
|
function server_updated(err, stats, reject) {
|
||||||
} else {
|
console.log(stats.toString({ colors: true }));
|
||||||
compiler.ready = Promise.all([
|
|
||||||
new Promise((fulfil, reject) => {
|
|
||||||
client.run((err, stats) => {
|
|
||||||
console.log(stats.toString({ colors: true }));
|
|
||||||
|
|
||||||
const info = stats.toJson();
|
const info = stats.toJson();
|
||||||
|
|
||||||
if (err || stats.hasErrors()) {
|
if (err || stats.hasErrors()) {
|
||||||
reject(err || info.errors[0]);
|
reject(err || info.errors[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
compiler.client_main = `/client/${info.assetsByChunkName.main}`;
|
compiler.chunks = info.assetsByChunkName;
|
||||||
compiler.assets = info.assets.map(asset => `/client/${asset.name}`);
|
}
|
||||||
|
|
||||||
compiler.asset_cache = {};
|
function both_updated() {
|
||||||
compiler.assets.forEach(file => {
|
|
||||||
compiler.asset_cache[file] = fs.readFileSync(path.join(dest, file), 'utf-8');
|
|
||||||
});
|
|
||||||
|
|
||||||
fulfil();
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
|
|
||||||
new Promise((fulfil, reject) => {
|
|
||||||
server.run((err, stats) => {
|
|
||||||
console.log(stats.toString({ colors: true }));
|
|
||||||
|
|
||||||
const info = stats.toJson();
|
|
||||||
|
|
||||||
if (err || stats.hasErrors()) {
|
|
||||||
reject(err || info.errors[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
compiler.chunks = info.assetsByChunkName;
|
|
||||||
fulfil();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
]).then(() => {
|
|
||||||
const assets = glob.sync('**', { cwd: 'assets', nodir: true });
|
const assets = glob.sync('**', { cwd: 'assets', nodir: true });
|
||||||
|
|
||||||
const route_code = `[${
|
const route_code = `[${
|
||||||
@@ -93,12 +73,65 @@ module.exports = function create_webpack_compiler(dest, routes, dev) {
|
|||||||
// useful for debugging, but the files are served from memory
|
// useful for debugging, but the files are served from memory
|
||||||
fs.writeFileSync(path.resolve(dest, 'service-worker.js'), compiler.service_worker);
|
fs.writeFileSync(path.resolve(dest, 'service-worker.js'), compiler.service_worker);
|
||||||
fs.writeFileSync(path.resolve(dest, 'index.html'), compiler.shell);
|
fs.writeFileSync(path.resolve(dest, 'index.html'), compiler.shell);
|
||||||
});
|
}
|
||||||
|
|
||||||
compiler.get_chunk = async id => {
|
if (dev) {
|
||||||
return path.resolve(dest, 'server', compiler.chunks[id]);
|
let client_is_ready = false;
|
||||||
};
|
let server_is_ready = false;
|
||||||
|
|
||||||
|
let fulfil;
|
||||||
|
let reject;
|
||||||
|
|
||||||
|
const invalidate = () => new Promise((f, r) => {
|
||||||
|
fulfil = f;
|
||||||
|
reject = r;
|
||||||
|
});
|
||||||
|
|
||||||
|
compiler.ready = invalidate();
|
||||||
|
|
||||||
|
client.plugin('invalid', () => {
|
||||||
|
client_is_ready = false;
|
||||||
|
compiler.ready = invalidate();
|
||||||
|
});
|
||||||
|
|
||||||
|
server.plugin('invalid', () => {
|
||||||
|
server_is_ready = false;
|
||||||
|
compiler.ready = invalidate();
|
||||||
|
});
|
||||||
|
|
||||||
|
client.watch({}, (err, stats) => {
|
||||||
|
client_updated(err, stats, reject);
|
||||||
|
client_is_ready = true;
|
||||||
|
if (server_is_ready) fulfil();
|
||||||
|
});
|
||||||
|
|
||||||
|
server.watch({}, (err, stats) => {
|
||||||
|
server_updated(err, stats, reject);
|
||||||
|
server_is_ready = true;
|
||||||
|
if (client_is_ready) fulfil();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
compiler.ready = Promise.all([
|
||||||
|
new Promise((fulfil, reject) => {
|
||||||
|
client.run((err, stats) => {
|
||||||
|
client_updated(err, stats, reject);
|
||||||
|
fulfil();
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
|
||||||
|
new Promise((fulfil, reject) => {
|
||||||
|
server.run((err, stats) => {
|
||||||
|
server_updated(err, stats, reject);
|
||||||
|
fulfil();
|
||||||
|
});
|
||||||
|
})
|
||||||
|
]).then(both_updated);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO rerun go when routes are added/renamed
|
||||||
|
// (or webpack config/templates change?)
|
||||||
|
go();
|
||||||
|
|
||||||
return compiler;
|
return compiler;
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user