diff --git a/src/compfile.ts b/src/compfile.ts index 8bb43ea..b95fdd7 100644 --- a/src/compfile.ts +++ b/src/compfile.ts @@ -8,31 +8,32 @@ import { TemplateResult, TemplateResultMap, } from './types'; -import { createCompiler } from './compile'; -import chalk from "chalk"; +import {createCompiler, createCompilerWatcher} from './compile'; +import chalk from 'chalk'; const moduleDetectRegEx = /comp-dist\/union\.main\.js$/; -export function createCompfileWatcher( - name = './compfile.js' -): CompfileWatcher { - const compiler = createCompiler(path.join(process.cwd(), name)); +export function createCompfileWatcher(name = './compfile.js'): CompfileWatcher { + const compiler = createCompilerWatcher(path.join(process.cwd(), name)); let compfile: Compfile | null = null; - compiler.watch({}, (err, _stats) => { + compiler.on('compile', (err, _stats) => { if (err) { console.log(err); } else { console.log(chalk`{green Complied for server}`); Object.keys(require.cache).forEach((module) => { if (moduleDetectRegEx.test(require.cache[module].filename)) { - console.log(chalk`{cyan Reloading ${require.cache[module].filename}}`); + console.log( + chalk`{cyan Reloading ${require.cache[module].filename}}` + ); delete require.cache[module]; } }); compfile = require(path.join(process.cwd(), 'comp-dist/union.main.js')) - .default as Compfile; } + .default as Compfile; + } }); return { @@ -45,17 +46,12 @@ export function createCompfileWatcher( export function getCompfile(name = './compfile.js'): Promise { const compiler = createCompiler(path.join(process.cwd(), name)); - return new Promise((resolve, reject) => { - compiler.run((err, _stats) => { - if (err) { - return reject(err); - } - const compfile = require(path.join( - process.cwd(), - 'comp-dist/union.main.js' - )).default as Compfile; - resolve(compfile); - }); + return compiler.then(() => { + const compfile = require(path.join( + process.cwd(), + 'comp-dist/union.main.js' + )).default as Compfile; + return compfile; }); } diff --git a/src/compile-process.ts b/src/compile-process.ts new file mode 100644 index 0000000..69ee651 --- /dev/null +++ b/src/compile-process.ts @@ -0,0 +1,70 @@ +import * as path from "path"; +import * as webpack from "webpack"; +import * as nodeExternals from "webpack-node-externals"; + +function getWebpackInstance(compfilePath: string) { + const prodOptions = require(path.join( + process.cwd(), + './config/webpack.comp.config.js' + )); + const options = { + ...prodOptions, + entry: compfilePath, + target: 'node', + externals: nodeExternals(), + plugins: [ + ...prodOptions.plugins, + new webpack.DefinePlugin({ + 'process.env.COMP_NODE': '1', + }), + ], + output: { + ...prodOptions.output, + library: 'CompApp', + libraryTarget: 'umd', + }, + }; + + return webpack(options); +} + +if (!process.send) { + console.error('not in child process'); + process.exit(1); +} else { + function watch(compfilePath: string, process: any) { + const webpack = getWebpackInstance(compfilePath); + webpack.watch({}, (err, _stats) => { + if (err) { + console.log(err); + } else { + process.send({ type: 'status', value: 'compiled' }) + } + }) + } + + function run(compfilePath: string, process: any) { + const webpack = getWebpackInstance(compfilePath); + + webpack.run((err, _stats) => { + if (err) { + console.log(err); + process.send({ type: 'status', value: 'error' }) + } else { + process.send({ type: 'status', value: 'compiled' }) + } + }) + } + + + process.on('message', (message) => { + const { type, value } = message; + if (type === 'start') { + if (value === 'watch') { + watch(message.compfilePath, process); + } else if (value === 'run') { + run(message.compfilePath, process); + } + } + }); +} diff --git a/src/compile.ts b/src/compile.ts index c9057ba..845398f 100644 --- a/src/compile.ts +++ b/src/compile.ts @@ -1,31 +1,41 @@ -import * as webpack from 'webpack'; -import * as nodeExternals from 'webpack-node-externals'; -import * as path from 'path'; +import * as childProcess from 'child_process'; +import * as EventEmitter from 'events'; +import * as path from "path"; +import chalk from "chalk"; + +class WebpackEmitter extends EventEmitter {} + +export function createCompilerWatcher(compfilePath: string): WebpackEmitter { + const compilerChild = childProcess.fork(path.join(__dirname, './compile-process')); + const ee = new WebpackEmitter(); + + compilerChild.send({ type: 'start', value: 'watch', compfilePath }); + + compilerChild.on('message', ({ type, value }) => { + if (type === 'status') { + if (value === 'compiling') { + } else if (value === 'compiled') { + ee.emit('compile'); + } + } + }); + + return ee; +} export function createCompiler(compfilePath: string) { - const prodOptions = require(path.join( - process.cwd(), - 'webpack.comp.config.js' - )); - const options = { - ...prodOptions, - entry: compfilePath, - target: 'node', - externals: nodeExternals(), - plugins: [ - ...prodOptions.plugins, - new webpack.DefinePlugin({ - 'process.env': { - 'COMP_NODE': '1' - } - }) - ], - output: { - ...prodOptions.output, - library: 'CompApp', - libraryTarget: 'umd', - }, - }; + const compilerChild = childProcess.fork(path.join(__dirname, './compile-process')); + compilerChild.send({ type: 'start', value: 'run', compfilePath }); - return webpack(options); + return new Promise((resolve, reject) => { + compilerChild.on('message', ({ type, value}) => { + if (type === 'status') { + if (value === 'compiled') { + resolve(); + } else if (value === 'error') { + reject(); + } + } + }) + }) }