-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.generated.js
144 lines (132 loc) · 4.59 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
/**
* 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 {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, 'target/classes/META-INF/VAADIN');
// 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');
mkdirp(buildFolder);
mkdirp(confFolder);
const devMode = process.argv.find(v => v.indexOf('webpack-dev-server') >= 0);
let stats;
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
},
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.toJson());
});
app.get(`/stats.hash`, function(req, res) {
res.json(stats.toJson().hash.toString());
});
app.get(`/stop`, function(req, res) {
// eslint-disable-next-line no-console
console.log("Stopped 'webpack-dev-server'");
process.exit(0);
});
}
},
module: {
rules: [
{ // 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: [
// Transpile with babel, and produce different bundles per browser
new BabelMultiTargetPlugin({
babel: {
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) => {
if (!devMode) {
// eslint-disable-next-line no-console
console.log(" Emitted " + statsFile)
fs.writeFile(statsFile, JSON.stringify(compilation.getStats().toJson(), null, 1), done);
} else {
// eslint-disable-next-line no-console
console.log(" Serving the 'stats.json' file dynamically.");
stats = compilation.getStats();
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/`
}]),
]
};