-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
172 lines (154 loc) · 5.2 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
165
166
167
168
169
170
171
172
// Webpack config to organize and build web frontend code
// Require modulea and plugins
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
// Set the npm runtime environment based on npm script call
// Setup the config object as function
module.exports = env => {
// Config object
let config = {};
// Entry files to be bundled
config.entry = {
custom: './src/js/index.js',
//static: './src/js/static.js'
};
// Definition and optimization of output chunks
config.optimization = {
splitChunks: {
cacheGroups: {
vendor: {
test: /node_modules/,
chunks: 'initial',
name: 'vendor',
enforce: true
},
}
}
};
// Assigning runtime environment to the mode variable
config.mode = env !== undefined && env.production ? 'production' : 'development';
console.log('NODE_ENV: ', config.mode);
// Output definition for location and filename of the bundle files
config.output = {
path: path.resolve(__dirname, 'html/resources/dist'),
filename: 'js/[name].bundle.js',
chunkFilename: 'js/[name].bundle.js'
};
// Plugin array to assign custom plgins to the bundle process
config.plugins = [];
config.plugins.push(
// Automated assignment if bundling files to an index.html -> the file can be opened in a browser for test porposes
new HtmlWebpackPlugin({
template: "./src/static/index.html",
inject: "body",
chunks: ["vendor", "custom"],
chunksSortMode: function (b, a) {
//alphabetical order to define the insert tags
if (a.names[0] > b.names[0]) {
return 1;
}
if (a.names[0] < b.names[0]) {
return -1;
}
return 0;
},
}),
new HtmlWebpackPlugin({
template: "./src/static/reflexionen.html",
inject: true,
chunks: ["vendor", "custom"],
filename: "./ausstellungen/reflexionen/index.html",
}),
new HtmlWebpackPlugin({
template: "./src/static/werkschau.html",
inject: true,
chunks: ["vendor", "custom"],
filename: "./ausstellungen/werkschau/index.html",
}),
// Write the css income stream to css bundle file(s)
new MiniCssExtractPlugin({
filename: "css/[name].bundle.css",
chunkFilename: "css/[name].bundle.css",
}),
// Plugin to copy static web content (files without reference to js or scss files)
new CopyWebpackPlugin([
{
from: path.join(__dirname, "./src/static"),
ignore: ["*.html"],
},
]),
new webpack.ProvidePlugin({
jQuery: "jquery",
$: "jquery",
jquery: "jquery",
"window.$": "jquery",
"window.jQuery": "jquery",
})
);
// Module object to keep the info how resources are being loaded
config.module = {
rules: [
{
// Rules how to process JS code
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
, {
// Rules how to process CSS/SCSS code
test: /\.(sa|sc|c)ss$/,
use: [
config.mode !== 'production' ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
],
}, {
// Rules how to process images
test: /\.(png|jpg|jpeg|gif)$/,
loader: {
loader: 'file-loader',
options: {
name: 'img/[name].[ext]',
publicPath: '/'
}
}
}, {
// Rules how to process font files
test: /\.(svg|woff|woff2|ttf|eot)$/,
loader: {
loader: 'file-loader',
options: {
name: 'font/[name].[ext]',
publicPath: '/'
}
}
}, {
// Rules how to process pure html code called by js code
test: /\.html$/,
loader: 'raw-loader'
}
]
};
// Dev tool - source maps will be used in test/dev mode
if (config.mode === 'development') {
config.devtool = 'inline-source-map';
}
// Dev server - can be used while development ($ npm run build-dev)
config.devServer = {
contentBase: [path.join(__dirname, 'html/resources/dist'), path.join(__dirname, '')],
compress: true,
port: 9000,
open: true,
}
// Return the config object
return config;
}