From d34428a12906af1f362ef190e1bb8bdbbff88a1f Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Thu, 4 Oct 2018 11:40:52 +0200 Subject: [PATCH] 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. --- lib/core/config.js | 39 +++++++++++++++++++++++--- lib/modules/pipeline/index.js | 9 +++++- lib/modules/pipeline/webpackProcess.js | 23 ++------------- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/lib/core/config.js b/lib/core/config.js index 513b7f51..f999e664 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -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() { diff --git a/lib/modules/pipeline/index.js b/lib/modules/pipeline/index.js index c08e9fc6..b82516ab 100644 --- a/lib/modules/pipeline/index.js +++ b/lib/modules/pipeline/index.js @@ -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) => { diff --git a/lib/modules/pipeline/webpackProcess.js b/lib/modules/pipeline/webpackProcess.js index 4303ebb4..8cd8932c 100644 --- a/lib/modules/pipeline/webpackProcess.js +++ b/lib/modules/pipeline/webpackProcess.js @@ -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));