-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.web.js
177 lines (172 loc) · 5.32 KB
/
webpack.web.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
const _ = require('lodash')
const path = require('path')
const AssetsPlugin = require('assets-webpack-plugin')
const MomentLocalesPlugin = require('moment-locales-webpack-plugin')
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const { LOCAL_IDENT_NAME } = require('./buildOptions')
const { getJsxRule } = require('./helpers')
const VERBOSE = process.env.VERBOSE
const DEV_PORT = ~~process.env.DEV_PORT || 3010
const PROD = !process.env.WEBPACK_SERVE
const STYLES_PATH = path.join(process.cwd(), '/styles/index.styl')
const BUILD_DIR = '/build/client/'
const BUILD_PATH = path.join(process.cwd(), BUILD_DIR)
const BUNDLE_NAME = 'main'
// Turn on support of asynchronously loaded chunks (dynamic import())
// This will make a separate mini-bundle (chunk) for each npm module (from node_modules)
// and each component from the /components/ folder
const ASYNC = process.env.ASYNC
if (ASYNC) console.log('[dm-bundler] ASYNC optimization is turned ON')
const EXTENSIONS = ['.web.js', '.js', '.web.jsx', '.jsx', '.json']
const ASYNC_EXTENSIONS = EXTENSIONS.map(i => '.async' + i)
process.env.BABEL_ENV = PROD ? 'web_production' : 'web_development'
module.exports = _.pickBy({
mode: PROD ? 'production' : 'development',
entry: {
[BUNDLE_NAME]: ['@babel/polyfill', './index.web.js']
},
optimization: (PROD || ASYNC) && _.pickBy({
minimizer: PROD && [
new TerserPlugin({
cache: false,
parallel: true,
sourceMap: false // set to true if you want JS source maps
}),
new OptimizeCSSAssetsPlugin({})
],
splitChunks: ASYNC && {
maxInitialRequests: Infinity,
maxAsyncRequests: Infinity,
minSize: 0,
cacheGroups: {
vendor: {
chunks: 'async',
test: /[\\/]node_modules[\\/]/,
name (module) {
// get the name. E.g. node_modules/packageName/not/this/part.js
// or node_modules/packageName
const packageName = module.context.match(/[\\/]node_modules[\\/](@[^\\/]+[\\/][^\\/]+|[^@\\/]+)([\\/]|$)/)[1]
// npm package names are URL-safe, but some servers don't like @ symbols
return `npm.${packageName.replace('@', '').replace(/[\\/]/, '_')}`
}
},
components: {
chunks: 'async',
minChunks: 1,
test: /[\\/]components[\\/][^\\/]+[\\/]/,
name (module) {
const componentName = module.context.match(/[\\/]components[\\/](.*?)([\\/]|$)/)[1]
return `component.${componentName}`
}
}
}
}
}, Boolean),
plugins: [
!VERBOSE && !PROD && new FriendlyErrorsWebpackPlugin(),
new MomentLocalesPlugin(), // strip all locales except 'en'
PROD && new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[name].[chunkhash].css'
}),
PROD && new AssetsPlugin({
filename: 'assets.json',
fullPath: false,
path: BUILD_PATH
})
].filter(Boolean),
output: {
path: BUILD_PATH,
publicPath: PROD ? BUILD_DIR : `http://localhost:${DEV_PORT}${BUILD_DIR}`,
filename: PROD ? '[name].[chunkhash].js' : '[name].js'
},
module: {
rules: [
Object.assign(getJsxRule(), {
exclude: /node_modules/
}),
{
test: /\.(jpg|png|svg)$/,
use: {
loader: 'file-loader',
options: {
name: '[path][name].[hash].[ext]',
publicPath: '/build/client/'
}
}
},
{
test: /\.styl$/,
use: [
{
loader: PROD ? MiniCssExtractPlugin.loader : 'style-loader'
},
{
loader: 'css-loader',
options: {
modules: true,
localIdentName: LOCAL_IDENT_NAME
}
},
{
loader: 'postcss-loader',
options: {
plugins: [require('autoprefixer')()]
}
},
{
loader: 'stylus-loader',
options: {
import: [STYLES_PATH],
define: {
__WEB__: true
}
}
}
]
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [
{
loader: PROD ? MiniCssExtractPlugin.loader : 'style-loader'
},
{
loader: 'css-loader',
options: {
modules: true,
localIdentName: LOCAL_IDENT_NAME
}
}
]
},
// Vendor stylesheets
{
test: /\.css$/,
include: /node_modules/,
use: [
{
loader: PROD ? MiniCssExtractPlugin.loader : 'style-loader'
},
{
loader: 'css-loader'
}
]
}
]
},
resolve: {
alias: {
// fix warning requiring './locale': https://github.com/moment/moment/issues/1435
moment$: 'moment/moment.js',
'react-native': 'react-native-web',
'react-router-native': 'react-router-dom'
},
extensions: ASYNC ? ASYNC_EXTENSIONS.concat(EXTENSIONS) : EXTENSIONS,
mainFields: ['jsnext:main', 'browser', 'main']
}
}, Boolean)