2018-05-10 14:10:09 +00:00
|
|
|
const constants = require('../constants');
|
2018-08-16 20:53:23 +00:00
|
|
|
const fs = require('../core/fs');
|
2018-07-27 21:33:50 +00:00
|
|
|
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-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-08-16 20:53:23 +00:00
|
|
|
if (!process.env.DAPP_PATH) {
|
|
|
|
process.env.DAPP_PATH = fs.dappPath();
|
|
|
|
}
|
|
|
|
if (!process.env.EMBARK_PATH) {
|
|
|
|
process.env.EMBARK_PATH = fs.embarkPath();
|
|
|
|
}
|
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) {
|
|
|
|
console.error(e.message);
|
|
|
|
callback(e);
|
|
|
|
// ?? should return e or e.message
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e.message);
|
|
|
|
return callback(e);
|
|
|
|
// ?? should return e or e.message
|
|
|
|
}
|
2018-05-10 14:10:09 +00:00
|
|
|
|
2018-08-16 20:53:23 +00:00
|
|
|
const dappConfigPath = fs.dappPath('webpack.config.js');
|
|
|
|
const defaultConfigPath = fs.embarkPath('webpack.config.js');
|
2018-05-10 14:10:09 +00:00
|
|
|
|
2018-08-16 20:53:23 +00:00
|
|
|
let config, configPath;
|
|
|
|
try {
|
|
|
|
if (fs.existsSync(dappConfigPath)) {
|
|
|
|
configPath = dappConfigPath;
|
2018-08-16 22:02:06 +00:00
|
|
|
delete require.cache[configPath];
|
2018-08-16 20:53:23 +00:00
|
|
|
} else {
|
|
|
|
configPath = defaultConfigPath;
|
2018-05-10 14:10:09 +00:00
|
|
|
}
|
2018-08-16 22:02:06 +00:00
|
|
|
config = require(configPath);
|
2018-08-16 20:53:23 +00:00
|
|
|
// valid config types: https://webpack.js.org/configuration/configuration-types/
|
|
|
|
// + function that returns a config object
|
|
|
|
// + function that returns a promise for a config object
|
|
|
|
// + array of named config objects
|
|
|
|
// + config object
|
|
|
|
if (typeof config === 'function') {
|
2018-08-16 21:38:50 +00:00
|
|
|
config = await config(this.webpackConfigName);
|
2018-08-16 20:53:23 +00:00
|
|
|
} else if (Array.isArray(config)) {
|
2018-08-16 21:38:50 +00:00
|
|
|
config = config.filter(cfg => cfg.name === this.webpackConfigName);
|
2018-08-16 20:53:23 +00:00
|
|
|
if (!config.length) {
|
2018-08-16 21:38:50 +00:00
|
|
|
const errMsg = `no webpack config has the name '${this.webpackConfigName}'`;
|
2018-08-16 20:53:23 +00:00
|
|
|
console.error(errMsg);
|
|
|
|
return callback(errMsg);
|
|
|
|
// ?? should the message be wrapped in new Error()
|
|
|
|
}
|
|
|
|
if (config.length > 1) {
|
|
|
|
console.warn(
|
2018-08-16 21:38:50 +00:00
|
|
|
`detected ${config.length} webpack configs having the name '${this.webpackConfigName}', using the first one`
|
2018-08-16 20:53:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
config = config[0];
|
2018-07-30 21:26:25 +00:00
|
|
|
} else {
|
2018-08-16 22:01:50 +00:00
|
|
|
// proceed with the value obtained from require(dapp/default/ConfigPath)
|
2018-07-30 21:26:25 +00:00
|
|
|
}
|
2018-08-16 20:53:23 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(`error while loading webpack config ${configPath}`);
|
|
|
|
console.error(e.message);
|
|
|
|
callback(e);
|
|
|
|
// ?? should return e or e.message
|
|
|
|
}
|
2018-05-10 14:10:09 +00:00
|
|
|
|
2018-08-16 20:53:23 +00:00
|
|
|
if (typeof config !== 'object' || config === null) {
|
|
|
|
const errMsg = 'bad webpack config, the resolved config was null or not an object';
|
|
|
|
console.error(errMsg);
|
|
|
|
return callback(errMsg);
|
|
|
|
// ?? should the message be wrapped in new Error()
|
|
|
|
}
|
2018-05-10 14:10:09 +00:00
|
|
|
|
2018-08-16 20:53:23 +00:00
|
|
|
webpack(config).run(async (err, stats) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (config.stats && config.stats !== 'none') {
|
2018-08-16 21:38:50 +00:00
|
|
|
this._log('info', 'writing file '+ ('.embark/stats.report').bold.dim);
|
2018-08-16 20:53:23 +00:00
|
|
|
await writeFile(
|
|
|
|
fs.dappPath('.embark/stats.report'),
|
|
|
|
stats.toString(config.stats)
|
|
|
|
);
|
2018-08-16 21:38:50 +00:00
|
|
|
this._log('info','writing file '+ ('.embark/stats.json').bold.dim);
|
2018-08-16 20:53:23 +00:00
|
|
|
await writeFile(
|
|
|
|
fs.dappPath('.embark/stats.json'),
|
|
|
|
JSON.stringify(stats.toJson(config.stats))
|
|
|
|
);
|
2018-07-30 21:26:25 +00:00
|
|
|
}
|
2018-08-16 20:53:23 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e.message);
|
|
|
|
return callback(err);
|
2018-05-10 14:10:09 +00:00
|
|
|
}
|
2018-08-16 20:53:23 +00:00
|
|
|
if (config.stats && stats.hasErrors()) {
|
|
|
|
const errors = stats.toJson(config.stats).errors.join('\n');
|
|
|
|
console.error(errors);
|
|
|
|
return callback(errors);
|
|
|
|
}
|
|
|
|
callback();
|
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});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|