-
Notifications
You must be signed in to change notification settings - Fork 3
/
default-webpack.config.js
154 lines (144 loc) · 3.77 KB
/
default-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
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const extract = require("webpack-extract-module-to-global");
const TerserPlugin = require("terser-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const RemovePlugin = require('remove-files-webpack-plugin');
// temp build directory, intended to be cleaned up after successful build
const TEMP_DIR = './bld-tmp';
// convention driven require of location by the client side apps build config
let appBuildConfig = require(path.resolve(process.cwd(), '.wvr', 'build-config.js')).config;
appBuildConfig = !!appBuildConfig ? appBuildConfig : {
entry: {}
};
const {
copy, // copy patterns
entry, // webpack default entry point
} = appBuildConfig;
const patterns = [];
// prepare copy patterns to dist directory
copy.forEach(c => {
patterns.push({
from: path.resolve(c.from),
to: path.resolve('dist', c.to),
});
});
if (fs.existsSync(TEMP_DIR)) {
fs.rmSync(TEMP_DIR, { recursive: true, force: true });
}
fs.mkdirSync(TEMP_DIR);
/**
* Used to order static JavaScript from legacy Gruntfile.js matching dev index.html file.
* ES5 to ES6 limitation of plugins inability to declare order of concatenated ES5 code.
*
* @param {*} scope bundle
* @param {*} paths static assets
* @returns ordered list of static assets renamed to maintain order through the default webpack entry point
*/
const orderPaths = (scope, paths) => {
const array = [];
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
const ext = path.split('.').pop();
// es5 JavaScript requires correct order for concatenation
if (ext === 'js') {
const index = `${i}`.padStart(8, '0');
const tempFilePath = `${TEMP_DIR}/${scope}-${index}.${ext}`;
if (!fs.existsSync(path)) {
console.error(`${path} does not exist!`);
}
fs.copyFileSync(path, tempFilePath);
array.push(tempFilePath);
} else {
array.push(path);
}
}
return array;
}
// iterate over app build defined entry points
for (const bundle of Object.keys(entry)) {
const prune = [];
entry[bundle] = orderPaths(bundle, entry[bundle].filter((e => {
if (e.startsWith('!')) {
prune.push(e.substring(1));
return false;
}
return true;
})).flatMap((e) => {
return glob.sync(e);
}).filter((e) => {
return prune.indexOf(e) < 0;
}));
}
const env = process.env.NODE_ENV || 'development';
// see webpack https://webpack.js.org/configuration/
module.exports = {
mode: env,
target: ['web', 'es5'],
devtool: env === 'development' ? 'source-map' : undefined,
context: process.cwd(),
entry,
output: {
path: path.resolve(process.cwd(), 'dist'),
filename: '[name].bundle.js',
},
optimization: {
minimize: env === 'production',
minimizer: [new TerserPlugin(
{
terserOptions: {
ecma: '5',
parse: {},
compress: {},
mangle: false,
module: false
},
}
)],
},
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: extract.ExtractModuleToGlobal.loader,
},
],
},
{
test: /\.s[ac]ss$/i,
type: 'asset/resource',
generator: {
filename: 'resources/styles/app.css'
},
use: [
{
loader: 'sass-loader',
options: {
sourceMap: false,
},
},
],
},
]
},
plugins: [
new extract.ExtractModuleToGlobal(),
new CopyPlugin({
patterns,
}),
new RemovePlugin({
after: {
include: [
path.resolve(process.cwd(), TEMP_DIR)
],
log: false,
logWarning: true,
logError: true,
logDebug: false
}
})
],
}