@backstage/cli-node 0.3.6-next.0 → 0.3.6-next.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @backstage/cli-node
2
2
 
3
+ ## 0.3.6-next.1
4
+
5
+ ### Patch Changes
6
+
7
+ - a96b99c: The Node.js module transform now uses the synchronous `module.registerHooks()` API when it is available, which avoids the `module.register()` deprecation warning printed by Node.js 26. On older Node.js versions it continues to use `module.register()`.
8
+ - 04a54e2: Switched the compilation target to ES2024, which all supported Node.js versions fully support. Package builds, app bundles, and the backend development server now compile code for ES2024, and the TypeScript `target` and `lib` options in `@backstage/cli/config/tsconfig.json` are set to `ES2024`. This requires TypeScript 5.9 or later, which is now the oldest supported version according to the [versioning policy](https://backstage.io/docs/overview/versioning-policy#typescript-releases).
9
+
10
+ The `lib` change allows code to use ES2024 built-in APIs such as `Object.groupBy` and `Promise.withResolvers`. Backstage doesn't include polyfills, so frontend code that uses these APIs needs at least Chrome or Edge 119, Firefox 121, or Safari 17.4.
11
+
3
12
  ## 0.3.6-next.0
4
13
 
5
14
  ### Patch Changes
@@ -61,7 +61,7 @@ addHook(
61
61
  ignoreDynamic: true,
62
62
  },
63
63
  jsc: {
64
- target: 'es2023',
64
+ target: 'es2024',
65
65
  parser: {
66
66
  syntax: 'typescript',
67
67
  },
@@ -84,4 +84,12 @@ addHook(
84
84
  // Register module hooks, used by "type": "module" in package.json, .mjs and
85
85
  // .mts files, as well as dynamic import(...)s, although dynamic imports will be
86
86
  // handled be the CommonJS hooks in this file if what it points to is CommonJS.
87
- Module.register('./nodeTransformHooks.mjs', pathToFileURL(__filename));
87
+ //
88
+ // The synchronous hooks API runs in the same thread and is used when available,
89
+ // since Node.js 26 deprecates Module.register(). It requires loading the hooks
90
+ // module with require(), which can be disabled with --no-experimental-require-module.
91
+ if (Module.registerHooks && process.features.require_module) {
92
+ Module.registerHooks(require('./nodeTransformHooks.mjs').syncHooks);
93
+ } else {
94
+ Module.register('./nodeTransformHooks.mjs', pathToFileURL(__filename));
95
+ }
@@ -16,10 +16,9 @@
16
16
 
17
17
  import { dirname, extname, resolve as resolvePath } from 'node:path';
18
18
  import { fileURLToPath } from 'node:url';
19
- import { transformFile } from '@swc/core';
19
+ import { transformFileSync } from '@swc/core';
20
20
  import { isBuiltin } from 'node:module';
21
- import { readFile } from 'node:fs/promises';
22
- import { existsSync } from 'node:fs';
21
+ import { existsSync, readFileSync } from 'node:fs';
23
22
 
24
23
  // @ts-check
25
24
 
@@ -38,8 +37,8 @@ const moduleTypeTable = {
38
37
  '.js': undefined,
39
38
  };
40
39
 
41
- /** @type {import('module').ResolveHook} */
42
- export async function resolve(specifier, context, nextResolve) {
40
+ /** @type {import('module').ResolveHookSync} */
41
+ function resolveSync(specifier, context, nextResolve) {
43
42
  // Built-in modules are handled by the default resolver
44
43
  if (isBuiltin(specifier)) {
45
44
  return nextResolve(specifier, context);
@@ -47,12 +46,13 @@ export async function resolve(specifier, context, nextResolve) {
47
46
 
48
47
  const ext = extname(specifier);
49
48
 
50
- // Unless there's an explicit import attribute, JSON files are loaded with our custom loader that's defined below.
49
+ // Unless there's an explicit import attribute, JSON files are loaded with the
50
+ // native JSON format. The CommonJS workaround in the load hook is only needed
51
+ // for the asynchronous hooks.
51
52
  if (ext === '.json' && !context.importAttributes?.type) {
52
- const jsonResult = await nextResolve(specifier, context);
53
53
  return {
54
- ...jsonResult,
55
- format: 'commonjs',
54
+ ...nextResolve(specifier, context),
55
+ format: 'json',
56
56
  importAttributes: { type: 'json' },
57
57
  };
58
58
  }
@@ -60,13 +60,13 @@ export async function resolve(specifier, context, nextResolve) {
60
60
  // Anything else with an explicit extension is handled by the default
61
61
  // resolver, except that we help determine the module type where needed.
62
62
  if (ext !== '') {
63
- return withDetectedModuleType(await nextResolve(specifier, context));
63
+ return withDetectedModuleType(nextResolve(specifier, context));
64
64
  }
65
65
 
66
66
  // Other external modules are handled by the default resolver, but again we
67
67
  // help determine the module type where needed.
68
68
  if (!specifier.startsWith('.')) {
69
- return withDetectedModuleType(await nextResolve(specifier, context));
69
+ return withDetectedModuleType(nextResolve(specifier, context));
70
70
  }
71
71
 
72
72
  // The rest of this function handles the case of resolving imports that do not
@@ -81,16 +81,16 @@ export async function resolve(specifier, context, nextResolve) {
81
81
  try {
82
82
  // This is expected to throw, but in the event that this module specifier is
83
83
  // supported we prefer to use the default resolver.
84
- return await nextResolve(specifier, context);
84
+ return nextResolve(specifier, context);
85
85
  } catch (error) {
86
86
  if (error.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
87
87
  const spec = `${specifier}${specifier.endsWith('/') ? '' : '/'}index`;
88
- const resolved = await resolveWithoutExt(spec, context, nextResolve);
88
+ const resolved = resolveWithoutExtSync(spec, context, nextResolve);
89
89
  if (resolved) {
90
90
  return withDetectedModuleType(resolved);
91
91
  }
92
92
  } else if (error.code === 'ERR_MODULE_NOT_FOUND') {
93
- const resolved = await resolveWithoutExt(specifier, context, nextResolve);
93
+ const resolved = resolveWithoutExtSync(specifier, context, nextResolve);
94
94
  if (resolved) {
95
95
  return withDetectedModuleType(resolved);
96
96
  }
@@ -105,14 +105,14 @@ export async function resolve(specifier, context, nextResolve) {
105
105
  * Populates the `format` field in the resolved object based on the closest `package.json` file.
106
106
  *
107
107
  * @param {import('module').ResolveFnOutput} resolved
108
- * @returns {Promise<import('module').ResolveFnOutput>}
108
+ * @returns {import('module').ResolveFnOutput}
109
109
  */
110
- async function withDetectedModuleType(resolved) {
110
+ function withDetectedModuleType(resolved) {
111
111
  // Already has an explicit format
112
112
  if (resolved.format) {
113
113
  return resolved;
114
114
  }
115
- // Happens in Node.js v22 when there's a package.json without an explicit "type" field. Use the default.
115
+ // Happens when there's a package.json without an explicit "type" field. Use the default.
116
116
  if (resolved.format === null) {
117
117
  return { ...resolved, format: DEFAULT_MODULE_FORMAT };
118
118
  }
@@ -133,12 +133,12 @@ async function withDetectedModuleType(resolved) {
133
133
  // either. As such we don't restrict the file format at this point.
134
134
 
135
135
  // TODO(Rugvip): Does this need caching? kept it simple for now but worth exploring
136
- const packageJsonPath = await findPackageJSON(fileURLToPath(resolved.url));
136
+ const packageJsonPath = findPackageJSON(fileURLToPath(resolved.url));
137
137
  if (!packageJsonPath) {
138
138
  return resolved;
139
139
  }
140
140
 
141
- const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf8'));
141
+ const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
142
142
  return {
143
143
  ...resolved,
144
144
  format: packageJson.type ?? DEFAULT_MODULE_FORMAT,
@@ -150,9 +150,9 @@ async function withDetectedModuleType(resolved) {
150
150
  *
151
151
  * TODO(Rugvip): This can be replaced with the Node.js built-in with the same name once it is stable.
152
152
  * @param {string} startPath
153
- * @returns {Promise<string | undefined>}
153
+ * @returns {string | undefined}
154
154
  */
155
- async function findPackageJSON(startPath) {
155
+ function findPackageJSON(startPath) {
156
156
  let path = startPath;
157
157
 
158
158
  // Some confidence check to avoid infinite loop
@@ -174,11 +174,11 @@ async function findPackageJSON(startPath) {
174
174
  );
175
175
  }
176
176
 
177
- /** @type {import('module').ResolveHook} */
178
- async function resolveWithoutExt(specifier, context, nextResolve) {
177
+ /** @type {import('module').ResolveHookSync} */
178
+ function resolveWithoutExtSync(specifier, context, nextResolve) {
179
179
  for (const tryExt of SRC_EXTS) {
180
180
  try {
181
- const resolved = await nextResolve(specifier + tryExt, {
181
+ const resolved = nextResolve(specifier + tryExt, {
182
182
  ...context,
183
183
  format: 'commonjs',
184
184
  });
@@ -193,24 +193,28 @@ async function resolveWithoutExt(specifier, context, nextResolve) {
193
193
  return undefined;
194
194
  }
195
195
 
196
- /** @type {import('module').LoadHook} */
197
- export async function load(url, context, nextLoad) {
196
+ /**
197
+ * The load implementation is shared between the synchronous and asynchronous
198
+ * hooks, as it only passes on the result of `nextLoad`.
199
+ *
200
+ * @type {import('module').LoadHookSync}
201
+ */
202
+ function loadImpl(url, context, nextLoad) {
198
203
  // Non-file URLs are handled by the default loader
199
204
  if (!url.startsWith('file://')) {
200
205
  return nextLoad(url, context);
201
206
  }
202
207
 
203
208
  // JSON files loaded as CommonJS are handled by this custom loader, because
204
- // the default one doesn't work. For JSON loading to work we'd need the
205
- // synchronous hooks that aren't supported yet, or avoid using the CommonJS
206
- // compatibility.
209
+ // the default one doesn't work. This is only used by the asynchronous hooks,
210
+ // the synchronous ones load JSON files using the native JSON format.
207
211
  if (
208
212
  context.format === 'commonjs' &&
209
213
  context.importAttributes?.type === 'json'
210
214
  ) {
211
215
  try {
212
216
  // TODO(Rugvip): Make sure this is valid JSON
213
- const content = await readFile(fileURLToPath(url), 'utf8');
217
+ const content = readFileSync(fileURLToPath(url), 'utf8');
214
218
  return {
215
219
  source: `module.exports = (${content})`,
216
220
  format: 'commonjs',
@@ -267,7 +271,7 @@ export async function load(url, context, nextLoad) {
267
271
  return nextLoad(url, { ...context, format });
268
272
  }
269
273
 
270
- const transformed = await transformFile(fileURLToPath(url), {
274
+ const transformed = transformFileSync(fileURLToPath(url), {
271
275
  sourceMaps: 'inline',
272
276
  module: {
273
277
  type: format === 'module' ? 'es6' : 'commonjs',
@@ -277,7 +281,7 @@ export async function load(url, context, nextLoad) {
277
281
  exportInteropAnnotation: true,
278
282
  },
279
283
  jsc: {
280
- target: 'es2023',
284
+ target: 'es2024',
281
285
  parser: {
282
286
  syntax: 'typescript',
283
287
  },
@@ -292,3 +296,116 @@ export async function load(url, context, nextLoad) {
292
296
  responseURL: url,
293
297
  };
294
298
  }
299
+
300
+ /**
301
+ * Synchronous hooks for use with `module.registerHooks()`.
302
+ *
303
+ * Unlike the asynchronous hooks, these also see `require()` calls. Those are
304
+ * left to the default implementation, since CommonJS is transformed by the
305
+ * `require` hooks in `nodeTransform.cjs`.
306
+ */
307
+ export const syncHooks = {
308
+ /** @type {import('module').ResolveHookSync} */
309
+ resolve(specifier, context, nextResolve) {
310
+ if (isRequire(context)) {
311
+ return nextResolve(specifier, context);
312
+ }
313
+ return resolveSync(specifier, context, nextResolve);
314
+ },
315
+ /** @type {import('module').LoadHookSync} */
316
+ load(url, context, nextLoad) {
317
+ if (isRequire(context)) {
318
+ return nextLoad(url, context);
319
+ }
320
+ return loadImpl(url, context, nextLoad);
321
+ },
322
+ };
323
+
324
+ /**
325
+ * Custom conditions, e.g. `--conditions=require`, are added to both `import`
326
+ * and `require()` calls, so we check for the absence of `import` as well.
327
+ *
328
+ * @param {{ conditions?: readonly string[] }} context
329
+ */
330
+ function isRequire(context) {
331
+ const conditions = context.conditions ?? [];
332
+ return conditions.includes('require') && !conditions.includes('import');
333
+ }
334
+
335
+ /**
336
+ * Asynchronous resolve hook for use with `module.register()`, on Node.js
337
+ * versions that don't support `module.registerHooks()`. It mirrors
338
+ * `resolveSync`, except that `nextResolve` returns a promise.
339
+ *
340
+ * Together with `load`, this is also re-exported as a public entry point by
341
+ * `@backstage/cli/config/nodeTransformHooks.mjs`, so it needs to stay in
342
+ * place even once all supported Node.js versions have `module.registerHooks()`.
343
+ *
344
+ * @type {import('module').ResolveHook}
345
+ */
346
+ export async function resolve(specifier, context, nextResolve) {
347
+ if (isBuiltin(specifier)) {
348
+ return nextResolve(specifier, context);
349
+ }
350
+
351
+ const ext = extname(specifier);
352
+
353
+ if (ext === '.json' && !context.importAttributes?.type) {
354
+ return {
355
+ ...(await nextResolve(specifier, context)),
356
+ format: 'commonjs',
357
+ importAttributes: { type: 'json' },
358
+ };
359
+ }
360
+
361
+ if (ext !== '' || !specifier.startsWith('.')) {
362
+ return withDetectedModuleType(await nextResolve(specifier, context));
363
+ }
364
+
365
+ try {
366
+ return await nextResolve(specifier, context);
367
+ } catch (error) {
368
+ if (error.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
369
+ const spec = `${specifier}${specifier.endsWith('/') ? '' : '/'}index`;
370
+ const resolved = await resolveWithoutExt(spec, context, nextResolve);
371
+ if (resolved) {
372
+ return withDetectedModuleType(resolved);
373
+ }
374
+ } else if (error.code === 'ERR_MODULE_NOT_FOUND') {
375
+ const resolved = await resolveWithoutExt(specifier, context, nextResolve);
376
+ if (resolved) {
377
+ return withDetectedModuleType(resolved);
378
+ }
379
+ }
380
+
381
+ throw error;
382
+ }
383
+ }
384
+
385
+ /** @type {import('module').ResolveHook} */
386
+ async function resolveWithoutExt(specifier, context, nextResolve) {
387
+ for (const tryExt of SRC_EXTS) {
388
+ try {
389
+ const resolved = await nextResolve(specifier + tryExt, {
390
+ ...context,
391
+ format: 'commonjs',
392
+ });
393
+ return {
394
+ ...resolved,
395
+ format: moduleTypeTable[tryExt] ?? resolved.format,
396
+ };
397
+ } catch {
398
+ /* ignore */
399
+ }
400
+ }
401
+ return undefined;
402
+ }
403
+
404
+ /**
405
+ * Asynchronous load hook for use with `module.register()`, see `resolve`.
406
+ *
407
+ * @type {import('module').LoadHook}
408
+ */
409
+ export async function load(url, context, nextLoad) {
410
+ return loadImpl(url, context, nextLoad);
411
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/cli-node",
3
- "version": "0.3.6-next.0",
3
+ "version": "0.3.6-next.1",
4
4
  "description": "Node.js library for Backstage CLIs",
5
5
  "backstage": {
6
6
  "role": "node-library"
@@ -48,8 +48,8 @@
48
48
  "zod": "^3.25.76 || ^4.0.0"
49
49
  },
50
50
  "devDependencies": {
51
- "@backstage/backend-test-utils": "1.11.9-next.0",
52
- "@backstage/cli": "0.36.8-next.0",
51
+ "@backstage/backend-test-utils": "1.11.9-next.1",
52
+ "@backstage/cli": "0.36.8-next.1",
53
53
  "@backstage/test-utils": "1.7.24-next.0",
54
54
  "@types/proper-lockfile": "^4",
55
55
  "@types/yarnpkg__lockfile": "^1.1.4"