refactor(modules/pipeline/webpackProcess): load pipeline config in Config class

to configure pipeline specific options like TypeScript support.

At the time this has been added, `core/config` didn't handle the loading
of configuration files for pipeline related tasks yet.

This commit ensures a dapp's `pipeline.json` will be loaded and used to
configure Embark's webpack process as part of Embark's `Config` initialization.
This commit is contained in:
Pascal Precht 2018-10-04 11:40:52 +02:00
parent 71e92358a8
commit d34428a129
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
3 changed files with 46 additions and 25 deletions

View File

@ -19,7 +19,7 @@ var Config = function(options) {
this.chainTracker = {};
this.assetFiles = {};
this.contractsFiles = [];
this.configDir = options.configDir || 'config/';
this.configDir = options.configDir || DEFAULT_CONFIG_PATH;
this.chainsFile = options.chainsFile || './chains.json';
this.plugins = options.plugins;
this.logger = options.logger;
@ -67,6 +67,7 @@ Config.prototype.loadConfigFiles = function(options) {
this.loadCommunicationConfigFile();
this.loadNameSystemConfigFile();
this.loadPipelineConfigFile();
this.loadAssetFiles();
this.loadContractsConfigFile();
this.loadExternalContractsFiles();
this.loadWebServerConfigFile();
@ -83,6 +84,7 @@ Config.prototype.reloadConfig = function() {
this.loadCommunicationConfigFile();
this.loadNameSystemConfigFile();
this.loadPipelineConfigFile();
this.loadAssetFiles();
this.loadContractsConfigFile();
this.loadExternalContractsFiles();
this.loadChainTrackerFile();
@ -388,10 +390,39 @@ Config.prototype.loadEmbarkConfigFile = function() {
};
Config.prototype.loadPipelineConfigFile = function() {
var assets = this.embarkConfig.app;
for(var targetFile in assets) {
this.assetFiles[targetFile] = this.loadFiles(assets[targetFile]);
const defaultPipelineConfig = {
typescript: false
};
let pipelineConfigPath = this._getFileOrOject(this.configDir, 'pipeline', 'pipeline');
// Embark applications in "simple" mode that aren't aware of `pipeline.js` configuration capabilities
// won't have a pipeline config path so we need to perform this safety check here, otherwise the
// next expression is going to throw.
if (pipelineConfigPath !== undefined) {
// At this point, `pipelineConfigPath` could be either `config/pipeline` or a filepath including its extension.
// We need to make sure that we always have an extension.
pipelineConfigPath = `${fs.dappPath(pipelineConfigPath)}${path.extname(pipelineConfigPath) === '.js' ? '' : '.js'}`;
}
let pipelineConfig = defaultPipelineConfig;
if (pipelineConfigPath && fs.existsSync(pipelineConfigPath)) {
delete require.cache[pipelineConfigPath];
pipelineConfig = utils.recursiveMerge(
utils.recursiveMerge(true, pipelineConfig),
require(pipelineConfigPath)
);
}
this.pipelineConfig = pipelineConfig;
};
Config.prototype.loadAssetFiles = function () {
Object.keys(this.embarkConfig.app).forEach(targetFile => {
this.assetFiles[targetFile] = this.loadFiles(this.embarkConfig.app[targetFile]);
});
};
Config.prototype.loadChainTrackerFile = function() {

View File

@ -15,6 +15,7 @@ class Pipeline {
this.plugins = embark.config.plugins;
this.webpackConfigName = options.webpackConfigName;
this.pipelinePlugins = this.plugins.getPluginsFor('pipeline');
this.pipelineConfig = embark.config.pipelineConfig;
this.isFirstBuild = true;
this.events.setCommandHandler('pipeline:build', (options, callback) => this.build(callback));
@ -104,7 +105,13 @@ class Pipeline {
}
}
});
webpackProcess.send({action: constants.pipeline.init, options: {webpackConfigName: self.webpackConfigName}});
webpackProcess.send({
action: constants.pipeline.init,
options: {
webpackConfigName: self.webpackConfigName,
pipelineConfig: self.pipelineConfig
}
});
webpackProcess.send({action: constants.pipeline.build, assets: self.assetFiles, importsList});
webpackProcess.once('result', constants.pipeline.built, (msg) => {

View File

@ -3,17 +3,7 @@ const fs = require('../../core/fs');
const ProcessWrapper = require('../../core/processes/processWrapper');
const webpack = require('webpack');
const writeFile = require('util').promisify(require('fs').writeFile);
const {errorMessage, recursiveMerge} = require('../../utils/utils');
// TODO: refactor into lib/core/config once pipeline is a proper embark module
const defaultPipelineConfig = {
typescript: false
};
const pipelineConfigPath = fs.dappPath(
require(fs.dappPath('embark.json')).config || 'config/',
'pipeline.js'
);
const {errorMessage} = require('../../utils/utils');
let webpackProcess;
@ -21,6 +11,7 @@ class WebpackProcess extends ProcessWrapper {
constructor(options) {
super(options);
this.webpackConfigName = options.webpackConfigName;
this.pipelineConfig = options.pipelineConfig;
}
async build(assets, importsList, callback) {
@ -41,17 +32,9 @@ class WebpackProcess extends ProcessWrapper {
fs.dappPath('.embark/embark-assets.json'),
JSON.stringify(assets)
);
let pipelineConfig = defaultPipelineConfig;
if (fs.existsSync(pipelineConfigPath)) {
delete require.cache[pipelineConfigPath];
pipelineConfig = recursiveMerge(
recursiveMerge(true, pipelineConfig),
require(pipelineConfigPath)
);
}
await writeFile(
fs.dappPath('.embark/embark-pipeline.json'),
JSON.stringify(pipelineConfig)
JSON.stringify(this.pipelineConfig)
);
} catch (e) {
return callback(errorMessage(e));