-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.babel.js
128 lines (105 loc) · 2.72 KB
/
webpack.config.babel.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
import { join, resolve } from 'path';
import webpack from 'webpack';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import CompressionPlugin from 'compression-webpack-plugin';
const isHot = process.argv.indexOf('serve') !== -1; // detect webpack serve
const isDev = process.argv.indexOf('development') !== -1; // detect --mode development
console.info(`Webpack ${isHot ? 'serve HMR server' : 'BUILDING output'} in ${isDev ? 'DEVELOPMENT' : 'PRODUCTION'} mode`)
const app = {
context: resolve('.'),
entry: {
app: "./src/app.js"
},
output: {
path: resolve(__dirname, "./public/"),
publicPath: isHot ? "http://localhost:5000/" : "/",
filename: isDev ? '[name].js' : '[name].[chunkhash].js',
chunkFilename: isDev ? '[name].js' : '[name].[chunkhash].js'
},
resolve: {
modules: [resolve(__dirname, 'src'), 'node_modules']
},
devServer: {
compress: true,
host: "localhost",
allowedHosts: 'all',
headers: { 'Access-Control-Allow-Origin': '*' },
hot: true,
port: 5000,
},
module: {
rules: [
// JS
{
test: /\.js$/,
exclude: /(node_modules)/,
use: { loader: "babel-loader" },
},
// Styles
{
test: /\.(scss|css)$/,
use: isHot ? ["style-loader", "css-loader", "postcss-loader", "sass-loader"] : [MiniCssExtractPlugin.loader, "css-loader", "postcss-loader", "sass-loader"]
},
// Angular templates
{
test: /\.html$/,
use: {
loader: 'file-loader',
options: { name: isDev ? 'templates/[name].[ext]' : 'templates/[name].[hash:8].[ext]', esModule: false }
}
},
// images & fonts loader
{
test: /\.(jpe?g|png|gif|webp|eot|ttf|woff|woff2|svg|)$/i,
use: [
{ loader: "url-loader", options: { limit: 1000, name: "assets/[name].[hash].[ext]" } }
]
}
]
},
plugins: [
// HTML outputs...
new HtmlWebpackPlugin({
inject: "head",
filename: 'index.html',
template: "!!raw-loader!./src/index.html"
}
),
// BC: import jQuery to old plugins... ¯\_(ツ)_/¯
new webpack.ProvidePlugin({
"$": "jquery",
"jquery": "jquery",
"jQuery": "jquery",
"window.$": "jquery",
"window.jQuery": "jquery" // Angular 1.+
}),
]
.concat(isHot ? [] : [
// Extract CSS to file when is not HMR mode...
new MiniCssExtractPlugin({
filename: isDev ? 'css/[name].css' : 'css/[name].[contenthash].css',
chunkFilename: 'css/[id].css'
})
])
.concat(
isDev ? [] : [
// gzip results
new CompressionPlugin({
test: /\.(js|css|html)$/
}),
]
),
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'commons',
chunks: 'all',
},
},
},
},
};
module.exports = app;