embark/packages/plugins/basic-pipeline/src/webpackConfigReader.js
Eric Mastro 3f7842cf24 refactor(@embark/embarkjs): move module into own package (#1945)
**TODO:** The `embark eject-webpack` command needs to be updated. @michaelsbradleyjr - do you have suggestions as to how we could port this over? We could **assume** that the `basic-pipeline` plugin will be installed, and then read the embark config and overrides from the file system, however this feels like we are sort adding a dependency to a plugin, which is not right. Any suggestions?
2019-10-14 16:05:17 +09:00

49 lines
1.6 KiB
JavaScript

import { dappPath, errorMessage } from 'embark-utils';
const fs = require('fs-extra');
import * as path from 'path';
class WebpackConfigReader {
constructor(options) {
this.webpackConfigName = options.webpackConfigName;
}
async readConfig(callback){
const dappConfigPath = dappPath('webpack.config.js');
const defaultConfigPath = path.resolve(__dirname, 'webpack.config.js');
let config, configPath;
try {
if (fs.existsSync(dappConfigPath)) {
configPath = dappConfigPath;
delete require.cache[configPath];
} else {
configPath = defaultConfigPath;
}
config = require(configPath);
// 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') {
config = await config(this.webpackConfigName);
} else if (Array.isArray(config)) {
config = config.filter(cfg => cfg.name === this.webpackConfigName);
if (!config.length) {
return callback(`no webpack config has the name '${this.webpackConfigName}'`);
}
if (config.length > 1) {
console.warn(`detected ${config.length} webpack configs having the name '${this.webpackConfigName}', using the first one`);
}
config = config[0];
}
callback(null, config);
} catch (e) {
console.error(`error while loading webpack config ${configPath}`);
callback(errorMessage(e));
}
}
}
module.exports = WebpackConfigReader;