feat: external config loading

+refactor many pieces
This commit is contained in:
Igor Randjelovic
2020-11-21 13:34:09 +01:00
parent b1bc2640db
commit 46853d2c83
15 changed files with 358 additions and 107 deletions

View File

@@ -1,3 +1,6 @@
import webpack from 'webpack';
const id = 'WatchStateLoggerPlugin';
export enum messages {
compilationComplete = 'Webpack compilation complete.',
startWatching = 'Webpack compilation complete. Watching for file changes.',
@@ -13,15 +16,20 @@ export class WatchStateLoggerPlugin {
apply(compiler) {
const plugin = this;
compiler.hooks.watchRun.tapAsync('WatchStateLoggerPlugin', function (compiler, callback) {
compiler.hooks.watchRun.tapAsync(id, function (compiler, callback) {
plugin.isRunningWatching = true;
if (plugin.isRunningWatching) {
console.log(messages.changeDetected);
}
process.send && process.send(messages.changeDetected, (error) => null);
notify(messages.changeDetected);
callback();
});
compiler.hooks.afterEmit.tapAsync('WatchStateLoggerPlugin', function (compilation, callback) {
compiler.hooks.afterEmit.tapAsync(id, function (compilation, callback) {
callback();
if (plugin.isRunningWatching) {
@@ -30,18 +38,20 @@ export class WatchStateLoggerPlugin {
console.log(messages.compilationComplete);
}
const emittedFiles = Object.keys(compilation.assets).filter((assetKey) => compilation.assets[assetKey].emitted);
const emittedFiles = Object.keys(compilation.assets).filter(
(assetKey) => compilation.assets[assetKey].emitted
);
const chunkFiles = getChunkFiles(compilation);
process.send && process.send(messages.compilationComplete, (error) => null);
notify(messages.compilationComplete);
// Send emitted files so they can be LiveSynced if need be
process.send && process.send({ emittedFiles, chunkFiles, hash: compilation.hash }, (error) => null);
notify({ emittedFiles, chunkFiles, hash: compilation.hash });
});
}
}
function getChunkFiles(compilation) {
function getChunkFiles(compilation: webpack.Compilation) {
const chunkFiles = [];
try {
compilation.chunks.forEach((chunk) => {
@@ -57,3 +67,17 @@ function getChunkFiles(compilation) {
return chunkFiles;
}
function notify(message: any) {
if (!process.send) {
return;
}
process.send(message, (error) => {
if (error) {
console.error(`[${id}] Process Send Error: `, error);
}
return null;
});
}