/
dist.config.ts
169 lines (157 loc) · 5.09 KB
/
dist.config.ts
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
import BuildTimeRender from '@dojo/webpack-contrib/build-time-render/BuildTimeRender';
import ExternalLoaderPlugin from '@dojo/webpack-contrib/external-loader-plugin/ExternalLoaderPlugin';
import BundleAnalyzerPlugin from '@dojo/webpack-contrib/webpack-bundle-analyzer/BundleAnalyzerPlugin';
import ServiceWorkerPlugin, {
ServiceWorkerOptions
} from '@dojo/webpack-contrib/service-worker-plugin/ServiceWorkerPlugin';
import * as CleanWebpackPlugin from 'clean-webpack-plugin';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
import * as fs from 'fs';
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
import * as MiniCssExtractPlugin from 'mini-css-extract-plugin';
import * as path from 'path';
import * as webpack from 'webpack';
import baseConfigFactory, {
bootstrapEntry,
mainEntry,
InsertScriptPlugin,
packageName,
libraryName
} from './base.config';
import { WebAppManifest } from './interfaces';
const BrotliPlugin = require('brotli-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const WebpackPwaManifest = require('webpack-pwa-manifest');
const banner = `
[Dojo](https://dojo.io/)
Copyright [JS Foundation](https://js.foundation/) & contributors
[New BSD license](https://github.com/dojo/meta/blob/master/LICENSE)
All rights reserved
`;
function webpackConfig(args: any): webpack.Configuration {
const basePath = process.cwd();
const base = args.base || '/';
const config = baseConfigFactory(args);
const manifest: WebAppManifest = args.pwa && args.pwa.manifest;
const { plugins, output } = config;
const outputPath = path.join(output!.path!, 'dist');
const assetsDir = path.join(process.cwd(), 'assets');
const assetsDirExists = fs.existsSync(assetsDir);
const entryName = args.singleBundle ? mainEntry : bootstrapEntry;
let serviceWorkerOptions: ServiceWorkerOptions | undefined;
if (args.pwa && args.pwa.serviceWorker) {
serviceWorkerOptions =
typeof args.pwa.serviceWorker === 'string'
? args.pwa.serviceWorker
: { cachePrefix: packageName, ...args.pwa.serviceWorker };
}
config.mode = 'production';
config.optimization = {
...config.optimization,
namedChunks: true,
minimizer: [new TerserPlugin({ sourceMap: true, cache: true })],
flagIncludedChunks: false
};
config.plugins = [
...plugins!,
assetsDirExists && new CopyWebpackPlugin([{ from: assetsDir, to: path.join(outputPath, 'assets') }]),
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
generateStatsFile: true,
reportFilename: '../info/report.html',
statsFilename: '../info/stats.json'
}),
new HtmlWebpackPlugin({
base,
inject: true,
chunks: [entryName],
meta: manifest ? { 'mobile-web-app-capable': 'yes' } : {},
template: 'src/index.html',
cache: false
}),
args.externals &&
args.externals.dependencies &&
new ExternalLoaderPlugin({
dependencies: args.externals.dependencies,
hash: true,
outputPath: args.externals.outputPath
}),
manifest &&
new WebpackPwaManifest({
...manifest,
ios: true,
icons: Array.isArray(manifest.icons)
? manifest.icons.map((icon) => ({ ...icon, ios: true }))
: manifest.icons
}),
new InsertScriptPlugin([
{ content: `<base href="${base}">`, type: 'prepend' },
{
content: `<script>
if (!window['${libraryName}']) {
window['${libraryName}'] = {}
}
window['${libraryName}'].base = '${base}'</script>`,
type: 'append'
}
]),
serviceWorkerOptions && new ServiceWorkerPlugin(serviceWorkerOptions),
serviceWorkerOptions &&
new InsertScriptPlugin({
content: `<script>
if ('serviceWorker' in window.navigator) {
window.addEventListener('load', function() {
window.navigator.serviceWorker.register('service-worker.js');
});
}
</script>`,
type: 'append'
}),
new webpack.BannerPlugin({
banner,
test: /^.*\.js$/i
}),
new CleanWebpackPlugin(['dist', 'info'], { root: output!.path, verbose: false })
].filter((item) => item);
if (args['build-time-render']) {
config.plugins.push(
new BuildTimeRender({
...args['build-time-render'],
entries: Object.keys(config.entry!),
sync: args.singleBundle,
basePath,
baseUrl: base,
scope: libraryName
})
);
}
config.plugins = config.plugins.map((plugin) => {
if (plugin instanceof MiniCssExtractPlugin) {
return new MiniCssExtractPlugin({
filename: args.omitHash ? '[name].bundle.css' : '[name].[contenthash].bundle.css'
});
}
return plugin;
});
if (Array.isArray(args.compression)) {
const compressionPlugins: any = {
gzip: CompressionPlugin,
brotli: BrotliPlugin
};
args.compression.forEach((algorithm: 'brotli' | 'gzip') => {
const options = { algorithm, test: /\.(js|css|html|svg)$/ };
const Plugin = compressionPlugins[algorithm];
config.plugins!.push(new Plugin(options));
});
}
config.output = {
...output,
path: outputPath,
chunkFilename: args.omitHash ? '[name].bundle.js' : '[name].[chunkhash].bundle.js',
filename: args.omitHash ? '[name].bundle.js' : '[name].[chunkhash].bundle.js'
};
return config;
}
export default webpackConfig;