-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.generated.js
287 lines (255 loc) · 8.99 KB
/
webpack.generated.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
/**
* NOTICE: this is an auto-generated file
*
* This file has been generated by the `flow:prepare-frontend` maven goal.
* This file will be overwritten on every run. Any custom changes should be made to webpack.config.js
*/
const fs = require('fs');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const {BabelMultiTargetPlugin} = require('webpack-babel-multi-target-plugin');
const path = require('path');
const baseDir = path.resolve(__dirname);
// the folder of app resources (main.js and flow templates)
const frontendFolder = require('path').resolve(__dirname, 'frontend');
const fileNameOfTheFlowGeneratedMainEntryPoint = require('path').resolve(__dirname, 'target/frontend/generated-flow-imports.js');
const mavenOutputFolderForFlowBundledFiles = require('path').resolve(__dirname, '');
// public path for resources, must match Flow VAADIN_BUILD
const build = 'build';
// public path for resources, must match the request used in flow to get the /build/stats.json file
const config = 'config';
// folder for outputting index.js bundle, etc.
const buildFolder = `${mavenOutputFolderForFlowBundledFiles}/${build}`;
// folder for outputting stats.json
const confFolder = `${mavenOutputFolderForFlowBundledFiles}/${config}`;
// file which is used by flow to read templates for server `@Id` binding
const statsFile = `${confFolder}/stats.json`;
// make sure that build folder exists before outputting anything
const mkdirp = require('mkdirp');
const devMode = process.argv.find(v => v.indexOf('webpack-dev-server') >= 0);
!devMode && mkdirp(buildFolder);
mkdirp(confFolder);
let stats;
const watchDogPrefix = '--watchDogPort=';
let watchDogPort = process.argv.find(v => v.indexOf(watchDogPrefix) >= 0);
if (watchDogPort){
watchDogPort = watchDogPort.substr(watchDogPrefix.length);
}
const transpile = !devMode || process.argv.find(v => v.indexOf('--transpile-es5') >= 0);
const net = require('net');
function setupWatchDog(){
var client = new net.Socket();
client.connect(watchDogPort, 'localhost');
client.on('error', function(){
console.log("Watchdog connection error. Terminating webpack process...");
client.destroy();
process.exit(0);
});
client.on('close', function() {
client.destroy();
setupWatchDog();
});
}
if (watchDogPort){
setupWatchDog();
}
exports = {
frontendFolder: `${frontendFolder}`,
buildFolder: `${buildFolder}`,
confFolder: `${confFolder}`
};
module.exports = {
mode: 'production',
context: frontendFolder,
entry: {
bundle: fileNameOfTheFlowGeneratedMainEntryPoint
},
output: {
filename: `${build}/vaadin-[name]-[contenthash].cache.js`,
path: mavenOutputFolderForFlowBundledFiles,
publicPath: 'VAADIN/',
},
resolve: {
alias: {
Frontend: frontendFolder
}
},
devServer: {
// webpack-dev-server serves ./ , webpack-generated, and java webapp
contentBase: [mavenOutputFolderForFlowBundledFiles, 'src/main/webapp'],
after: function(app, server) {
app.get(`/stats.json`, function(req, res) {
res.json(stats);
});
app.get(`/stats.hash`, function(req, res) {
res.json(stats.hash.toString());
});
app.get(`/assetsByChunkName`, function(req, res) {
res.json(stats.assetsByChunkName);
});
app.get(`/stop`, function(req, res) {
// eslint-disable-next-line no-console
console.log("Stopped 'webpack-dev-server'");
process.exit(0);
});
}
},
module: {
rules: [
...(transpile ? [{ // Files that Babel has to transpile
test: /\.js$/,
use: [BabelMultiTargetPlugin.loader()]
}] : []),
{
test: /\.css$/i,
use: ['raw-loader']
}
]
},
performance: {
maxEntrypointSize: 2097152, // 2MB
maxAssetSize: 2097152 // 2MB
},
plugins: [
// Generate compressed bundles when not devMode
...(devMode ? [] : [new CompressionPlugin()]),
// Transpile with babel, and produce different bundles per browser
...(transpile ? [new BabelMultiTargetPlugin({
babel: {
plugins: [
// workaround for Safari 10 scope issue (https://bugs.webkit.org/show_bug.cgi?id=159270)
"@babel/plugin-transform-block-scoping",
// Edge does not support spread '...' syntax in object literals (#7321)
"@babel/plugin-proposal-object-rest-spread"
],
presetOptions: {
useBuiltIns: false // polyfills are provided from webcomponents-loader.js
}
},
targets: {
'es6': { // Evergreen browsers
browsers: [
// It guarantees that babel outputs pure es6 in bundle and in stats.json
// In the case of browsers no supporting certain feature it will be
// covered by the webcomponents-loader.js
'last 1 Chrome major versions'
],
},
'es5': { // IE11
browsers: [
'ie 11'
],
tagAssetsWithKey: true, // append a suffix to the file name
}
}
})] : []),
// Generates the stats file for flow `@Id` binding.
function (compiler) {
compiler.hooks.afterEmit.tapAsync("FlowIdPlugin", (compilation, done) => {
let statsJson = compilation.getStats().toJson();
// Get bundles as accepted keys (except any es5 bundle)
let acceptedKeys = statsJson.assets.filter(asset => asset.chunks.length > 0 && !asset.chunkNames.toString().includes("es5"))
.map(asset => asset.chunks).reduce((acc, val) => acc.concat(val), []);
// Collect all modules for the given keys
const modules = collectModules(statsJson, acceptedKeys);
// Collect accepted chunks and their modules
const chunks = collectChunks(statsJson, acceptedKeys);
let customStats = {
hash: statsJson.hash,
assetsByChunkName: statsJson.assetsByChunkName,
chunks: chunks,
modules: modules
};
if (!devMode) {
// eslint-disable-next-line no-console
console.log(" Emitted " + statsFile);
fs.writeFile(statsFile, JSON.stringify(customStats, null, 1), done);
} else {
// eslint-disable-next-line no-console
console.log(" Serving the 'stats.json' file dynamically.");
stats = customStats;
done();
}
});
},
// Copy webcomponents polyfills. They are not bundled because they
// have its own loader based on browser quirks.
new CopyWebpackPlugin([{
from: `${baseDir}/node_modules/@webcomponents/webcomponentsjs`,
to: `${build}/webcomponentsjs/`
}]),
]
};
/**
* Collect chunk data for accepted chunk ids.
* @param statsJson full stats.json content
* @param acceptedKeys chunk ids that are accepted
* @returns slimmed down chunks
*/
function collectChunks(statsJson, acceptedChunks) {
const chunks = [];
// only handle chunks if they exist for stats
if (statsJson.chunks) {
statsJson.chunks.forEach(function (chunk) {
// Acc chunk if chunk id is in accepted chunks
if (acceptedChunks.includes(chunk.id)) {
const modules = [];
// Add all modules for chunk as slimmed down modules
chunk.modules.forEach(function (module) {
const slimModule = {
id: module.id,
name: module.name,
source: module.source,
};
modules.push(slimModule);
});
const slimChunk = {
id: chunk.id,
names: chunk.names,
files: chunk.files,
hash: chunk.hash,
modules: modules
}
chunks.push(slimChunk);
}
});
}
return chunks;
}
/**
* Collect all modules that are for a chunk in acceptedChunks.
* @param statsJson full stats.json
* @param acceptedChunks chunk names that are accepted for modules
* @returns slimmed down modules
*/
function collectModules(statsJson, acceptedChunks) {
let modules = [];
// skip if no modules defined
if (statsJson.modules) {
statsJson.modules.forEach(function (module) {
// Add module if module chunks contain an accepted chunk and the module is generated-flow-imports.js module
if (module.chunks.filter(key => acceptedChunks.includes(key)).length > 0
&& (module.name.includes("generated-flow-imports.js") || module.name.includes("generated-flow-imports-fallback.js"))) {
let subModules = [];
// Create sub modules only if they are available
if (module.modules) {
module.modules.filter(module => !module.name.includes("es5")).forEach(function (module) {
const subModule = {
name: module.name,
source: module.source
};
subModules.push(subModule);
});
}
const slimModule = {
id: module.id,
name: module.name,
source: module.source,
modules: subModules
};
modules.push(slimModule);
}
});
}
return modules;
}