-
Notifications
You must be signed in to change notification settings - Fork 32
/
dev.config.ts
143 lines (133 loc) · 4.08 KB
/
dev.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
import baseConfigFactory, {
bootstrapEntry,
mainEntry,
packageName,
InsertScriptPlugin,
libraryName
} from './base.config';
import { WebAppManifest } from './interfaces';
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
import ServiceWorkerPlugin, {
ServiceWorkerOptions
} from '@dojo/webpack-contrib/service-worker-plugin/ServiceWorkerPlugin';
import * as fs from 'fs';
import * as path from 'path';
import webpack = require('webpack');
import BuildTimeRender from '@dojo/webpack-contrib/build-time-render/BuildTimeRender';
import ExternalLoaderPlugin from '@dojo/webpack-contrib/external-loader-plugin/ExternalLoaderPlugin';
import * as CleanWebpackPlugin from 'clean-webpack-plugin';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
const WebpackPwaManifest = require('webpack-pwa-manifest');
function webpackConfig(args: any): webpack.Configuration {
const experimental = args.experimental || {};
const isExperimentalSpeed = !!experimental.speed;
const singleBundle = args.singleBundle || isExperimentalSpeed;
const base = args.base || '/';
const basePath = process.cwd();
const config = baseConfigFactory(args);
const manifest: WebAppManifest = args.pwa && args.pwa.manifest;
const { plugins, output, module } = config;
const outputPath = path.join(output!.path!, 'dev');
const assetsDir = path.join(process.cwd(), 'assets');
const assetsDirExists = fs.existsSync(assetsDir);
const entryName = 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.plugins = [
...plugins!,
assetsDirExists && new CopyWebpackPlugin([{ from: assetsDir, to: path.join(outputPath, 'assets') }]),
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 CleanWebpackPlugin(['dev', 'info'], { root: output!.path, verbose: false })
].filter((item) => item);
if (module) {
module.rules = module.rules.map((rule) => {
if (Array.isArray(rule.use)) {
rule.use = rule.use.map((loader) => {
if (typeof loader === 'string') {
return loader;
}
const { loader: loaderName, options } = loader as webpack.RuleSetLoader;
if (loaderName === '@dojo/webpack-contrib/static-build-loader') {
if (typeof options === 'object') {
options.features = { ...(options.features || {}), 'dojo-debug': true };
}
return {
loader: loaderName,
options
};
}
return loader;
});
}
return rule;
});
}
if (args['build-time-render']) {
config.plugins.push(
new BuildTimeRender({
...args['build-time-render'],
sync: singleBundle,
entries: Object.keys(config.entry!),
basePath,
baseUrl: base,
scope: libraryName
})
);
}
config.output = {
...output,
path: outputPath
};
return config;
}
export default webpackConfig;