-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathnx-app-webpack-plugin.ts
57 lines (50 loc) · 1.85 KB
/
nx-app-webpack-plugin.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
import { Compiler } from 'webpack';
import {
NormalizedNxAppWebpackPluginOptions,
NxAppWebpackPluginOptions,
} from './nx-app-webpack-plugin-options';
import { normalizeOptions } from './lib/normalize-options';
import { deleteOutputDir } from '../../utils/fs';
import { applyBaseConfig } from './lib/apply-base-config';
import { applyWebConfig } from './lib/apply-web-config';
/**
* This plugin provides features to build Node and Web applications.
* - TS support (including tsconfig paths)
* - Different compiler options
* - Assets handling
* - Stylesheets handling
* - index.html and package.json generation
*
* Web-only features, such as stylesheets and images, are only supported when `target` is 'web' or 'webworker'.
*/
export class NxAppWebpackPlugin {
private readonly options: NormalizedNxAppWebpackPluginOptions;
constructor(options: NxAppWebpackPluginOptions = {}) {
// If we're building inferred targets, skip normalizing build options.
if (!global.NX_GRAPH_CREATION) {
this.options = normalizeOptions(options);
}
}
apply(compiler: Compiler): void {
// Defaults to 'web' if not specified to match Webpack's default.
const target = this.options.target ?? compiler.options.target ?? 'web';
this.options.outputPath ??= compiler.options.output?.path;
if (typeof target === 'string') {
this.options.target = target;
}
applyBaseConfig(this.options, compiler.options, {
useNormalizedEntry: true,
});
if (compiler.options.target) {
this.options.target = compiler.options.target;
}
if (this.options.target === 'web' || this.options.target === 'webworker') {
applyWebConfig(this.options, compiler.options, {
useNormalizedEntry: true,
});
}
if (this.options.deleteOutputPath) {
deleteOutputDir(this.options.root, this.options.outputPath);
}
}
}