-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.common.js
189 lines (182 loc) · 6.51 KB
/
webpack.config.common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Author: DrowsyFlesh
* Create: 2019-05-15
* Description:
*/
const path = require('path');
const glob = require('glob');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const MergeJsonWebpackPlugin = require('merge-jsons-webpack-plugin');
const WriteJsonPlugin = require('./write-json-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const srcPath = path.resolve('src');
const buildPath = path.resolve('build');
const jsPath = srcPath;
const indexFilename = 'index';
// get i18n json path for copy
const localesSupportList = require('./localesSupportList.json');
const localesGroup = localesSupportList.map((name) => ({
pattern: `{./src/**/_locales/${name}/messages.json,./src/_locales/${name}/*.json}`,
fileName: `./_locales/${name}/messages.json`,
}));
// sync version
let manifestJSON, packageJSON;
packageJSON = require('./package.json');
manifestJSON = require('./src/manifest.json');
if (process.env.npm_config_setversion) {
const version = /^([\d.]+)(?:-beta\.)?(\d+)?/.exec(process.env.npm_config_setversion);
if (version && version[1]) {
packageJSON.version = process.env.npm_config_setversion;
manifestJSON.version = `${version[1]}${version[2] ? '.' + version[2] : ''}`;
}
}
// get page entries
const pageRegExp = new RegExp(`${srcPath}\/pages\/(.+)\/index\.js`);
const pageEntryArray = glob.sync(`${srcPath}/pages/*/index.js`).map((pathname) => {
const res = pageRegExp.exec(pathname);
if (res) {
const filename = res[1];
return [`js/pages/${filename}`, path.join(jsPath, 'pages', filename, indexFilename)];
}
}).reduce((obj, {0: key, 1: value}) => Object.assign(obj, {[key]: value}), {});
// get feature entries
//const featureRegExp = new RegExp(`${srcPath}\/modules\/(.+)\/index\.js`);
//const featureEntryArray = glob.sync(`${srcPath}/modules/*/index.js`).map((pathname) => {
// const res = featureRegExp.exec(pathname);
// if (res) {
// const filename = res[1];
// return [`js/modules/${filename}`, path.join(jsPath, 'modules', filename, indexFilename)];
// }
//}).reduce((obj, {0: key, 1: value}) => Object.assign(obj, {[key]: value}), {});
// get ui entries
//const uiRegExp = new RegExp(`${srcPath}\/modules\/(.+)\/UI\/index\.js`);
//const uiEntryArray = glob.sync(`${srcPath}/modules/*/UI/index.js`).map((pathname) => {
// const res = uiRegExp.exec(pathname);
// if (res) {
// const filename = res[1];
// return [`js/ui/${filename}`, path.join(jsPath, 'modules', filename, 'UI', indexFilename)];
// }
//}).reduce((obj, {0: key, 1: value}) => Object.assign(obj, {[key]: value}), {});
module.exports = {
watch: true,
node: {
global: false,
},
devtool: 'source-map',
watchOptions: {
aggregateTimeout: 1000, // milliseconds
poll: 1000,
ignored: ['node_modules'],
},
entry: pageEntryArray,
output: {
filename: '[name].js',
path: buildPath,
chunkFilename: '[name].bundle.js',
},
optimization: {
splitChunks: {
minChunks: 2,
cacheGroups: {
vendors: {
name: 'js/vendors',
test: /[\\/](node_modules|src\/utils|src\/_locales|src\/libs|src\/components|src\/styles)[\\/]/,
chunks: 'all',
//minChunks: 1,
enforce: true,
},
//features: {
// name: 'js/features',
// test: /src\/modules\/(.+?)\/index\.js$/,
// chunks: 'all',
// //minChunks: 1,
// enforce: true,
//},
},
},
},
resolve: {
alias: {
'Libs': path.resolve(jsPath, 'libs'),
'Utils': path.resolve(jsPath, 'utils'),
'Components': path.resolve(jsPath, 'components'),
'Modules': path.resolve(jsPath, 'modules'),
'Static': path.resolve(srcPath, 'static'),
'Styles': path.resolve(srcPath, 'styles'),
},
mainFiles: ['index'],
extensions: ['.jsx', '.js', '.json', '.css', '.less', '.scss', '.sass'],
},
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
exclude: /(\/node_modules\/|\/modules\/|\.min\.js\/)/,
loader: 'eslint-loader',
options: {
emitError: true,
failOnError: true,
fix: true,
},
},
{
test: /\.js$/,
exclude: /(\/node_modules\/|\.min\.js\/)/,
include: /(\/src\/)/,
loaders: [
'babel-loader',
],
},
{
test: /\.(css|scss|sass)$/,
use: [
{loader: MiniCssExtractPlugin.loader},
{loader: 'css-loader'},
{loader: 'sass-loader'},
],
},
],
},
plugins: [
new webpack.EnvironmentPlugin(require('./env')),
new CleanWebpackPlugin([buildPath], {
verbose: false,
}),
new MiniCssExtractPlugin({
filename: path.join('styles', '[name].css'),
chunkFilename: path.join('styles', '[name].css'),
}),
new CopyWebpackPlugin([
{from: 'src/pages/**/*.html', to: '[1].html', test: /([^/]+)\/index\.html$/, flatten: true},
{from: 'src/pages/**/*.css', to: '[1].css', test: /([^/]+)\/style\.css$/, flatten: true},
{from: 'src/static', to: 'static'},
]),
new MergeJsonWebpackPlugin({
debug: true,
output: {groupBy: localesGroup},
}),
new WriteJsonPlugin({
object: manifestJSON,
path: '../src/',
filename: 'manifest.json',
pretty: true,
}),
new WriteJsonPlugin({
object: manifestJSON,
path: '/',
filename: 'manifest.json',
}),
new WriteJsonPlugin({
object: packageJSON,
path: '../',
filename: 'package.json',
pretty: true,
}),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
//new BundleAnalyzerPlugin(),
],
};