/
webpack-defaults.ts
102 lines (100 loc) 路 2.84 KB
/
webpack-defaults.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
import ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
import { join } from 'path';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import { defaultConfiguration } from '../../configuration/defaults';
import { appendTsExtension } from '../helpers/append-extension';
import { MultiNestCompilerPlugins } from '../plugins-loader';
import webpack = require('webpack');
import nodeExternals = require('webpack-node-externals');
export const webpackDefaultsFactory = (
sourceRoot: string,
relativeSourceRoot: string,
entryFilename: string,
isDebugEnabled = false,
tsConfigFile = defaultConfiguration.compilerOptions.tsConfigPath,
plugins: MultiNestCompilerPlugins,
): webpack.Configuration => ({
entry: appendTsExtension(join(sourceRoot, entryFilename)),
devtool: isDebugEnabled ? 'inline-source-map' : false,
target: 'node',
output: {
filename: join(relativeSourceRoot, `${entryFilename}.js`),
},
externals: [nodeExternals() as any],
module: {
rules: [
{
test: /.tsx?$/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: !isAnyPluginRegistered(plugins),
configFile: tsConfigFile,
getCustomTransformers: (program: any) => ({
before: plugins.beforeHooks.map((hook) => hook(program)),
after: plugins.afterHooks.map((hook) => hook(program)),
afterDeclarations: plugins.afterDeclarationsHooks.map((hook) =>
hook(program),
),
}),
},
},
],
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
plugins: [
new TsconfigPathsPlugin({
configFile: tsConfigFile,
}),
],
},
mode: 'none',
optimization: {
nodeEnv: false,
},
node: {
__filename: false,
__dirname: false,
},
plugins: [
new webpack.IgnorePlugin({
checkResource(resource: any) {
const lazyImports = [
'@nestjs/microservices',
'cache-manager',
'class-validator',
'class-transformer',
];
if (!lazyImports.includes(resource)) {
return false;
}
try {
require.resolve(resource, {
paths: [process.cwd()],
});
} catch (err) {
return true;
}
return false;
},
}),
new ForkTsCheckerWebpackPlugin({
typescript: {
configFile: tsConfigFile,
},
}),
],
});
function isAnyPluginRegistered(plugins: MultiNestCompilerPlugins) {
return (
(plugins.afterHooks && plugins.afterHooks.length > 0) ||
(plugins.beforeHooks && plugins.beforeHooks.length > 0) ||
(plugins.afterDeclarationsHooks &&
plugins.afterDeclarationsHooks.length > 0)
);
}