-
Notifications
You must be signed in to change notification settings - Fork 34
/
config.ts
166 lines (151 loc) · 4.49 KB
/
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
import { Configuration, ProgressPlugin, Stats } from 'webpack';
import * as ts from 'typescript';
import { LicenseWebpackPlugin } from 'license-webpack-plugin';
import CircularDependencyPlugin = require('circular-dependency-plugin');
import ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
import TsConfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
const CopyWebpackPlugin = require('copy-webpack-plugin');
import { readTsConfig } from '@nrwl/workspace';
import { BuildBuilderOptions } from './types';
export const OUT_FILENAME = '[name].js';
export const OUT_CHUNK_FILENAME = '[name]-[id].js';
function getAliases(options: BuildBuilderOptions): { [key: string]: string } {
return options.fileReplacements.reduce(
(aliases, replacement) => ({
...aliases,
[replacement.replace]: replacement.with,
}),
{}
);
}
function getStatsConfig(options: BuildBuilderOptions) {
return {
hash: true,
timings: false,
cached: false,
cachedAssets: false,
chunks: true, // Required to get external modules
chunkModules: true, // Required to get external modules
modules: true,
warnings: true,
errors: true,
colors: !options.verbose && !options.statsJson,
assets: !!options.verbose,
chunkOrigins: !!options.verbose,
children: !!options.verbose,
reasons: !!options.verbose,
version: !!options.verbose,
errorDetails: !!options.verbose,
moduleTrace: !!options.verbose,
usedExports: !!options.verbose,
};
}
export function getBaseWebpackPartial(
options: BuildBuilderOptions
): Configuration {
const { options: compilerOptions } = readTsConfig(options.tsConfig);
const supportsEs2015 =
compilerOptions.target !== ts.ScriptTarget.ES3 &&
compilerOptions.target !== ts.ScriptTarget.ES5;
const mainFields = [...(supportsEs2015 ? ['es2015'] : []), 'module', 'main'];
const extensions = ['.ts', '.tsx', '.mjs', '.js', '.jsx'];
const webpackConfig: Configuration = {
entry: options.entry,
profile: true,
// devtool: options.sourceMap ? 'source-map' : false,
mode: options.optimization ? 'production' : 'development',
output: {
path: options.outputPath,
// filename: OUT_FILENAME,
// chunkFilename: OUT_CHUNK_FILENAME
},
module: {
rules: [
{
test: /\.(j|t)sx?$/,
loader: 'ts-loader',
options: {
configFile: options.tsConfig,
transpileOnly: true,
// https://github.com/TypeStrong/ts-loader/pull/685
experimentalWatchApi: true,
},
},
],
},
resolve: {
extensions,
alias: getAliases(options),
plugins: [
new TsConfigPathsPlugin({
configFile: options.tsConfig,
extensions,
mainFields,
}),
],
mainFields,
},
performance: {
hints: false,
},
plugins: [
new ForkTsCheckerWebpackPlugin({
tsconfig: options.tsConfig,
workers: options.maxWorkers || ForkTsCheckerWebpackPlugin.TWO_CPUS_FREE,
useTypescriptIncrementalApi: false,
}),
],
watch: options.watch,
watchOptions: {
poll: options.poll,
},
stats: getStatsConfig(options),
};
const extraPlugins: any[] = [];
if (options.progress) {
extraPlugins.push(new ProgressPlugin());
}
if (options.extractLicenses) {
extraPlugins.push(
new LicenseWebpackPlugin({
stats: {
errors: false,
},
perChunkOutput: false,
outputFilename: '3rdpartylicenses.txt',
})
);
}
// process asset entries
if (options.assets) {
const copyWebpackPluginPatterns = options.assets.map((asset: any) => {
return {
context: asset.input,
// Now we remove starting slash to make Webpack place it from the output root.
to: asset.output,
ignore: asset.ignore,
from: {
glob: asset.glob,
dot: true,
},
};
});
const copyWebpackPluginOptions = {
ignore: ['.gitkeep', '**/.DS_Store', '**/Thumbs.db'],
};
const copyWebpackPluginInstance = new CopyWebpackPlugin(
copyWebpackPluginPatterns,
copyWebpackPluginOptions
);
extraPlugins.push(copyWebpackPluginInstance);
}
if (options.showCircularDependencies) {
extraPlugins.push(
new CircularDependencyPlugin({
exclude: /[\\\/]node_modules[\\\/]/,
})
);
}
webpackConfig.plugins = [...webpackConfig.plugins, ...extraPlugins];
return webpackConfig;
}