-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
executable file
·273 lines (269 loc) · 8.93 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const path = require("path");
const WebpackBar = require("webpackbar");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const FileManagerPlugin = require("filemanager-webpack-plugin");
const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
const { extendDefaultPlugins } = require("svgo");
const { cp } = require("fs");
const sass = require("sass-embedded");
const FaviconsWebpackPlugin = require("favicons-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const BASE_PATH = path.resolve(__dirname, ".");
module.exports = (env, argv) => {
const mode = argv.mode ? argv.mode : "development";
const THEME_FOLDER = env.theme ? env.theme : "base";
const THEME_PATH = `${BASE_PATH}/${THEME_FOLDER}`;
const BUNDLE_NAME = `++theme++${THEME_FOLDER}`;
return {
mode: mode,
entry: [path.resolve(THEME_PATH, "./src/index.js")],
output: {
path: THEME_PATH,
filename: "dist/js/theme.js",
},
resolve: {
alias: {
"@theme": THEME_PATH,
},
},
plugins: [
new FileManagerPlugin({
events: {
onStart: {
delete: [`${THEME_PATH}/dist`],
},
onEnd: [
{
mkdir: [`${THEME_PATH}/tmp`, `${THEME_PATH}/tmp/${THEME_FOLDER}`],
},
{
copy: [
{
source: `${THEME_PATH}/**`,
destination: `${THEME_PATH}/tmp/${THEME_FOLDER}`,
options: {
flat: false,
preserveTimestamps: true,
overwite: true,
},
globOptions: {
ignore: [
"**/package.json",
"**/*.zip",
"**/src",
"**/node_modules",
],
},
},
{
source: `${THEME_PATH}/dist`,
destination: `${THEME_PATH}/tmp/${THEME_FOLDER}/dist`,
},
],
},
{
archive: [
{
source: `${THEME_PATH}/tmp`,
destination: `${THEME_PATH}/theme.zip`,
},
],
},
{ delete: [`${THEME_PATH}/tmp`] },
],
},
}),
new MiniCssExtractPlugin({
filename: "dist/css/theme.css",
}),
new WebpackBar(),
new FaviconsWebpackPlugin({
logo: THEME_PATH + "/icons/logo.png",
mode: "webapp",
cache: false,
outputPath: THEME_PATH + "/icons",
inject: false,
favicons: {
appName: null, // Your application's name. `string`
appShortName: null, // Your application's short_name. `string`. Optional. If not set, appName will be used
appDescription: null, // Your application's description. `string`
developerName: null, // Your (or your developer's) name. `string`
developerURL: null, // Your (or your developer's) URL. `string`
loadManifestWithCredentials: false,
icons: {
android: true, // Create Android homescreen icon. `boolean` or `{ offset, background }` or an array of sources
appleIcon: true, // Create Apple touch icons. `boolean` or `{ offset, background }` or an array of sources
appleStartup: false, // Create Apple startup images. `boolean` or `{ offset, background }` or an array of sources
favicons: true, // Create regular favicons. `boolean` or `{ offset, background }` or an array of sources
windows: false, // Create Windows 8 tile icons. `boolean` or `{ offset, background }` or an array of sources
yandex: false, // Create Yandex browser icon. `boolean` or `{ offset, background }` or an array of sources
},
},
}),
].filter(Boolean),
module: {
rules: [
{
test: /\.(js|jsx)$/i,
loader: "babel-loader",
},
{
test: /\.s[ac]ss$/i,
use: [
// In production, creates CSS files
// In development serve CSS through JS 'with style-loader'
{
loader:
mode === "development"
? "style-loader"
: MiniCssExtractPlugin.loader,
},
// Translates CSS into CommonJS
{
loader: "css-loader",
options: {
sourceMap: mode === "development",
},
},
// Use postcss to add vendor prefixes and various transforms to the css
{
loader: "postcss-loader",
options: {
sourceMap: mode === "development",
},
},
{
loader: "sass-loader",
options: {
sourceMap: mode === "development",
implementation: require("sass-embedded"),
},
},
],
},
{
test: /\.css$/i,
use: [
// In production, creates CSS files
// In development serve CSS through JS 'with style-loader'
{
loader:
mode === "development"
? "style-loader"
: MiniCssExtractPlugin.loader,
},
// Translates CSS into CommonJS
{
loader: "css-loader",
options: {
sourceMap: mode === "development",
},
},
],
},
{
// Use @svgr/webpack to import svg directly in .js files
test: /\.svg$/i,
issuer: /\.(js|mjs|jsx|ts|tsx)$/,
loader: "@svgr/webpack",
},
{
test: /\.(png|jpg|gif|jpeg|svg|webp)$/i,
type: "asset/resource",
generator: {
filename: "dist/images/[name][hash:8].[ext]",
},
},
{
test: /\.(eot|woff|woff2|ttf)([?]?.*)$/,
type: "asset/resource",
generator: {
filename: "dist/fonts/[name].[ext]",
},
},
],
},
optimization: {
usedExports: true,
minimizer: [
new CssMinimizerPlugin(),
new TerserPlugin({
parallel: true,
}),
new ImageMinimizerPlugin({
minimizer: {
implementation: ImageMinimizerPlugin.imageminMinify,
options: {
// Lossless optimization with custom option
// Feel free to experiment with options for better result for you
plugins: [
["gifsicle", { interlaced: true }],
["jpegtran", { progressive: true }],
["optipng", { optimizationLevel: 5 }],
// Svgo configuration here https://github.com/svg/svgo#configuration
[
"svgo",
{
plugins: [
{
name: "preset-default",
params: {
overrides: {
// customize default plugin options
inlineStyles: {
onlyMatchedOnce: false,
},
// or disable plugins
removeDoctype: false,
},
},
},
],
},
],
],
},
},
}),
],
},
devServer: {
port: 3000,
hot: true,
allowedHosts: "all",
liveReload: false, // Mandatory as we use hot module replacements (see `hot` above)
static: {
directory: THEME_PATH,
},
client: {
overlay: false,
},
devMiddleware: {
// This is necessary as Plone use `<script integrity=` and if we don't store the file on disk,
// Plone has no way to recompute the hash and thus the file is not executed in the browser.
// We filter out 'hot-update' files as we don't need them to be written on disk.
writeToDisk: (filePath) => {
return !/hot-update/.test(filePath);
},
},
// Proxy everything to the Plone Backend EXCEPT our bundle as
// Webpack Dev Server will serve it.
proxy: [
{
context: ["/**"],
target: "http://localhost:8080",
bypass: function (req, res, proxyOptions) {
let path = req.url;
if (path.includes(BUNDLE_NAME)) {
path = path.split(BUNDLE_NAME)[1]; // Keep only the path after our bundle name
return path;
}
return null;
},
},
],
},
};
};