@nx/devkit 21.1.0-beta.1 → 21.1.0-beta.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/devkit",
3
- "version": "21.1.0-beta.1",
3
+ "version": "21.1.0-beta.2",
4
4
  "private": false,
5
5
  "description": "The Nx Devkit is used to customize Nx for different technologies and use cases. It contains many utility functions for reading and writing files, updating configuration, working with Abstract Syntax Trees(ASTs), and more. Learn more about [extending Nx by leveraging the Nx Devkit](https://nx.dev/extending-nx/intro/getting-started) on our docs.",
6
6
  "repository": {
@@ -38,7 +38,7 @@
38
38
  "enquirer": "~2.3.6"
39
39
  },
40
40
  "peerDependencies": {
41
- "nx": "21.1.0-beta.1"
41
+ "nx": "21.1.0-beta.2"
42
42
  },
43
43
  "publishConfig": {
44
44
  "access": "public"
@@ -5,37 +5,59 @@ exports.loadConfigFile = loadConfigFile;
5
5
  exports.getRootTsConfigPath = getRootTsConfigPath;
6
6
  exports.getRootTsConfigFileName = getRootTsConfigFileName;
7
7
  exports.clearRequireCache = clearRequireCache;
8
- const path_1 = require("path");
9
8
  const fs_1 = require("fs");
10
9
  const node_url_1 = require("node:url");
11
10
  const devkit_exports_1 = require("nx/src/devkit-exports");
12
11
  const devkit_internals_1 = require("nx/src/devkit-internals");
12
+ const path_1 = require("path");
13
13
  exports.dynamicImport = new Function('modulePath', 'return import(modulePath);');
14
14
  async function loadConfigFile(configFilePath) {
15
- {
16
- let module;
17
- if ((0, path_1.extname)(configFilePath) === '.ts') {
18
- const siblingFiles = (0, fs_1.readdirSync)((0, path_1.dirname)(configFilePath));
19
- const tsConfigPath = siblingFiles.includes('tsconfig.json')
20
- ? (0, path_1.join)((0, path_1.dirname)(configFilePath), 'tsconfig.json')
21
- : getRootTsConfigPath();
22
- if (tsConfigPath) {
23
- const unregisterTsProject = (0, devkit_internals_1.registerTsProject)(tsConfigPath);
24
- try {
25
- module = await load(configFilePath);
26
- }
27
- finally {
28
- unregisterTsProject();
29
- }
30
- }
31
- else {
32
- module = await load(configFilePath);
33
- }
15
+ const extension = (0, path_1.extname)(configFilePath);
16
+ const module = await loadModule(configFilePath, extension);
17
+ return module.default ?? module;
18
+ }
19
+ async function loadModule(path, extension) {
20
+ if (isTypeScriptFile(extension)) {
21
+ return await loadTypeScriptModule(path, extension);
22
+ }
23
+ return await loadJavaScriptModule(path, extension);
24
+ }
25
+ function isTypeScriptFile(extension) {
26
+ return extension.endsWith('ts');
27
+ }
28
+ async function loadTypeScriptModule(path, extension) {
29
+ const tsConfigPath = getTypeScriptConfigPath(path);
30
+ if (tsConfigPath) {
31
+ const unregisterTsProject = (0, devkit_internals_1.registerTsProject)(tsConfigPath);
32
+ try {
33
+ return await loadModuleByExtension(path, extension);
34
34
  }
35
- else {
36
- module = await load(configFilePath);
35
+ finally {
36
+ unregisterTsProject();
37
37
  }
38
- return module.default ?? module;
38
+ }
39
+ return await loadModuleByExtension(path, extension);
40
+ }
41
+ function getTypeScriptConfigPath(path) {
42
+ const siblingFiles = (0, fs_1.readdirSync)((0, path_1.dirname)(path));
43
+ return siblingFiles.includes('tsconfig.json')
44
+ ? (0, path_1.join)((0, path_1.dirname)(path), 'tsconfig.json')
45
+ : getRootTsConfigPath();
46
+ }
47
+ async function loadJavaScriptModule(path, extension) {
48
+ return await loadModuleByExtension(path, extension);
49
+ }
50
+ async function loadModuleByExtension(path, extension) {
51
+ switch (extension) {
52
+ case '.cts':
53
+ case '.cjs':
54
+ return await loadCommonJS(path);
55
+ case '.mjs':
56
+ return await loadESM(path);
57
+ default:
58
+ // For both .ts and .mts files, try to load them as CommonJS first, then try ESM.
59
+ // It's possible that the file is written like ESM (e.g. using `import`) but uses CJS features like `__dirname` or `__filename`.
60
+ return await load(path);
39
61
  }
40
62
  }
41
63
  function getRootTsConfigPath() {
@@ -62,26 +84,32 @@ function clearRequireCache() {
62
84
  }
63
85
  }
64
86
  }
65
- /**
66
- * Load the module after ensuring that the require cache is cleared.
67
- */
68
87
  async function load(path) {
69
- // Clear cache if the path is in the cache
70
- if (require.cache[path]) {
71
- clearRequireCache();
72
- }
73
88
  try {
74
89
  // Try using `require` first, which works for CJS modules.
75
90
  // Modules are CJS unless it is named `.mjs` or `package.json` sets type to "module".
76
- return require(path);
91
+ return loadCommonJS(path);
77
92
  }
78
93
  catch (e) {
79
94
  if (e.code === 'ERR_REQUIRE_ESM') {
80
95
  // If `require` fails to load ESM, try dynamic `import()`. ESM requires file url protocol for handling absolute paths.
81
- const pathAsFileUrl = (0, node_url_1.pathToFileURL)(path).pathname;
82
- return await (0, exports.dynamicImport)(`${pathAsFileUrl}?t=${Date.now()}`);
96
+ return loadESM(path);
83
97
  }
84
98
  // Re-throw all other errors
85
99
  throw e;
86
100
  }
87
101
  }
102
+ /**
103
+ * Load the module after ensuring that the require cache is cleared.
104
+ */
105
+ async function loadCommonJS(path) {
106
+ // Clear cache if the path is in the cache
107
+ if (require.cache[path]) {
108
+ clearRequireCache();
109
+ }
110
+ return require(path);
111
+ }
112
+ async function loadESM(path) {
113
+ const pathAsFileUrl = (0, node_url_1.pathToFileURL)(path).pathname;
114
+ return await (0, exports.dynamicImport)(`${pathAsFileUrl}?t=${Date.now()}`);
115
+ }