@nestjs/cli 12.0.0-alpha.0 → 12.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
1
  import { MultiNestCompilerPlugins } from '../plugins/plugins-loader.js';
2
- export declare const rspackDefaultsFactory: (sourceRoot: string, relativeSourceRoot: string, entryFilename: string, isDebugEnabled: boolean | undefined, tsConfigFile: string | undefined, plugins: MultiNestCompilerPlugins) => Record<string, any>;
2
+ export declare const rspackDefaultsFactory: (sourceRoot: string, relativeSourceRoot: string, entryFilename: string, isDebugEnabled: boolean | undefined, tsConfigFile: string | undefined, plugins: MultiNestCompilerPlugins, isEsm?: boolean) => Record<string, any>;
@@ -1,11 +1,22 @@
1
1
  import { createRequire } from 'module';
2
2
  import { join } from 'path';
3
- import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
4
3
  import { defaultTsconfigFilename } from '../../configuration/defaults.js';
5
4
  import { appendTsExtension } from '../helpers/append-extension.js';
6
- import nodeExternals from 'webpack-node-externals';
7
5
  const require = createRequire(import.meta.url);
8
- export const rspackDefaultsFactory = (sourceRoot, relativeSourceRoot, entryFilename, isDebugEnabled = false, tsConfigFile = defaultTsconfigFilename, plugins) => {
6
+ function loadRspackDeps() {
7
+ try {
8
+ const externals = require('webpack-node-externals');
9
+ const { TsconfigPathsPlugin } = require('tsconfig-paths-webpack-plugin');
10
+ return { nodeExternals: externals, TsconfigPathsPlugin };
11
+ }
12
+ catch (e) {
13
+ const pkg = e?.message?.match?.(/Cannot find.*'([^']+)'/)?.[1] ?? 'webpack-node-externals';
14
+ throw new Error(`The "${pkg}" package is required when using the rspack compiler but could not be found. ` +
15
+ `Please install it:\n\n npm install --save-dev @rspack/core webpack-node-externals tsconfig-paths-webpack-plugin\n`);
16
+ }
17
+ }
18
+ export const rspackDefaultsFactory = (sourceRoot, relativeSourceRoot, entryFilename, isDebugEnabled = false, tsConfigFile = defaultTsconfigFilename, plugins, isEsm = false) => {
19
+ const { nodeExternals: externals, TsconfigPathsPlugin } = loadRspackDeps();
9
20
  const isPluginRegistered = isAnyPluginRegistered(plugins);
10
21
  const rspackConfiguration = {
11
22
  entry: appendTsExtension(join(sourceRoot, entryFilename)),
@@ -13,9 +24,19 @@ export const rspackDefaultsFactory = (sourceRoot, relativeSourceRoot, entryFilen
13
24
  target: 'node',
14
25
  output: {
15
26
  filename: join(relativeSourceRoot, `${entryFilename}.js`),
27
+ ...(isEsm && {
28
+ module: true,
29
+ library: { type: 'module' },
30
+ chunkFormat: 'module',
31
+ }),
16
32
  },
33
+ ...(isEsm && {
34
+ experiments: { outputModule: true },
35
+ }),
17
36
  ignoreWarnings: [/^(?!CriticalDependenciesWarning$)/],
18
- externals: [nodeExternals()],
37
+ externals: [
38
+ externals(isEsm ? { importType: 'module' } : {}),
39
+ ],
19
40
  externalsPresets: { node: true },
20
41
  module: {
21
42
  rules: [
@@ -1,3 +1,3 @@
1
1
  import { MultiNestCompilerPlugins } from '../plugins/plugins-loader.js';
2
- import webpack from 'webpack';
3
- export declare const webpackDefaultsFactory: (sourceRoot: string, relativeSourceRoot: string, entryFilename: string, isDebugEnabled: boolean | undefined, tsConfigFile: string | undefined, plugins: MultiNestCompilerPlugins) => webpack.Configuration;
2
+ import type webpack from 'webpack';
3
+ export declare const webpackDefaultsFactory: (sourceRoot: string, relativeSourceRoot: string, entryFilename: string, isDebugEnabled: boolean | undefined, tsConfigFile: string | undefined, plugins: MultiNestCompilerPlugins, isEsm?: boolean) => webpack.Configuration;
@@ -1,12 +1,23 @@
1
1
  import { createRequire } from 'module';
2
2
  import { join } from 'path';
3
- import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
4
3
  import { defaultTsconfigFilename } from '../../configuration/defaults.js';
5
4
  import { appendTsExtension } from '../helpers/append-extension.js';
6
- import webpack from 'webpack';
7
- import nodeExternals from 'webpack-node-externals';
8
5
  const require = createRequire(import.meta.url);
9
- export const webpackDefaultsFactory = (sourceRoot, relativeSourceRoot, entryFilename, isDebugEnabled = false, tsConfigFile = defaultTsconfigFilename, plugins) => {
6
+ function loadWebpackDeps() {
7
+ try {
8
+ const wp = require('webpack');
9
+ const externals = require('webpack-node-externals');
10
+ const { TsconfigPathsPlugin } = require('tsconfig-paths-webpack-plugin');
11
+ return { webpack: wp, nodeExternals: externals, TsconfigPathsPlugin };
12
+ }
13
+ catch (e) {
14
+ const pkg = e?.message?.match?.(/Cannot find.*'([^']+)'/)?.[1] ?? 'webpack';
15
+ throw new Error(`The "${pkg}" package is required when using the webpack compiler but could not be found. ` +
16
+ `Please install it:\n\n npm install --save-dev webpack webpack-node-externals tsconfig-paths-webpack-plugin ts-loader fork-ts-checker-webpack-plugin\n`);
17
+ }
18
+ }
19
+ export const webpackDefaultsFactory = (sourceRoot, relativeSourceRoot, entryFilename, isDebugEnabled = false, tsConfigFile = defaultTsconfigFilename, plugins, isEsm = false) => {
20
+ const { webpack: wp, nodeExternals: externals, TsconfigPathsPlugin } = loadWebpackDeps();
10
21
  const isPluginRegistered = isAnyPluginRegistered(plugins);
11
22
  const webpackConfiguration = {
12
23
  entry: appendTsExtension(join(sourceRoot, entryFilename)),
@@ -14,9 +25,19 @@ export const webpackDefaultsFactory = (sourceRoot, relativeSourceRoot, entryFile
14
25
  target: 'node',
15
26
  output: {
16
27
  filename: join(relativeSourceRoot, `${entryFilename}.js`),
28
+ ...(isEsm && {
29
+ module: true,
30
+ library: { type: 'module' },
31
+ chunkFormat: 'module',
32
+ }),
17
33
  },
34
+ ...(isEsm && {
35
+ experiments: { outputModule: true },
36
+ }),
18
37
  ignoreWarnings: [/^(?!CriticalDependenciesWarning$)/],
19
- externals: [nodeExternals()],
38
+ externals: [
39
+ externals(isEsm ? { importType: 'module' } : {}),
40
+ ],
20
41
  externalsPresets: { node: true },
21
42
  module: {
22
43
  rules: [
@@ -57,7 +78,7 @@ export const webpackDefaultsFactory = (sourceRoot, relativeSourceRoot, entryFile
57
78
  __dirname: false,
58
79
  },
59
80
  plugins: [
60
- new webpack.IgnorePlugin({
81
+ new wp.IgnorePlugin({
61
82
  checkResource(resource) {
62
83
  const lazyImports = [
63
84
  '@nestjs/microservices',
@@ -2,6 +2,7 @@ import { existsSync } from 'fs';
2
2
  import { createRequire } from 'module';
3
3
  import { join } from 'path';
4
4
  import { INFO_PREFIX } from '../ui/index.js';
5
+ import { isEsmProject } from '../utils/is-esm-project.js';
5
6
  import { BaseCompiler } from './base-compiler.js';
6
7
  import { rspackDefaultsFactory } from './defaults/rspack-defaults.js';
7
8
  import { getValueOrDefault } from './helpers/get-value-or-default.js';
@@ -20,8 +21,15 @@ export class RspackCompiler extends BaseCompiler {
20
21
  const pathToSource = this.getPathToSource(configuration, tsConfigPath, appName);
21
22
  const entryFile = getValueOrDefault(configuration, 'entryFile', appName, 'entryFile', extras.options);
22
23
  const entryFileRoot = getValueOrDefault(configuration, 'root', appName) || '';
23
- const defaultOptions = rspackDefaultsFactory(pathToSource, entryFileRoot, entryFile, extras.debug ?? false, tsConfigPath, plugins);
24
- const rspack = require('@rspack/core');
24
+ const defaultOptions = rspackDefaultsFactory(pathToSource, entryFileRoot, entryFile, extras.debug ?? false, tsConfigPath, plugins, isEsmProject());
25
+ let rspack;
26
+ try {
27
+ rspack = require('@rspack/core');
28
+ }
29
+ catch {
30
+ throw new Error('@rspack/core is not installed. To use the rspack compiler, install the required packages:\n\n' +
31
+ ' npm install --save-dev @rspack/core webpack-node-externals tsconfig-paths-webpack-plugin\n');
32
+ }
25
33
  let compiler;
26
34
  let watchOptions;
27
35
  let watch;
@@ -2,7 +2,7 @@ import { Configuration } from '../configuration/index.js';
2
2
  import { AssetsManager } from './assets-manager.js';
3
3
  import { BaseCompiler } from './base-compiler.js';
4
4
  import { PluginsLoader } from './plugins/plugins-loader.js';
5
- import webpack from 'webpack';
5
+ import type webpack from 'webpack';
6
6
  type WebpackConfigFactory = (config: webpack.Configuration, webpackRef: typeof webpack) => webpack.Configuration;
7
7
  type WebpackConfigFactoryOrConfig = WebpackConfigFactory | webpack.Configuration;
8
8
  type WebpackCompilerExtras = {
@@ -1,10 +1,21 @@
1
1
  import { existsSync } from 'fs';
2
+ import { createRequire } from 'module';
2
3
  import { join } from 'path';
3
4
  import { INFO_PREFIX } from '../ui/index.js';
5
+ import { isEsmProject } from '../utils/is-esm-project.js';
4
6
  import { BaseCompiler } from './base-compiler.js';
5
7
  import { webpackDefaultsFactory } from './defaults/webpack-defaults.js';
6
8
  import { getValueOrDefault } from './helpers/get-value-or-default.js';
7
- import webpack from 'webpack';
9
+ const require = createRequire(import.meta.url);
10
+ function loadWebpack() {
11
+ try {
12
+ return require('webpack');
13
+ }
14
+ catch {
15
+ throw new Error('webpack is not installed. To use the webpack compiler, install the required packages:\n\n' +
16
+ ' npm install --save-dev webpack webpack-node-externals tsconfig-paths-webpack-plugin ts-loader\n');
17
+ }
18
+ }
8
19
  export class WebpackCompiler extends BaseCompiler {
9
20
  constructor(pluginsLoader) {
10
21
  super(pluginsLoader);
@@ -19,7 +30,8 @@ export class WebpackCompiler extends BaseCompiler {
19
30
  const pathToSource = this.getPathToSource(configuration, tsConfigPath, appName);
20
31
  const entryFile = getValueOrDefault(configuration, 'entryFile', appName, 'entryFile', extras.options);
21
32
  const entryFileRoot = getValueOrDefault(configuration, 'root', appName) || '';
22
- const defaultOptions = webpackDefaultsFactory(pathToSource, entryFileRoot, entryFile, extras.debug ?? false, tsConfigPath, plugins);
33
+ const defaultOptions = webpackDefaultsFactory(pathToSource, entryFileRoot, entryFile, extras.debug ?? false, tsConfigPath, plugins, isEsmProject());
34
+ const wp = loadWebpack();
23
35
  let compiler;
24
36
  let watchOptions;
25
37
  let watch;
@@ -27,27 +39,27 @@ export class WebpackCompiler extends BaseCompiler {
27
39
  const webpackConfigurations = extras.webpackConfigFactoryOrConfig.map((configOrFactory) => {
28
40
  const unwrappedConfig = typeof configOrFactory !== 'function'
29
41
  ? configOrFactory
30
- : configOrFactory(defaultOptions, webpack);
42
+ : configOrFactory(defaultOptions, wp);
31
43
  return {
32
44
  ...defaultOptions,
33
45
  mode: extras.watchMode ? 'development' : defaultOptions.mode,
34
46
  ...unwrappedConfig,
35
47
  };
36
48
  });
37
- compiler = webpack(webpackConfigurations);
49
+ compiler = wp(webpackConfigurations);
38
50
  watchOptions = webpackConfigurations.map((config) => config.watchOptions || {});
39
51
  watch = webpackConfigurations.some((config) => config.watch);
40
52
  }
41
53
  else {
42
54
  const projectWebpackOptions = typeof extras.webpackConfigFactoryOrConfig !== 'function'
43
55
  ? extras.webpackConfigFactoryOrConfig
44
- : extras.webpackConfigFactoryOrConfig(defaultOptions, webpack);
56
+ : extras.webpackConfigFactoryOrConfig(defaultOptions, wp);
45
57
  const webpackConfiguration = {
46
58
  ...defaultOptions,
47
59
  mode: extras.watchMode ? 'development' : defaultOptions.mode,
48
60
  ...projectWebpackOptions,
49
61
  };
50
- compiler = webpack(webpackConfiguration);
62
+ compiler = wp(webpackConfiguration);
51
63
  watchOptions = webpackConfiguration.watchOptions;
52
64
  watch = webpackConfiguration.watch;
53
65
  }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Detect whether the target project uses ESM output.
3
+ * Checks the project's package.json for `"type": "module"`.
4
+ */
5
+ export declare function isEsmProject(cwd?: string): boolean;
@@ -0,0 +1,16 @@
1
+ import { readFileSync } from 'fs';
2
+ import { join } from 'path';
3
+ /**
4
+ * Detect whether the target project uses ESM output.
5
+ * Checks the project's package.json for `"type": "module"`.
6
+ */
7
+ export function isEsmProject(cwd = process.cwd()) {
8
+ try {
9
+ const raw = readFileSync(join(cwd, 'package.json'), 'utf-8');
10
+ const pkg = JSON.parse(raw);
11
+ return pkg.type === 'module';
12
+ }
13
+ catch {
14
+ return false;
15
+ }
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/cli",
3
- "version": "12.0.0-alpha.0",
3
+ "version": "12.0.0-alpha.1",
4
4
  "type": "module",
5
5
  "description": "Nest - modern, fast, powerful node.js web framework (@cli)",
6
6
  "publishConfig": {
@@ -28,8 +28,7 @@
28
28
  "test:dev": "npm run clean && vitest --watch",
29
29
  "prerelease": "npm run build",
30
30
  "release": "release-it",
31
- "prepare": "husky",
32
- "postinstall": "node tools/postinstall.cjs"
31
+ "prepare": "husky && node tools/postinstall.cjs"
33
32
  },
34
33
  "repository": {
35
34
  "type": "git",
@@ -50,15 +49,11 @@
50
49
  "chokidar": "4.0.3",
51
50
  "cli-table3": "0.6.5",
52
51
  "commander": "14.0.3",
53
- "fork-ts-checker-webpack-plugin": "9.1.0",
54
52
  "glob": "13.0.6",
55
53
  "node-emoji": "2.2.0",
56
54
  "ora": "9.3.0",
57
55
  "tsconfig-paths": "4.2.0",
58
- "tsconfig-paths-webpack-plugin": "4.2.0",
59
- "typescript": "5.9.3",
60
- "webpack": "5.105.4",
61
- "webpack-node-externals": "3.0.0"
56
+ "typescript": "5.9.3"
62
57
  },
63
58
  "devDependencies": {
64
59
  "@commitlint/cli": "20.4.3",
@@ -72,6 +67,7 @@
72
67
  "eslint": "10.0.2",
73
68
  "eslint-config-prettier": "10.1.8",
74
69
  "typescript-eslint": "8.56.1",
70
+ "fork-ts-checker-webpack-plugin": "9.1.0",
75
71
  "gulp": "5.0.1",
76
72
  "gulp-clean": "0.4.0",
77
73
  "husky": "9.1.7",
@@ -79,21 +75,48 @@
79
75
  "prettier": "3.8.1",
80
76
  "release-it": "19.2.4",
81
77
  "ts-loader": "9.5.4",
82
- "vitest": "3.2.4"
78
+ "tsconfig-paths-webpack-plugin": "4.2.0",
79
+ "vitest": "3.2.4",
80
+ "webpack": "5.105.4",
81
+ "webpack-node-externals": "3.0.0"
83
82
  },
84
83
  "lint-staged": {
85
84
  "**/*.{ts,json}": []
86
85
  },
87
86
  "peerDependencies": {
87
+ "@rspack/core": "^1.0.0",
88
88
  "@swc/cli": "^0.1.62 || ^0.3.0 || ^0.4.0 || ^0.5.0 || ^0.6.0 || ^0.7.0 || ^0.8.0",
89
- "@swc/core": "^1.3.62"
89
+ "@swc/core": "^1.3.62",
90
+ "fork-ts-checker-webpack-plugin": "^9.0.0",
91
+ "ts-loader": "^9.0.0",
92
+ "tsconfig-paths-webpack-plugin": "^4.0.0",
93
+ "webpack": "^5.0.0",
94
+ "webpack-node-externals": "^3.0.0"
90
95
  },
91
96
  "peerDependenciesMeta": {
97
+ "@rspack/core": {
98
+ "optional": true
99
+ },
92
100
  "@swc/cli": {
93
101
  "optional": true
94
102
  },
95
103
  "@swc/core": {
96
104
  "optional": true
105
+ },
106
+ "fork-ts-checker-webpack-plugin": {
107
+ "optional": true
108
+ },
109
+ "ts-loader": {
110
+ "optional": true
111
+ },
112
+ "tsconfig-paths-webpack-plugin": {
113
+ "optional": true
114
+ },
115
+ "webpack": {
116
+ "optional": true
117
+ },
118
+ "webpack-node-externals": {
119
+ "optional": true
97
120
  }
98
121
  }
99
122
  }