Repository navigation
Expand file tree
/
Copy pathcommon.ts
More file actions
222 lines (194 loc) · 7.52 KB
/
Copy pathcommon.ts
File metadata and controls
222 lines (194 loc) · 7.52 KB
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
import * as webpack from 'webpack';
import * as path from 'path';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
import { NamedLazyChunksWebpackPlugin } from '../../plugins/named-lazy-chunks-webpack-plugin';
import { InsertConcatAssetsWebpackPlugin } from '../../plugins/insert-concat-assets-webpack-plugin';
import { extraEntryParser, getOutputHashFormat, AssetPattern } from './utils';
import { isDirectory } from '../../utilities/is-directory';
import { requireProjectModule } from '../../utilities/require-project-module';
import { WebpackConfigOptions } from '../webpack-config';
const ConcatPlugin = require('webpack-concat-plugin');
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
const CircularDependencyPlugin = require('circular-dependency-plugin');
const SilentError = require('silent-error');
/**
* Enumerate loaders and their dependencies from this file to let the dependency validator
* know they are used.
*
* require('source-map-loader')
* require('raw-loader')
* require('url-loader')
* require('file-loader')
* require('@angular-devkit/build-optimizer')
*/
export function getCommonConfig(wco: WebpackConfigOptions) {
const { projectRoot, buildOptions, appConfig } = wco;
const appRoot = path.resolve(projectRoot, appConfig.root);
const nodeModules = path.resolve(projectRoot, 'node_modules');
let extraPlugins: any[] = [];
let extraRules: any[] = [];
let entryPoints: { [key: string]: string[] } = {};
if (appConfig.main) {
entryPoints['main'] = [path.resolve(appRoot, appConfig.main)];
}
if (appConfig.polyfills) {
entryPoints['polyfills'] = [path.resolve(appRoot, appConfig.polyfills)];
}
// determine hashing format
const hashFormat = getOutputHashFormat(buildOptions.outputHashing);
// process global scripts
if (appConfig.scripts.length > 0) {
const globalScripts = extraEntryParser(appConfig.scripts, appRoot, 'scripts');
const globalScriptsByEntry = globalScripts
.reduce((prev: { entry: string, paths: string[], lazy: boolean }[], curr) => {
let existingEntry = prev.find((el) => el.entry === curr.entry);
if (existingEntry) {
existingEntry.paths.push(curr.path);
// All entries have to be lazy for the bundle to be lazy.
existingEntry.lazy = existingEntry.lazy && curr.lazy;
} else {
prev.push({ entry: curr.entry, paths: [curr.path], lazy: curr.lazy });
}
return prev;
}, []);
// Add a new asset for each entry.
globalScriptsByEntry.forEach((script) => {
const hash = hashFormat.chunk !== '' && !script.lazy ? '.[hash]' : '';
extraPlugins.push(new ConcatPlugin({
uglify: buildOptions.target === 'production' ? { sourceMapIncludeSources: true } : false,
sourceMap: buildOptions.sourcemaps,
name: script.entry,
// Lazy scripts don't get a hash, otherwise they can't be loaded by name.
fileName: `[name]${script.lazy ? '' : hash}.bundle.js`,
filesToConcat: script.paths
}));
});
// Insert all the assets created by ConcatPlugin in the right place in index.html.
extraPlugins.push(new InsertConcatAssetsWebpackPlugin(
globalScriptsByEntry
.filter((el) => !el.lazy)
.map((el) => el.entry)
));
}
// process asset entries
if (appConfig.assets) {
const copyWebpackPluginPatterns = appConfig.assets.map((asset: string | AssetPattern) => {
// Convert all string assets to object notation.
asset = typeof asset === 'string' ? { glob: asset } : asset;
// Add defaults.
// Input is always resolved relative to the appRoot.
asset.input = path.resolve(appRoot, asset.input || '');
asset.output = asset.output || '';
asset.glob = asset.glob || '';
// Prevent asset configurations from writing outside of the output path, except if the user
// specify a configuration flag.
// Also prevent writing outside the project path. That is not overridable.
const fullOutputPath = path.resolve(buildOptions.outputPath, asset.output);
if (!fullOutputPath.startsWith(projectRoot)) {
const message = 'An asset cannot be written to a location outside the project.';
throw new SilentError(message);
}
if (!fullOutputPath.startsWith(path.resolve(buildOptions.outputPath))) {
if (!appConfig.allowAssetsOutsideOutDir) {
const message = 'An asset cannot be written to a location outside of the output path.';
throw new SilentError(message);
}
}
// Ensure trailing slash.
if (isDirectory(path.resolve(asset.input))) {
asset.input += '/';
}
// Convert dir patterns to globs.
if (isDirectory(path.resolve(asset.input, asset.glob))) {
asset.glob = asset.glob + '/**/*';
}
return {
context: asset.input,
to: asset.output,
from: {
glob: asset.glob,
dot: true
}
};
});
const copyWebpackPluginOptions = { ignore: ['.gitkeep'] };
const copyWebpackPluginInstance = new CopyWebpackPlugin(copyWebpackPluginPatterns,
copyWebpackPluginOptions);
// Save options so we can use them in eject.
(copyWebpackPluginInstance as any)['copyWebpackPluginPatterns'] = copyWebpackPluginPatterns;
(copyWebpackPluginInstance as any)['copyWebpackPluginOptions'] = copyWebpackPluginOptions;
extraPlugins.push(copyWebpackPluginInstance);
}
if (buildOptions.progress) {
extraPlugins.push(new ProgressPlugin({ profile: buildOptions.verbose, colors: true }));
}
if (buildOptions.showCircularDependencies) {
extraPlugins.push(new CircularDependencyPlugin({
exclude: /(\\|\/)node_modules(\\|\/)/
}));
}
if (buildOptions.buildOptimizer) {
extraRules.push({
test: /\.js$/,
use: [{
loader: '@angular-devkit/build-optimizer/webpack-loader',
options: { sourceMap: buildOptions.sourcemaps }
}]
});
}
if (buildOptions.namedChunks) {
extraPlugins.push(new NamedLazyChunksWebpackPlugin());
}
// Read the tsconfig to determine if we should prefer ES2015 modules.
// Load rxjs path aliases.
// https://github.com/ReactiveX/rxjs/blob/master/doc/lettable-operators.md#build-and-treeshaking
let alias = {};
try {
const rxjsPathMappingImport = 'rxjs/_esm5/path-mapping';
const rxPaths = requireProjectModule(projectRoot, rxjsPathMappingImport);
alias = rxPaths(nodeModules);
} catch (e) { }
return {
resolve: {
extensions: ['.ts', '.js'],
modules: ['node_modules', nodeModules],
symlinks: !buildOptions.preserveSymlinks,
alias
},
resolveLoader: {
modules: [nodeModules, 'node_modules']
},
context: __dirname,
entry: entryPoints,
output: {
path: path.resolve(buildOptions.outputPath),
publicPath: buildOptions.deployUrl,
filename: `[name]${hashFormat.chunk}.bundle.js`,
chunkFilename: `[id]${hashFormat.chunk}.chunk.js`
},
module: {
rules: [
{ test: /\.html$/, loader: 'raw-loader' },
{
test: /\.(eot|svg|cur)$/,
loader: 'file-loader',
options: {
name: `[name]${hashFormat.file}.[ext]`,
limit: 10000
}
},
{
test: /\.(jpg|png|webp|gif|otf|ttf|woff|woff2|ani)$/,
loader: 'url-loader',
options: {
name: `[name]${hashFormat.file}.[ext]`,
limit: 10000
}
}
].concat(extraRules)
},
plugins: [
new webpack.NoEmitOnErrorsPlugin()
].concat(extraPlugins)
};
}