diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/@webpack-contrib/config-loader/lib/load.js | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/@webpack-contrib/config-loader/lib/load.js')
| -rw-r--r-- | node_modules/@webpack-contrib/config-loader/lib/load.js | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/node_modules/@webpack-contrib/config-loader/lib/load.js b/node_modules/@webpack-contrib/config-loader/lib/load.js new file mode 100644 index 00000000..62279036 --- /dev/null +++ b/node_modules/@webpack-contrib/config-loader/lib/load.js @@ -0,0 +1,99 @@ +const chalk = require('chalk'); +const cosmiconfig = require('cosmiconfig'); +const resolvePath = require('resolve').sync; +const webpackLog = require('webpack-log'); + +const LoadConfigError = require('./LoadConfigError'); +const RequireModuleError = require('./RequireModuleError'); + +const cwd = process.cwd(); +const { loadJs } = cosmiconfig; +const prefix = 'webpack'; +const cosmicOptions = { + loaders: { + '.es6': loadJs, + '.flow': loadJs, + '.mjs': loadJs, + '.ts': loadJs, + }, + searchPlaces: [ + `${prefix}.config.js`, + `${prefix}.config.es6`, + `${prefix}.config.flow`, + `${prefix}.config.mjs`, + `${prefix}.config.ts`, + `.${prefix}rc`, + 'package.json', + `.${prefix}rc.json`, + `.${prefix}rc.yaml`, + `.${prefix}rc.yml`, + ], +}; + +module.exports = (options = {}) => { + const log = webpackLog({ name: 'config', id: 'webpack-config-loader' }); + const requires = [].concat(options.require).filter((r) => !!r); + + // eslint-disable-next-line no-param-reassign + options = Object.assign({ cwd: process.cwd() }, options); + + for (const module of requires) { + try { + const modulePath = resolvePath(module, { basedir: cwd }); + + log.info(chalk`Requiring the {cyan ${module}} module`); + + if (options.requireOptions) { + const { requireOptions } = options; + // eslint-disable-next-line import/no-dynamic-require, global-require + require(modulePath)(requireOptions); + } else { + // eslint-disable-next-line import/no-dynamic-require, global-require + require(modulePath); + } + } catch (e) { + log.error(chalk`An error occurred while requiring: {grey ${module}}`); + throw new RequireModuleError(e, module); + } + } + + let config = {}; + let { configPath } = options; + + const explorer = cosmiconfig(prefix, cosmicOptions); + + try { + let result; + if (configPath) { + result = explorer.loadSync(configPath) || {}; + } else { + result = explorer.searchSync(options.cwd) || {}; + } + + ({ config, filepath: configPath } = result); + + log.debug(chalk`Found config at {grey ${configPath}}`); + } catch (e) { + /* istanbul ignore else */ + if (configPath) { + log.error(chalk`An error occurred while trying to load {grey ${configPath}} + Did you forget to specify a --require?`); + } else { + log.error(chalk`An error occurred while trying to find a config file + Did you forget to specify a --require?`); + } + throw new LoadConfigError(e, configPath); + } + + if (!configPath && !options.allowMissing) { + // prettier-ignore + log.error(chalk`Unable to load a config from: {grey ${options.cwd}}`); + const e = new Error(`Unable to load a config from: ${options.cwd}`); + throw new LoadConfigError(e, configPath); + } else { + config = config || {}; + configPath = configPath || ''; + } + + return { config, configPath }; +}; |
