aboutsummaryrefslogtreecommitdiff
path: root/node_modules/webpack-serve/lib/config.js
diff options
context:
space:
mode:
authorruki <waruqi@gmail.com>2018-11-08 00:38:48 +0800
committerruki <waruqi@gmail.com>2018-11-07 21:53:09 +0800
commit26105034da4fcce7ac883c899d781f016559310d (patch)
treec459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/webpack-serve/lib/config.js
parent2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff)
downloadxmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz
xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip
switch to vuepress
Diffstat (limited to 'node_modules/webpack-serve/lib/config.js')
-rw-r--r--node_modules/webpack-serve/lib/config.js121
1 files changed, 121 insertions, 0 deletions
diff --git a/node_modules/webpack-serve/lib/config.js b/node_modules/webpack-serve/lib/config.js
new file mode 100644
index 00000000..490ce4b4
--- /dev/null
+++ b/node_modules/webpack-serve/lib/config.js
@@ -0,0 +1,121 @@
+const { isAbsolute, resolve } = require('path');
+
+const loader = require('@webpack-contrib/config-loader');
+const isPlainObject = require('lodash/isPlainObject');
+const TimeFixPlugin = require('time-fix-plugin');
+const { MultiCompiler } = require('webpack');
+
+// automagically wrap hot-client-invalid entry values in an array
+function toArray(config) {
+ const result = config;
+
+ if (typeof result.entry === 'string') {
+ result.entry = [result.entry];
+ } else if (typeof result.entry === 'undefined') {
+ // webpack v4 defaults an empty config to { entry: './src' }. but since we
+ // need an array, we'll mimic that default config.
+ result.entry = ['./src'];
+ } else if (isPlainObject(result.entry)) {
+ for (const key of Object.keys(result.entry)) {
+ const entry = result.entry[key];
+
+ if (!Array.isArray(entry)) {
+ result.entry[key] = [entry];
+ }
+ }
+ }
+
+ return result;
+}
+
+// adds https://github.com/egoist/time-fix-plugin if not already added
+// to the config.
+function timeFix(config) {
+ const result = config;
+
+ if (result.plugins) {
+ let timeFixFound = false;
+ for (const plugin of result.plugins) {
+ if (!timeFixFound && plugin instanceof TimeFixPlugin) {
+ timeFixFound = true;
+ }
+ }
+
+ if (!timeFixFound) {
+ result.plugins.unshift(new TimeFixPlugin());
+ }
+ } else {
+ result.plugins = [new TimeFixPlugin()];
+ }
+
+ return result;
+}
+
+function prepare(config) {
+ return timeFix(toArray(config));
+}
+
+module.exports = {
+ load(options) {
+ const { compiler, config } = options;
+ const flags = options.flags || {};
+ const require = flags.require || options.require;
+
+ // if someone passed us a config Object, then just resolve that object
+ // we're only going to load the config from file when we're given a file
+ // to load
+ if (isPlainObject(config) || Array.isArray(config)) {
+ const result = [].concat(config).map((conf) => prepare(conf));
+ return Promise.resolve(result);
+ }
+
+ // if a user has passed us a compiler directly, then pull the config from
+ // the compiler and use that
+ if (compiler) {
+ let configs = [];
+ if (compiler instanceof MultiCompiler) {
+ const { compilers } = compiler;
+ configs = compilers.map((c) => c.options);
+ } else {
+ configs = [compiler.options];
+ }
+
+ return Promise.resolve(configs);
+ }
+
+ const loaderOptions = {
+ allowMissing: true,
+ require,
+ schema: {
+ properties: {
+ serve: {
+ additionalProperties: true,
+ type: 'object',
+ },
+ },
+ },
+ };
+
+ if (typeof config === 'string') {
+ if (isAbsolute(config)) {
+ loaderOptions.configPath = config;
+ } else {
+ loaderOptions.configPath = resolve(process.cwd(), config);
+ }
+ }
+
+ return loader(loaderOptions).then((result) => {
+ let found = result.config;
+
+ if (!found) {
+ // webpack v4+ defaults an empty config to { entry: './src' }. but since we
+ // need an array, we'll mimic that default config.
+ found = { entry: ['./src'] };
+ }
+
+ found = [].concat(found).map((conf) => prepare(conf));
+
+ return found;
+ });
+ },
+};