UNPKG

2.9 kBJavaScriptView Raw
1const path = require('path');
2const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
3// 1.0.1
4/**
5 * @options
6 * root: optional 项目的根目录 默认为当前文件所有路径的上上级,最好还是传进来
7 * entry: Required Webpack入口模块
8 * dllList: optional dll列表 默认为空数组
9 * hostPage: optional 本地承载页,需要是一个绝对路径, 默认的挂载点为#bsMain
10 * browsers: optional 浏览器支持 默认为 > 1% 详情请看 https://github.com/ai/browserslist,
11 * moduleDirectories: optional 数组 模块查找目录,默认为 node_modules,
12 * resoveAlias: 使用Alias来方便一些模块的引入
13 * alias
14 * transformInclude:
15 * transofrmExclude:
16 * port
17 * host
18 * engines: ['react'] default
19 * moduleScope: 默认为'.', 可以通过moduleScope来指明另外的目录,如果想使用根目录,请设置 ".", 如果设置了此scope,所有的相对模板只能从此目录下导入。
20 */
21module.exports = (options = {}) => {
22 // const ASSET_PATH = process.env.asset_path || "";
23 //使用全部变量保存配置项,给loaders和plugins使用
24 const { globalObjectKey, appRoot, typeFunc } = require('./constants.js');
25
26 let { port, host, friendly, moduleScope, mode, buildProd } = (global[
27 globalObjectKey
28 ] = o = require('./helpers/parse-config')(options));
29
30 let { applyPlugins, applyRules } = options;
31
32 let plugins = require('./plugins');
33 let rules = require('./rules');
34
35 if (typeof applyPlugins === typeFunc) {
36 plugins = applyPlugins(plugins);
37 }
38
39 if (typeof applyRules === typeFunc) {
40 rules = applyRules(rules);
41 }
42 return {
43 mode,
44 context: appRoot,
45 entry: o.entry,
46 output: require('./helpers/output-name')(options.output),
47 //webpack 4的优化配置,
48 optimization: require('./helpers/optimization')(),
49 module: {
50 // 如果忘了export,就报错,而不是只级警告
51 strictExportPresence: true,
52 rules
53 },
54 plugins,
55 resolve: {
56 extensions: require('./helpers/get-resolve-extensions'),
57 // modules: ["node_modules"],
58 alias: o.alias,
59 plugins: [new ModuleScopePlugin(moduleScope)]
60 },
61 externals: options.externals,
62 devServer: Object.assign(
63 {
64 port: port,
65 host: host,
66 hot: true,
67 contentBase: path.resolve(appRoot, 'dist/'),
68 publicPath: '/',
69 headers: { 'Access-Control-Allow-Origin': '*' },
70 quiet: friendly,
71 disableHostCheck: !buildProd //当使用代理启动本地开发环境时,需要打开此配置项,让热更新的请求正常起来
72 },
73 options.devServer
74 ),
75 target: 'web',
76 node: {
77 setImmediate: false,
78 dgram: 'empty',
79 fs: 'empty',
80 net: 'empty',
81 tls: 'empty',
82 child_process: 'empty'
83 },
84 devtool: buildProd ? 'source-map' : 'cheap-source-map'
85 };
86};