-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebpack.config.js
164 lines (147 loc) · 4.59 KB
/
webpack.config.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
'use strict';
var path = require('path');
var webpack = require('webpack');
var ManifestPlugin = require('webpack-manifest-plugin');
var autoprefixer = require('autoprefixer');
var CompressionPlugin = require("compression-webpack-plugin");
var host = process.env.HOST || 'localhost'
var devServerPort = 3808;
var production = process.env.NODE_ENV === 'production';
const ExtractCssChunks = require("extract-css-chunks-webpack-plugin")
class CleanUpExtractCssChunks {
shouldPickStatChild(child) {
return child.name.indexOf('extract-css-chunks-webpack-plugin') !== 0;
}
apply(compiler) {
compiler.hooks.done.tap('CleanUpExtractCssChunks', (stats) => {
const children = stats.compilation.children;
if (Array.isArray(children)) {
// eslint-disable-next-line no-param-reassign
stats.compilation.children = children
.filter(child => this.shouldPickStatChild(child));
}
});
}
}
var config = {
//stats: { children: false },
mode: production ? "production" : "development",
entry: {
// Sources are expected to live in $app_root/webpack
application: 'application.es6',
},
module: {
rules: [
{ test: /\.es6$/, use: "babel-loader" },
{ test: /\.jsx$/, use: "babel-loader" },
//{ test: /react-select\/src/, use: "babel-loader" },
{ test: /\.(jpe?g|png|gif)$/i, use: "file-loader" },
{
test: /\.woff($|\?)|\.woff2($|\?)|\.ttf($|\?)|\.eot($|\?)|\.svg($|\?)|\.otf($|\?)/,
//use: production ? 'file-loader' : 'url-loader'
use: 'file-loader'
},
{
test: /\.(sass|scss|css)$/,
use: [
{
loader: ExtractCssChunks.loader,
options: {
hot: production ? false : true,
// Force reload all
//reloadAll: true,
}
},
{
loader: "css-loader",
options: {
//minimize: true,
sourceMap: true
}
},
{
loader: "sass-loader"
}
]
},
]
},
output: {
// Build assets directly in to public/webpack/, let webpack know
// that all webpacked assets start with webpack/
// must match config.webpack.output_dir
path: path.join(__dirname, "..", 'public', 'webpack'),
publicPath: '/webpack/',
filename: production ? '[name]-[chunkhash].js' : '[name].js',
chunkFilename: production ? '[name]-[chunkhash].js' : '[name].js',
},
resolve: {
modules: [path.resolve(__dirname, "..", "webpack"), path.resolve(__dirname, "..", "node_modules")],
extensions: [".es6", ".jsx", ".sass", ".css", ".js"],
alias: {
'~': path.resolve(__dirname, "..", "webpack"),
}
},
plugins: [
new ExtractCssChunks(
{
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: production ? "[name]-[chunkhash].css" : "[name].css",
chunkfilename: production ? "[name]-[id].css" : "[name].css",
}
),
new CleanUpExtractCssChunks(),
new ManifestPlugin({
writeToFileEmit: true,
//basePath: "",
publicPath: production ? "/webpack/" : 'http://' + host + ':' + devServerPort + '/webpack/',
}),
//new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /ru|en/),
],
optimization: {
minimize: production,
splitChunks: {
cacheGroups: {
default: false,
vendors: {
test: /[\\/]node_modules[\\/].*js/,
priority: 1,
name: "vendor",
chunks: "initial",
enforce: true
},
},
},
}
};
if (production) {
config.plugins.push(
//new webpack.NoEmitOnErrorsPlugin(),
new webpack.DefinePlugin({ // <--key to reduce React's size
'process.env': { NODE_ENV: JSON.stringify('production') }
}),
new CompressionPlugin({
//asset: "[path].gz",
algorithm: "gzip",
test: /\.js$|\.css$/,
threshold: 4096,
minRatio: 0.8
})
);
config.output.publicPath = '/webpack/';
} else {
config.plugins.push(
new webpack.NamedModulesPlugin(),
)
config.devServer = {
port: devServerPort,
disableHostCheck: true,
headers: { 'Access-Control-Allow-Origin': '*' },
};
config.output.publicPath = 'http://' + host + ':' + devServerPort + '/webpack/';
// Source maps
config.devtool = 'source-map';
}
module.exports = config