-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
normalize.ts
133 lines (124 loc) · 3.85 KB
/
normalize.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
import { WebBuildBuilderOptions } from '../builders/build/build.impl';
import { normalize } from '@angular-devkit/core';
import { resolve, dirname, relative, basename } from 'path';
import { BuildBuilderOptions } from './types';
import { statSync } from 'fs';
import { ScriptTarget } from 'typescript';
export interface FileReplacement {
replace: string;
with: string;
}
export function normalizeBuildOptions<T extends BuildBuilderOptions>(
options: T,
root: string,
sourceRoot: string
): T {
return {
...options,
main: resolve(root, options.main),
outputPath: resolve(root, options.outputPath),
tsConfig: resolve(root, options.tsConfig),
fileReplacements: normalizeFileReplacements(root, options.fileReplacements),
assets: normalizeAssets(options.assets, root, sourceRoot),
webpackConfig: options.webpackConfig
? resolve(root, options.webpackConfig)
: options.webpackConfig
};
}
function normalizeAssets(
assets: any[],
root: string,
sourceRoot: string
): any[] {
return assets.map(asset => {
if (typeof asset === 'string') {
const assetPath = normalize(asset);
const resolvedAssetPath = resolve(root, assetPath);
const resolvedSourceRoot = resolve(root, sourceRoot);
if (!resolvedAssetPath.startsWith(resolvedSourceRoot)) {
throw new Error(
`The ${resolvedAssetPath} asset path must start with the project source root: ${sourceRoot}`
);
}
const isDirectory = statSync(resolvedAssetPath).isDirectory();
const input = isDirectory
? resolvedAssetPath
: dirname(resolvedAssetPath);
const output = relative(resolvedSourceRoot, resolve(root, input));
const glob = isDirectory ? '**/*' : basename(resolvedAssetPath);
return {
input,
output,
glob
};
} else {
if (asset.output.startsWith('..')) {
throw new Error(
'An asset cannot be written to a location outside of the output path.'
);
}
const assetPath = normalize(asset.input);
const resolvedAssetPath = resolve(root, assetPath);
return {
...asset,
input: resolvedAssetPath,
// Now we remove starting slash to make Webpack place it from the output root.
output: asset.output.replace(/^\//, '')
};
}
});
}
function normalizeFileReplacements(
root: string,
fileReplacements: FileReplacement[]
): FileReplacement[] {
return fileReplacements.map(fileReplacement => ({
replace: resolve(root, fileReplacement.replace),
with: resolve(root, fileReplacement.with)
}));
}
export function normalizeWebBuildOptions(
options: WebBuildBuilderOptions,
root: string,
sourceRoot: string
): WebBuildBuilderOptions {
return {
...normalizeBuildOptions(options, root, sourceRoot),
optimization:
typeof options.optimization !== 'object'
? {
scripts: options.optimization,
styles: options.optimization
}
: options.optimization,
sourceMap:
typeof options.sourceMap === 'object'
? options.sourceMap
: {
scripts: options.sourceMap,
styles: options.sourceMap,
hidden: false,
vendors: false
},
polyfills: options.polyfills ? resolve(root, options.polyfills) : undefined,
es2015Polyfills: options.es2015Polyfills
? resolve(root, options.es2015Polyfills)
: undefined
};
}
export function convertBuildOptions(
buildOptions: WebBuildBuilderOptions,
scriptTargetOverride: ScriptTarget
): any {
const options = buildOptions as any;
return <any>{
...options,
buildOptimizer: options.optimization,
aot: false,
scriptTargetOverride: scriptTargetOverride,
esVersionInFileName: !!scriptTargetOverride,
forkTypeChecker: false,
lazyModules: [] as string[],
assets: [] as string[]
};
}