server-renderer
Version:
library of server side render for React
103 lines (102 loc) • 4.05 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const webpack_1 = tslib_1.__importDefault(require("webpack"));
const path_1 = tslib_1.__importDefault(require("path"));
const fs_1 = tslib_1.__importDefault(require("fs"));
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const child_process_1 = require("child_process");
const utils_1 = require("./utils");
const webpack_config_1 = require("../config/webpack-config");
const config_1 = require("../config/config");
process.env.NODE_ENV = 'development';
const config = config_1.getConfig();
let childProcess = null;
let chunkPath = '';
let buildTime = 0;
let hasError = false;
let isChildProcessExited = true;
function createChildProcess() {
childProcess = child_process_1.fork(chunkPath, [], { stdio: 'inherit' });
isChildProcessExited = false;
childProcess.on('error', (err) => {
utils_1.logError(err.message);
process.exit(1);
});
childProcess.on('close', () => {
// !hasError && config.cleanConsoleOnRebuild && console.clear()
var _a;
if (((_a = childProcess) === null || _a === void 0 ? void 0 : _a.killed) && !hasError) {
createChildProcess();
}
});
childProcess.on('exit', () => {
isChildProcessExited = true;
});
!hasError && utils_1.logSuccess(`Compiled successfully! Done in ${(buildTime / 1000).toFixed(2)}s`);
}
function runCompile(configs) {
const compiler = webpack_1.default(configs);
compiler.hooks.watchRun.tap('server-renderer', compiler => {
const isClient = path_1.default.basename(compiler.outputPath) === 'client';
if (isClient) {
return;
}
config.cleanConsoleOnRebuild && console.clear();
utils_1.logSuccess('compiling...');
});
compiler.watch({ ignored: /node_modules/ }, (err, stats) => {
var _a, _b;
if (err) {
utils_1.logError(err.message);
hasError = true;
(_a = childProcess) === null || _a === void 0 ? void 0 : _a.kill(1);
return;
}
const [serverStats] = stats.stats;
const { errors, warnings, outputPath, assetsByChunkName, time } = serverStats.toJson();
// [app.js, app.js.map] or app.js
const assets = (_b = assetsByChunkName) === null || _b === void 0 ? void 0 : _b.app;
if (!assets || outputPath && path_1.default.basename(outputPath) === 'client') {
return;
}
if (warnings.length) {
console.log(chalk_1.default.yellowBright('Compiled with warnings'));
warnings.forEach(warning => {
console.log(chalk_1.default.yellowBright(warning));
});
}
if (errors.length) {
utils_1.logError('Compiled with errors');
errors.forEach(utils_1.logError);
hasError = true;
}
else {
hasError = false;
}
chunkPath = path_1.default.join(outputPath, Array.isArray(assets) ? assets[0] : assets);
buildTime = time;
if (childProcess && !isChildProcessExited) {
childProcess.kill();
}
else {
createChildProcess();
}
});
}
function build() {
var _a, _b;
const serverDevConfig = webpack_config_1.createWebpackConfig(true);
const clientDevConfig = webpack_config_1.createWebpackConfig(false);
// clear output directory and copy static files.
const serverOutput = (_a = serverDevConfig.output) === null || _a === void 0 ? void 0 : _a.path;
const clientOutput = (_b = clientDevConfig.output) === null || _b === void 0 ? void 0 : _b.path;
utils_1.deleteDir(config.distDir);
// create output directory
fs_1.default.mkdirSync(config.distDir);
const publicDir = path_1.default.join(config.rootDir, 'public');
utils_1.copyDir(publicDir, serverOutput);
utils_1.copyDir(publicDir, clientOutput);
runCompile([serverDevConfig, clientDevConfig]);
}
build();
;