-
Notifications
You must be signed in to change notification settings - Fork 13
/
webpack.config.js
290 lines (280 loc) · 11.7 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* global module, __dirname, require */
// FIXME
// -- Sur le plugin JsDocWebPackPlugin, on force la version en 0.0.1 car la
// version 0.0.2 semble buggée !?
// -- On ne supprime pas le logger lors de la minification, mais on le desactive...
// -- SourceMap ne fonctionne plus !?
// -- modules
var path = require("path");
var fs = require("fs");
var header = require("string-template");
var glob = require("glob");
var webpack = require("webpack");
// -- plugins
var BannerWebPackPlugin = webpack.BannerPlugin;
var EnvWebPackPlugin = webpack.EnvironmentPlugin;
var TerserJsWebPackPlugin = require("terser-webpack-plugin");
var JsDocWebPackPlugin = require("jsdoc-webpack-plugin");
var CleanWebpackPlugin = require("clean-webpack-plugin");
var ReplaceWebpackPlugin = require("replace-bundle-webpack-plugin");
var ShellWebpackPlugin = require("webpack-shell-plugin");
var HandlebarsPlugin = require("./scripts/webpackPlugins/handlebars-plugin");
var HandlebarsLayoutPlugin = require("handlebars-layouts");
var CopyWebpackPlugin = require("copy-webpack-plugin");
// -- performances
var SpeedMeasurePlugin = require("speed-measure-webpack-plugin");
var smp = new SpeedMeasurePlugin();
// -- variables
var licence = path.join(__dirname, "utils", "licence.tmpl");
var date = require(path.join(__dirname, "package.json")).date;
var version = require(path.join(__dirname, "package.json")).version;
module.exports = (env, argv) => {
// -- options : minification du bundle
// ex. webpack --mode=production
var production = (argv.mode === "production") ? true : false;
// -- options : sourcemap
// ex. webpack --mode=development
// sinon, mode none cad source sans optimisation
var development = (argv.mode === "development") ? true : false;
// -- options : nettoyage des répertoires temporaires
// ex. webpack --env.clean
// par defaut, false.
var clean = (env) ? env.clean : false;
return smp.wrap({
entry : [
path.join(__dirname, "src", "Gp")
],
output : {
path : path.join(__dirname, "dist"),
filename : (production) ? "GpServices.js" : (development) ? "GpServices-map.js" : "GpServices-src.js",
library : "Gp",
libraryTarget : "umd",
libraryExport : "default",
umdNamedDefine : true,
globalObject: 'this'
},
// utilisation des lib pour nodeJs uniquement
externals : {
"node-fetch" : {
commonjs2 : "node-fetch",
commonjs : "node-fetch",
amd : "require"
},
xmldom : {
commonjs2 : "xmldom",
commonjs : "xmldom",
amd : "require"
}
},
devtool : (development) ? "eval-source-map" : false,
stats : "none", // "verbose"
optimization : {
/** MINIFICATION */
minimizer: [
new TerserJsWebPackPlugin({
extractComments: false,
terserOptions: {
output: {
// FIXME avec les banner !
comments: "some",
// drop_console: true
},
mangle: true
}
})
]
},
module : {
rules : [
{
/**
* transpilation avec babel des sources.
* (on exclut les dependances...)
*/
test : /\.js$/,
include : [
path.join(__dirname, "src")
],
exclude : /node_modules/,
use : {
loader : "babel-loader",
options : {
compact : false,
presets : [[
"@babel/preset-env", {
// "useBuiltIns": "usage",
"corejs": { version: '3.6', proposals: false },
"debug": false
// "loose": true,
// "exclude": ['transform-typeof-symbol'],
// "targets": {
// "ie" : "10"
// }
}
]]
}
}
},
{
/**
* controle des JS en mode warning.
* (on exclut les dependances)
*/
test : /\.js$/,
enforce : "pre",
include : path.join(__dirname, "src"),
exclude : /node_modules/,
use : [
{
loader : "eslint-loader",
options : {
emitWarning : true
}
}
]
}
]
},
plugins : []
/** NETTOYAGE DES REPERTOIRES TEMPORAIRES */
.concat(
(clean) ? [
new CleanWebpackPlugin([
"dist",
"jsdoc",
"samples",
"tests"
], {
verbose : true
})] : []
)
/** EXECUTION TESTS UNITAIRES */
/** REPLACEMENT DE VALEURS */
/** GESTION DU LOGGER */
/** GENERATION DE LA JSDOC */
/** AJOUT DE LA LICENCE */
/** HANDLEBARS TEMPLATES */
/** TEMPLATES INDEX */
/** RESOURCES COPY FOR SAMPLES */
.concat(
new ShellWebpackPlugin({
onBuildExit : [],
onBuildStart : [/* "npm run test" */],
onBuildEnd : [],
safe : true
}),
new EnvWebPackPlugin({
VERBOSE : development
}),
// new ReplaceWebpackPlugin(
// [
// // {
// // partten : /__VERSION__/g,
// // /** replacement de la clef __VERSION__ par la version du package */
// // replacement : function () {
// // return version;
// // }
// // },
// // {
// // partten : /__DATE__/g,
// // /** replacement de la clef __DATE__ par la date du build */
// // replacement : function () {
// // return date;
// // }
// // },
// {
// partten : /__PRODUCTION__/g,
// replacement : function () {
// /** replacement de la clef __PRODUCTION__ pour le LOGGER */
// return production;
// }
// }
// ]
// ),
// new DefineWebpackPlugin({
// __PRODUCTION__ : JSON.stringify(production)
// }),
new JsDocWebPackPlugin({
conf : path.join(__dirname, "jsdoc.json")
}),
new BannerWebPackPlugin({
banner : header(fs.readFileSync(licence, "utf8"), {
__DATE__ : date,
__VERSION__ : version
}),
raw : true,
entryOnly : true
}),
new HandlebarsPlugin(
{
entry : {
path : path.join(__dirname, "samples-src", "pages"),
pattern : "**/pages-*.html"
},
output : {
path : path.join(__dirname, "samples"),
flatten : false,
filename : (production) ? "[name].html" : (development) ? "[name]-map.html" : "[name]-src.html"
},
helpers : [
HandlebarsLayoutPlugin
],
partials : [
path.join(__dirname, "samples-src", "templates", "*.hbs"),
path.join(__dirname, "samples-src", "templates", "partials", "*.hbs")
],
context : [
path.join(__dirname, "samples-src", "config.json"),
{
mode : (production) ? "" : (development) ? "-map" : "-src"
}
]
}
),
new HandlebarsPlugin(
{
entry : path.join(__dirname, "samples-src", "pages", "index.html"),
output : {
path : path.join(__dirname, "samples"),
filename : (production) ? "[name]-prod.html" : (development) ? "[name]-map.html" : "[name]-src.html"
},
context : {
samples : () => {
var root = path.join(__dirname, "samples-src", "pages");
var list = glob.sync(path.join(root, "**", "pages-*.html"));
list = list.map(function (filePath) {
var relativePath = path.relative(root, filePath);
var label = relativePath.replace("/", " -- ");
var pathObj = path.parse(relativePath);
return {
filePath : path.join(pathObj.dir, pathObj.name.concat((production) ? "" : (development) ? "-map" : "-src").concat(pathObj.ext)),
label : label
};
});
return list;
}
}
}
),
new CopyWebpackPlugin([
{
from : path.join(__dirname, "samples-src", "resources", "**/*"),
to : path.join(__dirname, "samples", "resources"),
context : path.join(__dirname, "samples-src", "resources")
},
{
from : path.join(__dirname, "samples-src", "pages-nodejs", "**/*.js"),
to : path.join(__dirname, "samples", "NodeJS"),
context : path.join(__dirname, "samples-src", "pages-nodejs")
}
])
)
/** DEVTOOL */
// .concat(
// (!production) ? [
// new SourceMapDevToolWebpackPlugin({
// filename: 'GpServices-src.js.map'
// })] : []
// )
});
};