This repository has been archived by the owner on Jul 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #13
- Loading branch information
James Canning
committed
Jan 23, 2018
1 parent
9c0df37
commit 31243a5
Showing
3 changed files
with
123 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
}) | ||
}) | ||
} |