Skip to content
This repository has been archived by the owner on Jul 23, 2019. It is now read-only.

Commit

Permalink
Workerize comp webpack instance
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
James Canning committed Jan 23, 2018
1 parent 9c0df37 commit 31243a5
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 47 deletions.
36 changes: 16 additions & 20 deletions src/compfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -45,17 +46,12 @@ export function createCompfileWatcher(
export function getCompfile(name = './compfile.js'): Promise<Compfile> {
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;
});
}

Expand Down
70 changes: 70 additions & 0 deletions src/compile-process.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
});
}
64 changes: 37 additions & 27 deletions src/compile.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
})
})
}

0 comments on commit 31243a5

Please sign in to comment.