UNPKG

5.67 kBJavaScriptView Raw
1const path = require('path');
2
3const autoprefixer = require('autoprefixer');
4const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
5const webpack = require('webpack');
6const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
7const merge = require('webpack-merge');
8const MiniCssExtractPlugin = require('mini-css-extract-plugin');
9const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
10const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
11
12const base = require('./base');
13
14module.exports = arg =>
15 merge(base(arg), {
16 bail: true,
17
18 mode: 'production',
19
20 module: {
21 rules: [
22 {
23 test: /\.jsx?$/,
24 include: arg.options.compileNodeModules
25 ? [path.resolve(arg.context, './src'), path.resolve(arg.context, './node-modules')]
26 : path.resolve(arg.context, './src'),
27 loader: 'babel-loader',
28 options: arg.options.babelrc
29 ? { babelrc: true }
30 : {
31 babelrc: false,
32 presets: [
33 [
34 require.resolve('babel-preset-env'),
35 {
36 targets: {
37 browsers: arg.options.browsers,
38 },
39 modules: false,
40 spec: true,
41 useBuiltIns: true,
42 },
43 ],
44 require.resolve('babel-preset-stage-2'),
45 require.resolve('babel-preset-react'),
46 ],
47 plugins: [require.resolve('babel-plugin-transform-decorators-legacy')],
48 },
49 },
50 {
51 test: /\.css$/,
52 oneOf: [
53 {
54 exclude: /node_modules/,
55 use: [
56 MiniCssExtractPlugin.loader,
57 {
58 loader: 'css-loader',
59 options: { modules: arg.options.cssModules, camelCase: 'only', importLoaders: 1 },
60 },
61 {
62 loader: 'postcss-loader',
63 options: {
64 ident: 'postcss',
65 plugins: () => [autoprefixer({ browsers: arg.options.browsers })],
66 },
67 },
68 ],
69 },
70 {
71 include: /node_modules/,
72 use: [
73 MiniCssExtractPlugin.loader,
74 {
75 loader: 'css-loader',
76 options: { importLoaders: 1 },
77 },
78 {
79 loader: 'postcss-loader',
80 options: {
81 ident: 'postcss',
82 plugins: () => [autoprefixer({ browsers: arg.options.browsers })],
83 },
84 },
85 ],
86 },
87 ],
88 },
89 {
90 test: /\.less$/,
91 oneOf: [
92 {
93 exclude: /node_modules/,
94 use: [
95 MiniCssExtractPlugin.loader,
96 {
97 loader: 'css-loader',
98 options: { modules: arg.options.cssModules, camelCase: 'only', importLoaders: 1 },
99 },
100 {
101 loader: 'postcss-loader',
102 options: {
103 ident: 'postcss',
104 plugins: () => [autoprefixer({ browsers: arg.options.browsers })],
105 },
106 },
107 'less-loader',
108 ],
109 },
110 {
111 include: /node_modules/,
112 use: [
113 MiniCssExtractPlugin.loader,
114 {
115 loader: 'css-loader',
116 options: { importLoaders: 1 },
117 },
118 {
119 loader: 'postcss-loader',
120 options: {
121 ident: 'postcss',
122 plugins: () => [autoprefixer({ browsers: arg.options.browsers })],
123 },
124 },
125 'less-loader',
126 ],
127 },
128 ],
129 },
130 ],
131 },
132
133 optimization: {
134 minimizer: [
135 new UglifyJsPlugin({
136 cache: true,
137 parallel: true,
138 sourceMap: true,
139 uglifyOptions: {
140 ie8: arg.options.supportIE8,
141 warnings: true,
142 compress: {
143 warnings: false,
144 // Disabled because of an issue with Uglify breaking seemingly valid code:
145 // https://github.com/facebookincubator/create-react-app/issues/2376
146 // Pending further investigation:
147 // https://github.com/mishoo/UglifyJS2/issues/2011
148 comparisons: false,
149 },
150 mangle: {
151 safari10: true,
152 },
153 output: {
154 comments: false,
155 // Turned on because emoji and regex is not minified properly using default
156 // https://github.com/facebookincubator/create-react-app/issues/2488
157 ascii_only: true,
158 },
159 },
160 }),
161 new OptimizeCSSAssetsPlugin({}),
162 ],
163 },
164
165 plugins: [
166 new BundleAnalyzerPlugin({
167 analyzerMode: 'static',
168 logLevel: 'warn',
169 reportFilename: 'bundle-report.html',
170 }),
171 new CaseSensitivePathsPlugin(),
172 new MiniCssExtractPlugin(),
173 new webpack.DefinePlugin({
174 __ENV__: JSON.stringify('prod'),
175 }),
176 new webpack.ProgressPlugin(),
177 arg.options.profile && new webpack.debug.ProfilingPlugin(),
178 ].filter(Boolean),
179 });