/
node.config.ts
54 lines (49 loc) · 1.52 KB
/
node.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
import { workspaceRoot } from '@nrwl/devkit';
import { Configuration } from 'webpack';
import { merge } from 'webpack-merge';
import { getBaseWebpackPartial } from './config';
import { BuildNodeBuilderOptions } from './types';
import nodeExternals = require('webpack-node-externals');
import TerserPlugin = require('terser-webpack-plugin');
function getNodePartial(options: BuildNodeBuilderOptions) {
const webpackConfig: Configuration = {
output: {
libraryTarget: 'commonjs',
},
target: 'node',
node: false,
};
if (options.optimization) {
webpackConfig.optimization = {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
mangle: false,
keep_classnames: true,
},
}),
],
concatenateModules: true,
};
}
if (options.externalDependencies === 'all') {
const modulesDir = `${workspaceRoot}/node_modules`;
webpackConfig.externals = [nodeExternals({ modulesDir })];
} else if (Array.isArray(options.externalDependencies)) {
webpackConfig.externals = [
function (context, callback: Function) {
if (options.externalDependencies.includes(context.request)) {
// not bundled
return callback(null, `commonjs ${context.request}`);
}
// bundled
callback();
},
];
}
return webpackConfig;
}
export function getNodeWebpackConfig(options: BuildNodeBuilderOptions) {
return merge([getBaseWebpackPartial(options), getNodePartial(options)]);
}