forked from spring-tips/vaadin-hilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.generated.js
376 lines (343 loc) · 13.2 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
* 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 HtmlWebpackPlugin = require('html-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const { InjectManifest } = require('workbox-webpack-plugin');
const { DefinePlugin } = require('webpack');
const ExtraWatchWebpackPlugin = require('extra-watch-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
// Flow plugins
const BuildStatusPlugin = require('@vaadin/build-status-plugin');
const ThemeLiveReloadPlugin = require('@vaadin/theme-live-reload-plugin');
const {
ApplicationThemePlugin,
processThemeResources,
extractThemeName,
findParentThemes
} = require('@vaadin/application-theme-plugin');
const path = require('path');
// this matches /themes/my-theme/ and is used to check css url handling and file path build.
const themePartRegex = /(\\|\/)themes\1[\s\S]*?\1/;
// the folder of app resources:
// - flow templates for classic Flow
// - client code with index.html and index.[ts/js] for CCDM
const frontendFolder = path.resolve(__dirname, 'frontend');
const frontendGeneratedFolder = path.resolve(__dirname, 'frontend/generated');
const fileNameOfTheFlowGeneratedMainEntryPoint = path.resolve(__dirname, 'target/frontend/generated-flow-imports.js');
const mavenOutputFolderForFlowBundledFiles = path.resolve(__dirname, 'target/classes/META-INF/VAADIN/webapp');
const mavenOutputFolderForResourceFiles = path.resolve(__dirname, 'target/classes/META-INF/VAADIN');
const useClientSideIndexFileForBootstrapping = true;
const clientSideIndexHTML = './index.html';
const clientSideIndexEntryPoint = path.resolve(__dirname, 'frontend', 'generated/', 'vaadin.ts');;
const pwaEnabled = true;
const offlinePath = '.';
const clientServiceWorkerEntryPoint = path.resolve(__dirname, 'target/sw');
// public path for resources, must match Flow VAADIN_BUILD
const VAADIN = 'VAADIN';
const build = 'build';
// public path for resources, must match the request used in flow to get the /build/stats.json file
const config = 'config';
const outputFolder = mavenOutputFolderForFlowBundledFiles;
const indexHtmlPath = 'index.html';
// folder for outputting vaadin-bundle and other fragments
const buildFolder = path.resolve(outputFolder, VAADIN, build);
// folder for outputting stats.json
const confFolder = path.resolve(mavenOutputFolderForResourceFiles, config);
const serviceWorkerPath = 'sw.js';
// file which is used by flow to read templates for server `@Id` binding
const statsFile = `${confFolder}/stats.json`;
// Folders in the project which can contain static assets.
const projectStaticAssetsFolders = [
path.resolve(__dirname, 'src', 'main', 'resources', 'META-INF', 'resources'),
path.resolve(__dirname, 'src', 'main', 'resources', 'static'),
frontendFolder
];
const projectStaticAssetsOutputFolder = path.resolve(__dirname, 'target/classes/META-INF/VAADIN/webapp/VAADIN/static');
// Folders in the project which can contain application themes
const themeProjectFolders = projectStaticAssetsFolders.map((folder) => path.resolve(folder, 'themes'));
const tsconfigJsonFile = path.resolve(__dirname, 'tsconfig.json');
const enableTypeScript = fs.existsSync(tsconfigJsonFile);
// Target flow-fronted auto generated to be the actual target folder
const flowFrontendFolder = path.resolve(__dirname, 'target/flow-frontend');
const statsSetViaCLI = process.argv.find((v) => v.indexOf('--stats') >= 0);
const devMode = process.argv.find((v) => v.indexOf('webpack-dev-server') >= 0);
if (!devMode) {
// make sure that build folder exists before outputting anything
const mkdirp = require('mkdirp');
mkdirp(buildFolder);
mkdirp(confFolder);
}
let stats;
// Open a connection with the Java dev-mode handler in order to finish
// webpack-dev-mode when it exits or crashes.
const watchDogPort = devMode && process.env.watchDogPort;
if (watchDogPort) {
const runWatchDog = () => {
const client = new require('net').Socket();
client.setEncoding('utf8');
client.on('error', function () {
console.log('Watchdog connection error. Terminating webpack process...');
client.destroy();
process.exit(0);
});
client.on('close', function () {
client.destroy();
runWatchDog();
});
client.connect(watchDogPort, 'localhost');
};
runWatchDog();
}
// Compute the entries that webpack have to visit
const webPackEntries = {};
if (useClientSideIndexFileForBootstrapping) {
webPackEntries.bundle = clientSideIndexEntryPoint;
const dirName = path.dirname(fileNameOfTheFlowGeneratedMainEntryPoint);
const baseName = path.basename(fileNameOfTheFlowGeneratedMainEntryPoint, '.js');
if (
fs
.readdirSync(dirName)
.filter((fileName) => !fileName.startsWith(baseName) && fileName.endsWith('.js') && fileName.includes('-')).length
) {
// if there are vaadin exported views, add a second entry
webPackEntries.export = fileNameOfTheFlowGeneratedMainEntryPoint;
}
} else {
webPackEntries.bundle = fileNameOfTheFlowGeneratedMainEntryPoint;
}
const appShellUrl = '.';
let appShellManifestEntry = undefined;
const swManifestTransform = (manifestEntries) => {
const warnings = [];
const manifest = manifestEntries;
if (useClientSideIndexFileForBootstrapping) {
// `index.html` is a special case: in contrast with the JS bundles produced by webpack
// it's not served as-is directly from the webpack output at `/index.html`.
// It goes through IndexHtmlRequestHandler and is served at `/`.
//
// TODO: calculate the revision based on the IndexHtmlRequestHandler-processed content
// of the index.html file
const indexEntryIdx = manifest.findIndex((entry) => entry.url === 'index.html');
if (indexEntryIdx !== -1) {
manifest[indexEntryIdx].url = appShellUrl;
appShellManifestEntry = manifest[indexEntryIdx];
} else {
// Index entry is only emitted on first compilation. Make sure it is cached also for incremental builds
manifest.push(appShellManifestEntry);
}
}
return { manifest, warnings };
};
const createServiceWorkerPlugin = function () {
return new InjectManifest({
swSrc: clientServiceWorkerEntryPoint,
swDest: serviceWorkerPath,
manifestTransforms: [swManifestTransform],
maximumFileSizeToCacheInBytes: 100 * 1024 * 1024,
dontCacheBustURLsMatching: /.*-[a-z0-9]{20}\.cache\.js/,
include: [
(chunk) => {
return true;
}
],
webpackCompilationPlugins: [
new DefinePlugin({
OFFLINE_PATH: JSON.stringify(offlinePath)
})
]
});
};
const flowFrontendThemesFolder = path.resolve(flowFrontendFolder, 'themes');
const themeOptions = {
devMode: devMode,
// The following matches folder 'target/flow-frontend/themes/'
// (not 'frontend/themes') for theme in JAR that is copied there
themeResourceFolder: flowFrontendThemesFolder,
themeProjectFolders: themeProjectFolders,
projectStaticAssetsOutputFolder: projectStaticAssetsOutputFolder,
frontendGeneratedFolder: frontendGeneratedFolder
};
let themeName = undefined;
let themeWatchFolders = undefined;
if (devMode) {
// Current theme name is being extracted from theme.js located in frontend
// generated folder
themeName = extractThemeName(frontendGeneratedFolder);
const parentThemePaths = findParentThemes(themeName, themeOptions);
const currentThemeFolders = [
...projectStaticAssetsFolders.map((folder) => path.resolve(folder, 'themes', themeName)),
path.resolve(flowFrontendThemesFolder, themeName)
];
// Watch the components folders for component styles update in both
// current theme and parent themes. Other folders or CSS files except
// 'styles.css' should be referenced from `styles.css` anyway, so no need
// to watch them.
themeWatchFolders = [...currentThemeFolders, ...parentThemePaths].map((themeFolder) =>
path.resolve(themeFolder, 'components')
);
}
const processThemeResourcesCallback = (logger) => processThemeResources(themeOptions, logger);
exports = {
frontendFolder: `${frontendFolder}`,
buildFolder: `${buildFolder}`,
confFolder: `${confFolder}`
};
module.exports = {
mode: 'production',
context: frontendFolder,
entry: webPackEntries,
output: {
filename: `${VAADIN}/${build}/vaadin-[name]-[contenthash].cache.js`,
path: outputFolder
},
resolve: {
// Search for import 'x/y' inside these folders, used at least for importing an application theme
modules: ['node_modules', flowFrontendFolder, ...projectStaticAssetsFolders],
extensions: [enableTypeScript && '.ts', '.js'].filter(Boolean),
alias: {
Frontend: frontendFolder
}
},
stats: devMode && !statsSetViaCLI ? 'errors-warnings' : 'normal', // Unclutter output in dev mode
devServer: {
hot: false, // disable HMR
client: false, // disable wds client as we handle reloads and errors better
// webpack-dev-server serves ./, webpack-generated, and java webapp
static: [outputFolder, path.resolve(__dirname, 'src', 'main', 'webapp')],
setupMiddlewares: function (middlewares, devServer) {
devServer.app.get(`/assetsByChunkName`, function (req, res) {
res.json(stats.assetsByChunkName);
});
devServer.app.get(`/stop`, function (req, res) {
// eslint-disable-next-line no-console
console.log("Stopped 'webpack-dev-server'");
process.exit(0);
});
return middlewares;
}
},
module: {
rules: [
enableTypeScript && {
test: /\.ts$/,
loader: 'esbuild-loader',
options: {
loader: 'ts',
target: 'es2019'
}
},
{
test: /\.css$/i,
use: [
{
loader: 'lit-css-loader',
options: {
import: 'lit'
}
},
{
loader: 'extract-loader'
},
{
loader: 'css-loader',
options: {
url: (url, resourcePath) => {
// Only translate files from node_modules
const resolve = resourcePath.match(/(\\|\/)node_modules\1/);
const themeResource = resourcePath.match(themePartRegex) && url.match(/^themes\/[\s\S]*?\//);
return resolve || themeResource;
},
// use theme-loader to also handle any imports in css files
importLoaders: 1
}
},
{
// theme-loader will change any url starting with './' to start with 'VAADIN/static' instead
// NOTE! this loader should be here so it's run before css-loader as loaders are applied Right-To-Left
loader: '@vaadin/theme-loader',
options: {
devMode: devMode
}
}
]
},
{
// File-loader only copies files used as imports in .js files or handled by css-loader
test: /\.(png|gif|jpg|jpeg|svg|eot|woff|woff2|otf|ttf)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'VAADIN/static/',
name(resourcePath, resourceQuery) {
if (resourcePath.match(/(\\|\/)node_modules\1/)) {
return /(\\|\/)node_modules\1(?!.*node_modules)([\S]+)/.exec(resourcePath)[2].replace(/\\/g, '/');
}
if (resourcePath.match(/(\\|\/)flow-frontend\1/)) {
return /(\\|\/)flow-frontend\1(?!.*flow-frontend)([\S]+)/.exec(resourcePath)[2].replace(/\\/g, '/');
}
return '[path][name].[ext]';
}
}
}
]
}
].filter(Boolean)
},
performance: {
maxEntrypointSize: 2097152, // 2MB
maxAssetSize: 2097152 // 2MB
},
plugins: [
new ApplicationThemePlugin(themeOptions),
...(devMode && themeName
? [
new ExtraWatchWebpackPlugin({
files: [],
dirs: themeWatchFolders
}),
new ThemeLiveReloadPlugin(processThemeResourcesCallback)
]
: []),
function (compiler) {
// V14 bootstrapping needs the bundle names
compiler.hooks.afterEmit.tapAsync("FlowStatsHelper", (compilation, done) => {
let miniStats = {
assetsByChunkName: compilation.getStats().toJson().assetsByChunkName
};
if (!devMode) {
fs.writeFile(statsFile, JSON.stringify(miniStats, null, 1),
() => done());
} else {
stats = miniStats;
done();
}
});
},
// Includes JS output bundles into "index.html"
useClientSideIndexFileForBootstrapping &&
new HtmlWebpackPlugin({
template: clientSideIndexHTML,
filename: indexHtmlPath,
inject: 'head',
scriptLoading: 'defer',
chunks: ['bundle']
}),
// Service worker for offline
pwaEnabled && createServiceWorkerPlugin(),
// Generate compressed bundles when not devMode
!devMode && new CompressionPlugin(),
enableTypeScript &&
new ForkTsCheckerWebpackPlugin({
typescript: {
configFile: tsconfigJsonFile
}
}),
new BuildStatusPlugin()
].filter(Boolean)
};