@nx/workspace 20.2.2 → 20.3.0-beta.1

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/workspace",
3
- "version": "20.2.2",
3
+ "version": "20.3.0-beta.1",
4
4
  "private": false,
5
5
  "description": "The Workspace plugin contains executors and generators that are useful for any Nx workspace. It should be present in every Nx workspace and other plugins build on it.",
6
6
  "repository": {
@@ -38,12 +38,12 @@
38
38
  }
39
39
  },
40
40
  "dependencies": {
41
- "@nx/devkit": "20.2.2",
41
+ "@nx/devkit": "20.3.0-beta.1",
42
42
  "chalk": "^4.1.0",
43
43
  "enquirer": "~2.3.6",
44
44
  "tslib": "^2.3.0",
45
45
  "yargs-parser": "21.1.1",
46
- "nx": "20.2.2"
46
+ "nx": "20.3.0-beta.1"
47
47
  },
48
48
  "publishConfig": {
49
49
  "access": "public"
@@ -7,6 +7,7 @@ const get_import_path_1 = require("../../../utilities/get-import-path");
7
7
  const ts_config_1 = require("../../../utilities/ts-config");
8
8
  const typescript_1 = require("../../../utilities/typescript");
9
9
  const utils_1 = require("./utils");
10
+ const ts_solution_setup_1 = require("../../../utilities/typescript/ts-solution-setup");
10
11
  let tsModule;
11
12
  /**
12
13
  * Updates all the imports in the workspace and modifies the tsconfig appropriately.
@@ -19,9 +20,13 @@ function updateImports(tree, schema, project) {
19
20
  }
20
21
  const { libsDir } = (0, devkit_1.getWorkspaceLayout)(tree);
21
22
  const projects = (0, devkit_1.getProjects)(tree);
23
+ const isUsingTsSolution = (0, ts_solution_setup_1.isUsingTsSolutionSetup)(tree);
22
24
  // use the source root to find the from location
23
25
  // this attempts to account for libs that have been created with --importPath
24
- const tsConfigPath = (0, ts_config_1.getRootTsConfigPathInTree)(tree);
26
+ const tsConfigPath = isUsingTsSolution
27
+ ? 'tsconfig.json'
28
+ : (0, ts_config_1.getRootTsConfigPathInTree)(tree);
29
+ // If we are using a ts solution setup, we need to use tsconfig.json instead of tsconfig.base.json
25
30
  let tsConfig;
26
31
  let mainEntryPointImportPath;
27
32
  let secondaryEntryPointImportPaths;
@@ -84,25 +89,53 @@ function updateImports(tree, schema, project) {
84
89
  to: schema.relativeToRootDestination,
85
90
  };
86
91
  if (tsConfig) {
87
- const path = tsConfig.compilerOptions.paths[projectRef.from];
88
- if (!path) {
89
- throw new Error([
90
- `unable to find "${projectRef.from}" in`,
91
- `${tsConfigPath} compilerOptions.paths`,
92
- ].join(' '));
93
- }
94
- const updatedPath = path.map((x) => (0, devkit_1.joinPathFragments)(projectRoot.to, (0, path_1.relative)(projectRoot.from, x)));
95
- if (schema.updateImportPath && projectRef.to) {
96
- tsConfig.compilerOptions.paths[projectRef.to] = updatedPath;
97
- if (projectRef.from !== projectRef.to) {
98
- delete tsConfig.compilerOptions.paths[projectRef.from];
99
- }
92
+ if (!isUsingTsSolution) {
93
+ updateTsConfigPaths(tsConfig, projectRef, tsConfigPath, projectRoot, schema);
100
94
  }
101
95
  else {
102
- tsConfig.compilerOptions.paths[projectRef.from] = updatedPath;
96
+ updateTsConfigReferences(tsConfig, projectRoot, tsConfigPath, schema);
103
97
  }
98
+ (0, devkit_1.writeJson)(tree, tsConfigPath, tsConfig);
99
+ }
100
+ }
101
+ }
102
+ function updateTsConfigReferences(tsConfig, projectRoot, tsConfigPath, schema) {
103
+ // Since paths can be './path' or 'path' we check if both are the same relative path to the workspace root
104
+ const projectRefIndex = tsConfig.references.findIndex((ref) => (0, path_1.relative)(ref.path, projectRoot.from) === '');
105
+ if (projectRefIndex === -1) {
106
+ throw new Error(`unable to find "${projectRoot.from}" in ${tsConfigPath} references`);
107
+ }
108
+ const updatedPath = (0, devkit_1.joinPathFragments)(projectRoot.to, (0, path_1.relative)(projectRoot.from, tsConfig.references[projectRefIndex].path));
109
+ let normalizedPath = (0, path_1.normalize)(updatedPath);
110
+ if (!normalizedPath.startsWith('.') &&
111
+ !normalizedPath.startsWith('../') &&
112
+ !(0, path_1.isAbsolute)(normalizedPath)) {
113
+ normalizedPath = `./${normalizedPath}`;
114
+ }
115
+ if (schema.updateImportPath && projectRoot.to) {
116
+ tsConfig.references[projectRefIndex].path = normalizedPath;
117
+ }
118
+ else {
119
+ tsConfig.references.push({ path: normalizedPath });
120
+ }
121
+ }
122
+ function updateTsConfigPaths(tsConfig, projectRef, tsConfigPath, projectRoot, schema) {
123
+ const path = tsConfig.compilerOptions.paths[projectRef.from];
124
+ if (!path) {
125
+ throw new Error([
126
+ `unable to find "${projectRef.from}" in`,
127
+ `${tsConfigPath} compilerOptions.paths`,
128
+ ].join(' '));
129
+ }
130
+ const updatedPath = path.map((x) => (0, devkit_1.joinPathFragments)(projectRoot.to, (0, path_1.relative)(projectRoot.from, x)));
131
+ if (schema.updateImportPath && projectRef.to) {
132
+ tsConfig.compilerOptions.paths[projectRef.to] = updatedPath;
133
+ if (projectRef.from !== projectRef.to) {
134
+ delete tsConfig.compilerOptions.paths[projectRef.from];
104
135
  }
105
- (0, devkit_1.writeJson)(tree, tsConfigPath, tsConfig);
136
+ }
137
+ else {
138
+ tsConfig.compilerOptions.paths[projectRef.from] = updatedPath;
106
139
  }
107
140
  }
108
141
  function ensureTrailingSlash(path) {
@@ -330,7 +330,11 @@ function setUpWorkspacesInPackageJson(tree, options) {
330
330
  options.preset === presets_1.Preset.NextJs ||
331
331
  options.preset === presets_1.Preset.ReactMonorepo ||
332
332
  options.preset === presets_1.Preset.ReactNative ||
333
- options.preset === presets_1.Preset.RemixMonorepo) &&
333
+ options.preset === presets_1.Preset.RemixMonorepo ||
334
+ options.preset === presets_1.Preset.VueMonorepo ||
335
+ options.preset === presets_1.Preset.Nuxt ||
336
+ options.preset === presets_1.Preset.NodeMonorepo ||
337
+ options.preset === presets_1.Preset.Express) &&
334
338
  options.workspaces)) {
335
339
  const workspaces = options.workspaceGlobs ?? ['packages/**'];
336
340
  if (options.packageManager === 'pnpm') {
@@ -129,6 +129,8 @@ async function createPreset(tree, options) {
129
129
  e2eTestRunner: options.e2eTestRunner ?? 'playwright',
130
130
  addPlugin,
131
131
  nxCloudToken: options.nxCloudToken,
132
+ useTsSolution: options.workspaces,
133
+ formatter: options.formatter,
132
134
  });
133
135
  }
134
136
  else if (options.preset === presets_1.Preset.VueStandalone) {
@@ -157,6 +159,8 @@ async function createPreset(tree, options) {
157
159
  e2eTestRunner: options.e2eTestRunner ?? 'playwright',
158
160
  addPlugin,
159
161
  nxCloudToken: options.nxCloudToken,
162
+ useTsSolution: options.workspaces,
163
+ formatter: options.formatter,
160
164
  });
161
165
  }
162
166
  else if (options.preset === presets_1.Preset.NuxtStandalone) {
@@ -229,6 +233,8 @@ async function createPreset(tree, options) {
229
233
  linter: options.linter,
230
234
  e2eTestRunner: options.e2eTestRunner ?? 'jest',
231
235
  addPlugin,
236
+ useTsSolution: options.workspaces,
237
+ formatter: options.formatter,
232
238
  });
233
239
  }
234
240
  else if (options.preset === presets_1.Preset.Express) {
@@ -239,6 +245,8 @@ async function createPreset(tree, options) {
239
245
  linter: options.linter,
240
246
  e2eTestRunner: options.e2eTestRunner ?? 'jest',
241
247
  addPlugin,
248
+ useTsSolution: options.workspaces,
249
+ formatter: options.formatter,
242
250
  });
243
251
  }
244
252
  else if (options.preset === presets_1.Preset.ReactNative) {
@@ -321,6 +329,8 @@ async function createPreset(tree, options) {
321
329
  rootProject: false,
322
330
  e2eTestRunner: options.e2eTestRunner ?? 'jest',
323
331
  addPlugin,
332
+ useTsSolution: options.workspaces,
333
+ formatter: options.formatter,
324
334
  });
325
335
  }
326
336
  else {
@@ -4,23 +4,38 @@ exports.updateTsconfig = updateTsconfig;
4
4
  const devkit_1 = require("@nx/devkit");
5
5
  const ts_config_1 = require("../../../utilities/ts-config");
6
6
  const find_project_for_path_1 = require("nx/src/project-graph/utils/find-project-for-path");
7
+ const ts_solution_setup_1 = require("../../../utilities/typescript/ts-solution-setup");
8
+ const path_1 = require("path");
7
9
  /**
8
10
  * Updates the tsconfig paths to remove the project.
9
11
  *
10
12
  * @param schema The options provided to the schematic
11
13
  */
12
14
  async function updateTsconfig(tree, schema) {
13
- const tsConfigPath = (0, ts_config_1.getRootTsConfigPathInTree)(tree);
15
+ const isUsingTsSolution = (0, ts_solution_setup_1.isUsingTsSolutionSetup)(tree);
16
+ const tsConfigPath = isUsingTsSolution
17
+ ? 'tsconfig.json'
18
+ : (0, ts_config_1.getRootTsConfigPathInTree)(tree);
14
19
  if (tree.exists(tsConfigPath)) {
15
20
  const graph = await (0, devkit_1.createProjectGraphAsync)();
16
21
  const projectMapping = (0, find_project_for_path_1.createProjectRootMappings)(graph.nodes);
17
22
  (0, devkit_1.updateJson)(tree, tsConfigPath, (json) => {
18
- for (const importPath in json.compilerOptions.paths) {
19
- for (const path of json.compilerOptions.paths[importPath]) {
20
- const project = (0, find_project_for_path_1.findProjectForPath)((0, devkit_1.normalizePath)(path), projectMapping);
21
- if (project === schema.projectName) {
22
- delete json.compilerOptions.paths[importPath];
23
- break;
23
+ if (isUsingTsSolution) {
24
+ const projectConfigs = (0, devkit_1.readProjectsConfigurationFromProjectGraph)(graph);
25
+ const project = projectConfigs.projects[schema.projectName];
26
+ if (!project) {
27
+ throw new Error(`Could not find project '${schema.project}'. Please choose a project that exists in the Nx Workspace.`);
28
+ }
29
+ json.references = json.references.filter((ref) => (0, path_1.relative)(ref.path, project.root) !== '');
30
+ }
31
+ else {
32
+ for (const importPath in json.compilerOptions.paths) {
33
+ for (const path of json.compilerOptions.paths[importPath]) {
34
+ const project = (0, find_project_for_path_1.findProjectForPath)((0, devkit_1.normalizePath)(path), projectMapping);
35
+ if (project === schema.projectName) {
36
+ delete json.compilerOptions.paths[importPath];
37
+ break;
38
+ }
24
39
  }
25
40
  }
26
41
  }
@@ -0,0 +1,2 @@
1
+ import { type Tree } from '@nx/devkit';
2
+ export declare function isUsingTsSolutionSetup(tree?: Tree): boolean;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isUsingTsSolutionSetup = isUsingTsSolutionSetup;
4
+ const devkit_1 = require("@nx/devkit");
5
+ const tree_1 = require("nx/src/generators/tree");
6
+ function isUsingPackageManagerWorkspaces(tree) {
7
+ return isWorkspacesEnabled(tree);
8
+ }
9
+ function isWorkspacesEnabled(tree) {
10
+ const packageManager = (0, devkit_1.detectPackageManager)(tree.root);
11
+ if (packageManager === 'pnpm') {
12
+ return tree.exists('pnpm-workspace.yaml');
13
+ }
14
+ // yarn and npm both use the same 'workspaces' property in package.json
15
+ if (tree.exists('package.json')) {
16
+ const packageJson = (0, devkit_1.readJson)(tree, 'package.json');
17
+ return !!packageJson?.workspaces;
18
+ }
19
+ return false;
20
+ }
21
+ function isWorkspaceSetupWithTsSolution(tree) {
22
+ if (!tree.exists('tsconfig.base.json') || !tree.exists('tsconfig.json')) {
23
+ return false;
24
+ }
25
+ const tsconfigJson = (0, devkit_1.readJson)(tree, 'tsconfig.json');
26
+ if (tsconfigJson.extends !== './tsconfig.base.json') {
27
+ return false;
28
+ }
29
+ /**
30
+ * New setup:
31
+ * - `files` is defined and set to an empty array
32
+ * - `references` is defined and set to an empty array
33
+ * - `include` is not defined or is set to an empty array
34
+ */
35
+ if (!tsconfigJson.files ||
36
+ tsconfigJson.files.length > 0 ||
37
+ !tsconfigJson.references ||
38
+ !!tsconfigJson.include?.length) {
39
+ return false;
40
+ }
41
+ const baseTsconfigJson = (0, devkit_1.readJson)(tree, 'tsconfig.base.json');
42
+ if (!baseTsconfigJson.compilerOptions ||
43
+ !baseTsconfigJson.compilerOptions.composite ||
44
+ !baseTsconfigJson.compilerOptions.declaration) {
45
+ return false;
46
+ }
47
+ const { compilerOptions, ...rest } = baseTsconfigJson;
48
+ if (Object.keys(rest).length > 0) {
49
+ return false;
50
+ }
51
+ return true;
52
+ }
53
+ function isUsingTsSolutionSetup(tree) {
54
+ tree ??= new tree_1.FsTree(devkit_1.workspaceRoot, false);
55
+ return (isUsingPackageManagerWorkspaces(tree) &&
56
+ isWorkspaceSetupWithTsSolution(tree));
57
+ }