@nx/devkit 23.0.0-canary.20260504-e8aa612 → 23.0.0-canary.20260508-a2dfac6

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.
@@ -0,0 +1,4 @@
1
+ import { type Tree } from 'nx/src/devkit-exports';
2
+ export declare const DEVKIT_INTERNAL_SYMBOLS: ReadonlySet<string>;
3
+ export default function updateDevkitDeepImports(tree: Tree): Promise<void>;
4
+ export declare function rewriteDevkitDeepImports(source: string): string;
@@ -0,0 +1,321 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEVKIT_INTERNAL_SYMBOLS = void 0;
4
+ exports.default = updateDevkitDeepImports;
5
+ exports.rewriteDevkitDeepImports = rewriteDevkitDeepImports;
6
+ const devkit_exports_1 = require("nx/src/devkit-exports");
7
+ const format_files_1 = require("../../generators/format-files");
8
+ const visit_not_ignored_files_1 = require("../../generators/visit-not-ignored-files");
9
+ const package_json_1 = require("../../utils/package-json");
10
+ const string_change_1 = require("../../utils/string-change");
11
+ const versions_1 = require("../../utils/versions");
12
+ const TS_EXTENSIONS = ['.ts', '.tsx', '.cts', '.mts'];
13
+ const DEEP_IMPORT_PREFIX = '@nx/devkit/src/';
14
+ const PUBLIC_SPECIFIER = '@nx/devkit';
15
+ const INTERNAL_SPECIFIER = '@nx/devkit/internal';
16
+ // Names re-exported from `@nx/devkit/internal` (see packages/devkit/internal.ts
17
+ // at the time this migration was authored). Anything imported from a
18
+ // `@nx/devkit/src/...` path whose name is NOT in this set is assumed to be
19
+ // part of the stable public `@nx/devkit` API.
20
+ exports.DEVKIT_INTERNAL_SYMBOLS = new Set([
21
+ 'signalToCode',
22
+ 'createProjectRootMappingsFromProjectConfigurations',
23
+ 'PluginCache',
24
+ 'safeWriteFileCache',
25
+ 'determineArtifactNameAndDirectoryOptions',
26
+ 'getRelativeCwd',
27
+ 'FileExtensionType',
28
+ 'getE2EWebServerInfo',
29
+ 'E2EWebServerDetails',
30
+ 'forEachExecutorOptions',
31
+ 'AggregatedLog',
32
+ 'migrateProjectExecutorsToPlugin',
33
+ 'migrateProjectExecutorsToPluginV1',
34
+ 'NoTargetsToMigrateError',
35
+ 'InferredTargetConfiguration',
36
+ 'processTargetOutputs',
37
+ 'deleteMatchingProperties',
38
+ 'toProjectRelativePath',
39
+ 'determineProjectNameAndRootOptions',
40
+ 'ensureRootProjectName',
41
+ 'resolveImportPath',
42
+ 'promptWhenInteractive',
43
+ 'addBuildTargetDefaults',
44
+ 'addE2eCiTargetDefaults',
45
+ 'addPlugin',
46
+ 'createAsyncIterable',
47
+ 'combineAsyncIterables',
48
+ 'mapAsyncIterable',
49
+ 'calculateHashForCreateNodes',
50
+ 'calculateHashesForCreateNodes',
51
+ 'getCatalogManager',
52
+ 'loadConfigFile',
53
+ 'clearRequireCache',
54
+ 'findPluginForConfigFile',
55
+ 'getNamedInputs',
56
+ 'logShowProjectCommand',
57
+ 'eachValueFrom',
58
+ 'checkAndCleanWithSemver',
59
+ 'camelize',
60
+ 'capitalize',
61
+ 'classify',
62
+ 'dasherize',
63
+ ]);
64
+ // Methods on `jest` and `vi` that take a module specifier as their first
65
+ // argument. Calls like `jest.mock('@nx/devkit/src/...')` are rewritten so the
66
+ // mock target lines up with the rewritten import.
67
+ const MOCK_HELPER_METHODS = new Set([
68
+ 'mock',
69
+ 'unmock',
70
+ 'doMock',
71
+ 'dontMock',
72
+ 'requireActual',
73
+ 'requireMock',
74
+ 'importActual',
75
+ 'importMock',
76
+ ]);
77
+ let ts;
78
+ async function updateDevkitDeepImports(tree) {
79
+ let touchedCount = 0;
80
+ (0, visit_not_ignored_files_1.visitNotIgnoredFiles)(tree, '.', (filePath) => {
81
+ if (!TS_EXTENSIONS.some((ext) => filePath.endsWith(ext))) {
82
+ return;
83
+ }
84
+ const original = tree.read(filePath, 'utf-8');
85
+ if (!original || !original.includes(DEEP_IMPORT_PREFIX)) {
86
+ return;
87
+ }
88
+ const updated = rewriteDevkitDeepImports(original);
89
+ if (updated !== original) {
90
+ tree.write(filePath, updated);
91
+ touchedCount += 1;
92
+ }
93
+ });
94
+ if (touchedCount > 0) {
95
+ devkit_exports_1.logger.info(`Rewrote @nx/devkit deep imports in ${touchedCount} file(s).`);
96
+ }
97
+ await (0, format_files_1.formatFiles)(tree);
98
+ }
99
+ function rewriteDevkitDeepImports(source) {
100
+ ts ??= (0, package_json_1.ensurePackage)('typescript', versions_1.typescriptVersion);
101
+ const sourceFile = ts.createSourceFile('tmp.ts', source, ts.ScriptTarget.Latest,
102
+ /* setParentNodes */ true, ts.ScriptKind.TSX);
103
+ const changes = [];
104
+ for (const stmt of sourceFile.statements) {
105
+ if (!ts.isImportDeclaration(stmt))
106
+ continue;
107
+ if (!ts.isStringLiteral(stmt.moduleSpecifier))
108
+ continue;
109
+ if (!stmt.moduleSpecifier.text.startsWith(DEEP_IMPORT_PREFIX))
110
+ continue;
111
+ const replacement = buildReplacement(stmt, sourceFile);
112
+ changes.push({
113
+ type: string_change_1.ChangeType.Delete,
114
+ start: stmt.getStart(sourceFile),
115
+ length: stmt.getEnd() - stmt.getStart(sourceFile),
116
+ }, {
117
+ type: string_change_1.ChangeType.Insert,
118
+ index: stmt.getStart(sourceFile),
119
+ text: replacement,
120
+ });
121
+ }
122
+ // Pass 2: rewrite `require('@nx/devkit/src/...')`, dynamic
123
+ // `import('@nx/devkit/src/...')`, and `jest.mock(...)` / `vi.mock(...)`-style
124
+ // calls. We can't bucket these by symbol (no named binding to inspect), so
125
+ // we route them at `/internal` as a best guess. Walking the AST instead of
126
+ // string-replacing keeps us out of unrelated string literals — template
127
+ // strings, `typeof import('...')` type queries, comments, etc.
128
+ collectCallExpressionRewrites(sourceFile, changes);
129
+ let updated = changes.length > 0 ? (0, string_change_1.applyChangesToString)(source, changes) : source;
130
+ // Final pass: collapse any duplicate `@nx/devkit` and `@nx/devkit/internal`
131
+ // named-only imports into a single declaration. This handles both the
132
+ // imports we just emitted AND any that the user already had, so we never
133
+ // leave the file with two `from '@nx/devkit'` lines.
134
+ updated = collapseDevkitImports(updated);
135
+ return updated;
136
+ }
137
+ function collapseDevkitImports(source) {
138
+ const sourceFile = ts.createSourceFile('tmp.ts', source, ts.ScriptTarget.Latest,
139
+ /* setParentNodes */ true, ts.ScriptKind.TSX);
140
+ const groups = new Map();
141
+ for (const stmt of sourceFile.statements) {
142
+ if (!ts.isImportDeclaration(stmt))
143
+ continue;
144
+ if (!ts.isStringLiteral(stmt.moduleSpecifier))
145
+ continue;
146
+ const specifier = stmt.moduleSpecifier.text;
147
+ if (specifier !== PUBLIC_SPECIFIER && specifier !== INTERNAL_SPECIFIER) {
148
+ continue;
149
+ }
150
+ const importClause = stmt.importClause;
151
+ if (!importClause)
152
+ continue; // skip side-effect imports
153
+ if (importClause.name)
154
+ continue; // skip default imports
155
+ const namedBindings = importClause.namedBindings;
156
+ if (!namedBindings || !ts.isNamedImports(namedBindings))
157
+ continue;
158
+ const typeOnly = !!importClause.isTypeOnly;
159
+ const key = `${specifier}\x00${typeOnly ? 'type' : 'value'}`;
160
+ if (!groups.has(key)) {
161
+ groups.set(key, { decls: [], specifier, typeOnly });
162
+ }
163
+ groups.get(key).decls.push(stmt);
164
+ }
165
+ const changes = [];
166
+ for (const { decls, specifier, typeOnly } of groups.values()) {
167
+ if (decls.length < 2)
168
+ continue;
169
+ const seen = new Set();
170
+ const merged = [];
171
+ for (const decl of decls) {
172
+ const named = decl.importClause.namedBindings;
173
+ for (const el of named.elements) {
174
+ const text = renderSpecifierFromNode(el, typeOnly);
175
+ if (!seen.has(text)) {
176
+ seen.add(text);
177
+ merged.push(text);
178
+ }
179
+ }
180
+ }
181
+ // Replace the first declaration with the merged one in place.
182
+ const first = decls[0];
183
+ changes.push({
184
+ type: string_change_1.ChangeType.Delete,
185
+ start: first.getStart(sourceFile),
186
+ length: first.getEnd() - first.getStart(sourceFile),
187
+ }, {
188
+ type: string_change_1.ChangeType.Insert,
189
+ index: first.getStart(sourceFile),
190
+ text: renderImport(merged, specifier, typeOnly),
191
+ });
192
+ // Delete every other declaration in this group (and consume one trailing
193
+ // newline so we don't leave behind a blank line that prettier has to clean
194
+ // up later).
195
+ for (let i = 1; i < decls.length; i++) {
196
+ const decl = decls[i];
197
+ const start = decl.getStart(sourceFile);
198
+ let end = decl.getEnd();
199
+ if (source[end] === '\n') {
200
+ end += 1;
201
+ }
202
+ else if (source[end] === '\r' && source[end + 1] === '\n') {
203
+ end += 2;
204
+ }
205
+ changes.push({
206
+ type: string_change_1.ChangeType.Delete,
207
+ start,
208
+ length: end - start,
209
+ });
210
+ }
211
+ }
212
+ return changes.length > 0 ? (0, string_change_1.applyChangesToString)(source, changes) : source;
213
+ }
214
+ function renderSpecifierFromNode(el, parentIsTypeOnly) {
215
+ const aliasText = el.propertyName ? ` as ${el.name.text}` : '';
216
+ const typePrefix = !parentIsTypeOnly && el.isTypeOnly ? 'type ' : '';
217
+ return `${typePrefix}${(el.propertyName ?? el.name).text}${aliasText}`;
218
+ }
219
+ function buildReplacement(decl, sourceFile) {
220
+ const importClause = decl.importClause;
221
+ // `import '@nx/devkit/src/...';` (side-effect) — no clause to bucket.
222
+ if (!importClause) {
223
+ return `import '${INTERNAL_SPECIFIER}';`;
224
+ }
225
+ const namedBindings = importClause.namedBindings;
226
+ const isNamedImport = namedBindings && ts.isNamedImports(namedBindings) && !importClause.name;
227
+ // Default / namespace / mixed-default-and-named — can't bucket reliably.
228
+ // Preserve the import shape, swap the specifier.
229
+ if (!isNamedImport) {
230
+ const before = source(decl, sourceFile).slice(0, decl.moduleSpecifier.getStart(sourceFile) - decl.getStart(sourceFile));
231
+ const after = source(decl, sourceFile).slice(decl.moduleSpecifier.getEnd() - decl.getStart(sourceFile));
232
+ return `${before}'${INTERNAL_SPECIFIER}'${after}`;
233
+ }
234
+ const isTypeOnlyImport = importClause.isTypeOnly;
235
+ const elements = namedBindings.elements;
236
+ const publik = [];
237
+ const internal = [];
238
+ for (const el of elements) {
239
+ bucketSpecifier(el, isTypeOnlyImport, publik, internal);
240
+ }
241
+ const lines = [];
242
+ if (publik.length > 0) {
243
+ lines.push(renderImport(publik, PUBLIC_SPECIFIER, isTypeOnlyImport));
244
+ }
245
+ if (internal.length > 0) {
246
+ lines.push(renderImport(internal, INTERNAL_SPECIFIER, isTypeOnlyImport));
247
+ }
248
+ if (lines.length === 0) {
249
+ // Defensive: empty `import {} from '...'` — point at /internal.
250
+ lines.push(`import {} from '${INTERNAL_SPECIFIER}';`);
251
+ }
252
+ return lines.join('\n');
253
+ }
254
+ function bucketSpecifier(el, parentIsTypeOnly, publik, internal) {
255
+ const lookupName = (el.propertyName ?? el.name).text;
256
+ const elementIsTypeOnly = el.isTypeOnly;
257
+ const aliasText = el.propertyName ? ` as ${el.name.text}` : '';
258
+ // Inline `type` is illegal when the parent import is already `import type`.
259
+ const typePrefix = !parentIsTypeOnly && elementIsTypeOnly ? 'type ' : '';
260
+ const text = `${typePrefix}${(el.propertyName ?? el.name).text}${aliasText}`;
261
+ if (exports.DEVKIT_INTERNAL_SYMBOLS.has(lookupName)) {
262
+ internal.push(text);
263
+ }
264
+ else {
265
+ publik.push(text);
266
+ }
267
+ }
268
+ function renderImport(specifiers, from, typeOnly) {
269
+ const prefix = typeOnly ? 'import type' : 'import';
270
+ return `${prefix} { ${specifiers.join(', ')} } from '${from}';`;
271
+ }
272
+ function source(decl, sourceFile) {
273
+ return sourceFile.text.slice(decl.getStart(sourceFile), decl.getEnd());
274
+ }
275
+ function collectCallExpressionRewrites(sourceFile, changes) {
276
+ const visit = (node) => {
277
+ if (ts.isCallExpression(node) &&
278
+ shouldRewriteCallExpression(node) &&
279
+ node.arguments.length >= 1 &&
280
+ ts.isStringLiteral(node.arguments[0]) &&
281
+ node.arguments[0].text.startsWith(DEEP_IMPORT_PREFIX)) {
282
+ const arg = node.arguments[0];
283
+ const start = arg.getStart(sourceFile);
284
+ const end = arg.getEnd();
285
+ const quote = sourceFile.text.charAt(start);
286
+ changes.push({
287
+ type: string_change_1.ChangeType.Delete,
288
+ start,
289
+ length: end - start,
290
+ }, {
291
+ type: string_change_1.ChangeType.Insert,
292
+ index: start,
293
+ text: `${quote}${INTERNAL_SPECIFIER}${quote}`,
294
+ });
295
+ }
296
+ ts.forEachChild(node, visit);
297
+ };
298
+ visit(sourceFile);
299
+ }
300
+ function shouldRewriteCallExpression(call) {
301
+ const callee = call.expression;
302
+ // `require('...')`
303
+ if (ts.isIdentifier(callee) && callee.text === 'require')
304
+ return true;
305
+ // dynamic `import('...')` — the runtime form parses as a CallExpression
306
+ // whose callee is the `import` keyword. The type-position form
307
+ // (`typeof import('...')`) parses as `ImportTypeNode`, not a CallExpression,
308
+ // so we don't touch it.
309
+ if (callee.kind === ts.SyntaxKind.ImportKeyword)
310
+ return true;
311
+ // `jest.mock(...)` / `vi.mock(...)` and friends.
312
+ if (ts.isPropertyAccessExpression(callee)) {
313
+ const obj = callee.expression;
314
+ if (ts.isIdentifier(obj) &&
315
+ (obj.text === 'jest' || obj.text === 'vi') &&
316
+ MOCK_HELPER_METHODS.has(callee.name.text)) {
317
+ return true;
318
+ }
319
+ }
320
+ return false;
321
+ }
package/migrations.json CHANGED
@@ -1,5 +1,11 @@
1
1
  {
2
- "generators": {},
2
+ "generators": {
3
+ "update-devkit-deep-imports": {
4
+ "version": "23.0.0-beta.6",
5
+ "description": "Rewrite imports from `@nx/devkit/src/...` to `@nx/devkit` (for public symbols) or `@nx/devkit/internal` (for the rest), since deep imports are no longer reachable through the package's `exports` map.",
6
+ "implementation": "./dist/src/migrations/update-23-0-0/update-deep-imports"
7
+ }
8
+ },
3
9
  "packageJsonUpdates": {},
4
10
  "version": "0.1"
5
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/devkit",
3
- "version": "23.0.0-canary.20260504-e8aa612",
3
+ "version": "23.0.0-canary.20260508-a2dfac6",
4
4
  "private": false,
5
5
  "type": "commonjs",
6
6
  "files": [
@@ -55,12 +55,12 @@
55
55
  "tslib": "^2.3.0",
56
56
  "semver": "^7.6.3",
57
57
  "yargs-parser": "21.1.1",
58
- "minimatch": "10.2.4",
58
+ "minimatch": "10.2.5",
59
59
  "enquirer": "~2.3.6"
60
60
  },
61
61
  "devDependencies": {
62
62
  "jest": "^30.0.2",
63
- "nx": "23.0.0-canary.20260504-e8aa612"
63
+ "nx": "23.0.0-canary.20260508-a2dfac6"
64
64
  },
65
65
  "peerDependencies": {
66
66
  "nx": ">= 22 <= 24 || ^23.0.0-0"