1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.webpackDefaultsFactory = void 0;
|
4 | const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
|
5 | const path_1 = require("path");
|
6 | const tsconfig_paths_webpack_plugin_1 = require("tsconfig-paths-webpack-plugin");
|
7 | const defaults_1 = require("../../configuration/defaults");
|
8 | const append_extension_1 = require("../helpers/append-extension");
|
9 | const webpack = require("webpack");
|
10 | const nodeExternals = require("webpack-node-externals");
|
11 | const webpackDefaultsFactory = (sourceRoot, relativeSourceRoot, entryFilename, isDebugEnabled = false, tsConfigFile = defaults_1.defaultConfiguration.compilerOptions.tsConfigPath, plugins) => {
|
12 | const isPluginRegistered = isAnyPluginRegistered(plugins);
|
13 | const webpackConfiguration = {
|
14 | entry: (0, append_extension_1.appendTsExtension)((0, path_1.join)(sourceRoot, entryFilename)),
|
15 | devtool: isDebugEnabled ? 'inline-source-map' : false,
|
16 | target: 'node',
|
17 | output: {
|
18 | filename: (0, path_1.join)(relativeSourceRoot, `${entryFilename}.js`),
|
19 | },
|
20 | ignoreWarnings: [/^(?!CriticalDependenciesWarning$)/],
|
21 | externals: [nodeExternals()],
|
22 | externalsPresets: { node: true },
|
23 | module: {
|
24 | rules: [
|
25 | {
|
26 | test: /.tsx?$/,
|
27 | use: [
|
28 | {
|
29 | loader: 'ts-loader',
|
30 | options: {
|
31 | transpileOnly: !isPluginRegistered,
|
32 | configFile: tsConfigFile,
|
33 | getCustomTransformers: (program) => ({
|
34 | before: plugins.beforeHooks.map((hook) => hook(program)),
|
35 | after: plugins.afterHooks.map((hook) => hook(program)),
|
36 | afterDeclarations: plugins.afterDeclarationsHooks.map((hook) => hook(program)),
|
37 | }),
|
38 | },
|
39 | },
|
40 | ],
|
41 | exclude: /node_modules/,
|
42 | },
|
43 | ],
|
44 | },
|
45 | resolve: {
|
46 | extensions: ['.tsx', '.ts', '.js'],
|
47 | plugins: [
|
48 | new tsconfig_paths_webpack_plugin_1.TsconfigPathsPlugin({
|
49 | configFile: tsConfigFile,
|
50 | }),
|
51 | ],
|
52 | },
|
53 | mode: 'none',
|
54 | optimization: {
|
55 | nodeEnv: false,
|
56 | },
|
57 | node: {
|
58 | __filename: false,
|
59 | __dirname: false,
|
60 | },
|
61 | plugins: [
|
62 | new webpack.IgnorePlugin({
|
63 | checkResource(resource) {
|
64 | const lazyImports = [
|
65 | '@nestjs/microservices',
|
66 | 'cache-manager',
|
67 | 'class-validator',
|
68 | 'class-transformer',
|
69 | ];
|
70 | if (!lazyImports.includes(resource)) {
|
71 | return false;
|
72 | }
|
73 | try {
|
74 | require.resolve(resource, {
|
75 | paths: [process.cwd()],
|
76 | });
|
77 | }
|
78 | catch (err) {
|
79 | return true;
|
80 | }
|
81 | return false;
|
82 | },
|
83 | }),
|
84 | ],
|
85 | };
|
86 | if (!isPluginRegistered) {
|
87 | webpackConfiguration.plugins.push(new ForkTsCheckerWebpackPlugin({
|
88 | typescript: {
|
89 | configFile: tsConfigFile,
|
90 | },
|
91 | }));
|
92 | }
|
93 | return webpackConfiguration;
|
94 | };
|
95 | exports.webpackDefaultsFactory = webpackDefaultsFactory;
|
96 | function isAnyPluginRegistered(plugins) {
|
97 | return ((plugins.afterHooks && plugins.afterHooks.length > 0) ||
|
98 | (plugins.beforeHooks && plugins.beforeHooks.length > 0) ||
|
99 | (plugins.afterDeclarationsHooks &&
|
100 | plugins.afterDeclarationsHooks.length > 0));
|
101 | }
|