2018-10-03 14:02:29 +00:00
|
|
|
const constants = require('../../constants');
|
|
|
|
const fs = require('../../core/fs');
|
|
|
|
const ProcessWrapper = require('../../core/processes/processWrapper');
|
2018-08-16 20:53:23 +00:00
|
|
|
const webpack = require('webpack');
|
|
|
|
const writeFile = require('util').promisify(require('fs').writeFile);
|
2018-10-04 09:40:52 +00:00
|
|
|
const {errorMessage} = require('../../utils/utils');
|
2018-10-11 11:19:27 +00:00
|
|
|
const WebpackConfigReader = require('./webpackConfigReader');
|
2018-05-10 14:10:09 +00:00
|
|
|
|
|
|
|
let webpackProcess;
|
|
|
|
|
2018-05-15 19:56:15 +00:00
|
|
|
class WebpackProcess extends ProcessWrapper {
|
2018-07-26 00:58:24 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2018-08-16 20:51:34 +00:00
|
|
|
this.webpackConfigName = options.webpackConfigName;
|
2018-10-04 09:40:52 +00:00
|
|
|
this.pipelineConfig = options.pipelineConfig;
|
2018-07-26 00:58:24 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 20:53:23 +00:00
|
|
|
async build(assets, importsList, callback) {
|
|
|
|
try {
|
|
|
|
await this.webpackRun(assets, importsList, callback);
|
|
|
|
} catch (e) {
|
2018-08-18 23:16:03 +00:00
|
|
|
callback(errorMessage(e));
|
2018-08-16 20:53:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-10 14:10:09 +00:00
|
|
|
|
2018-08-16 20:53:23 +00:00
|
|
|
async webpackRun(assets, importsList, callback) {
|
|
|
|
try {
|
|
|
|
await writeFile(
|
|
|
|
fs.dappPath('.embark/embark-aliases.json'),
|
|
|
|
JSON.stringify(importsList)
|
|
|
|
);
|
|
|
|
await writeFile(
|
|
|
|
fs.dappPath('.embark/embark-assets.json'),
|
|
|
|
JSON.stringify(assets)
|
|
|
|
);
|
2018-10-02 16:30:06 +00:00
|
|
|
await writeFile(
|
|
|
|
fs.dappPath('.embark/embark-pipeline.json'),
|
2018-10-04 09:40:52 +00:00
|
|
|
JSON.stringify(this.pipelineConfig)
|
2018-10-02 16:30:06 +00:00
|
|
|
);
|
2018-08-16 20:53:23 +00:00
|
|
|
} catch (e) {
|
2018-08-18 23:16:03 +00:00
|
|
|
return callback(errorMessage(e));
|
2018-08-16 20:53:23 +00:00
|
|
|
}
|
2018-05-10 14:10:09 +00:00
|
|
|
|
2018-10-11 11:19:27 +00:00
|
|
|
const configReader = new WebpackConfigReader({webpackConfigName: this.webpackConfigName});
|
|
|
|
configReader.readConfig((err, config) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2018-05-10 14:10:09 +00:00
|
|
|
|
2018-10-11 11:19:27 +00:00
|
|
|
if (typeof config !== 'object' || config === null) {
|
|
|
|
return callback('bad webpack config, the resolved config was null or not an object');
|
2018-05-10 14:10:09 +00:00
|
|
|
}
|
2018-10-11 11:19:27 +00:00
|
|
|
|
|
|
|
webpack(config).run(async (err, stats) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(errorMessage(err));
|
2018-08-16 20:53:23 +00:00
|
|
|
}
|
2018-10-11 11:19:27 +00:00
|
|
|
if (!config.stats || config.stats === 'none') {
|
|
|
|
return callback();
|
2018-08-16 20:53:23 +00:00
|
|
|
}
|
2018-10-11 11:19:27 +00:00
|
|
|
try {
|
|
|
|
this._log('info', 'writing file ' + ('.embark/stats.report').bold.dim);
|
|
|
|
await writeFile(
|
|
|
|
fs.dappPath('.embark/stats.report'),
|
|
|
|
stats.toString(config.stats)
|
|
|
|
);
|
|
|
|
this._log('info', 'writing file ' + ('.embark/stats.json').bold.dim);
|
|
|
|
await writeFile(
|
|
|
|
fs.dappPath('.embark/stats.json'),
|
|
|
|
JSON.stringify(stats.toJson(config.stats))
|
|
|
|
);
|
|
|
|
if (stats.hasErrors()) {
|
|
|
|
const errors = stats.toJson(config.stats).errors.join('\n');
|
|
|
|
return callback(errors);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
} catch (e) {
|
|
|
|
return callback(errorMessage(e));
|
2018-07-30 21:26:25 +00:00
|
|
|
}
|
2018-10-11 11:19:27 +00:00
|
|
|
});
|
2018-05-10 14:10:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process.on('message', (msg) => {
|
|
|
|
if (msg.action === constants.pipeline.init) {
|
|
|
|
webpackProcess = new WebpackProcess(msg.options);
|
|
|
|
return process.send({result: constants.pipeline.initiated});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.action === constants.pipeline.build) {
|
2018-08-16 20:53:23 +00:00
|
|
|
return webpackProcess.build(msg.assets, msg.importsList, (err) => {
|
2018-05-10 14:10:09 +00:00
|
|
|
process.send({result: constants.pipeline.built, error: err});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|