UNPKG

server-renderer

Version:

library of server side render for React

101 lines (100 loc) 3.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const webpack = require("webpack"); const fs = require("fs"); const path = require("path"); const chalk = require("chalk"); const child_process_1 = require("child_process"); const webpack_config_1 = require("../config/webpack-config"); process.env.NODE_ENV = 'development'; let childProcess = null; let chunkPath = ''; let buildTime = 0; let hasError = false; function createChildProcess() { childProcess = child_process_1.fork(chunkPath, [], { stdio: 'inherit' }); childProcess.on('error', (err) => { console.error(err.message); process.exit(1); }); childProcess.on('close', () => { var _a; console.clear(); if (((_a = childProcess) === null || _a === void 0 ? void 0 : _a.killed) && !hasError) { createChildProcess(); } }); console.log(chalk.green(`Compile succeed! Done in ${(buildTime / 1000).toFixed(2)}s`)); } function build(configs) { const compiler = webpack(configs); compiler.hooks.watchRun.tap('ssr-watch-run', () => { console.clear(); console.log(chalk.green(`Compiling...`)); }); compiler.watch({ ignored: /node_module/ }, (err, stats) => { var _a, _b, _c; if (err) { hasError = true; (_a = childProcess) === null || _a === void 0 ? void 0 : _a.kill(1); throw err; } const [serverStats] = stats.stats; const { errors, warnings, outputPath, assetsByChunkName, time } = serverStats.toJson(); if (warnings.length) { console.log(chalk.yellowBright('Compiled with warnings')); warnings.forEach(warning => { console.log(chalk.yellowBright(warning)); }); } if (errors.length) { console.log(chalk.redBright('Compiled with errors')); errors.forEach(error => { console.log(chalk.redBright(error)); }); hasError = true; (_b = childProcess) === null || _b === void 0 ? void 0 : _b.kill(1); process.exit(1); } hasError = false; // [app.js, app.js.map] const assets = (_c = assetsByChunkName) === null || _c === void 0 ? void 0 : _c.app; if (outputPath && path.basename(outputPath) === 'client') { return; } chunkPath = path.join(((outputPath !== null && outputPath !== void 0 ? outputPath : '')).toString(), assets[0]); buildTime = time; if (childProcess) { childProcess.kill(); } else { createChildProcess(); } }); } function rmdir(directory) { if (directory && fs.existsSync(directory)) { const files = fs.readdirSync(directory); if (files.length) { files.forEach(filename => { const filePath = path.join(directory, filename); const stats = fs.statSync(filePath); if (stats.isDirectory()) { rmdir(filePath); } else { fs.unlinkSync(filePath); } }); } fs.rmdirSync(directory); } } function buildEveryThing() { var _a; const serverDevConfig = webpack_config_1.createWebpackConfig(true); const clientDevConfig = webpack_config_1.createWebpackConfig(false); rmdir((_a = serverDevConfig.output) === null || _a === void 0 ? void 0 : _a.path); build([serverDevConfig, clientDevConfig]); } buildEveryThing();