@smoothbricks/nx-plugin 0.3.0 → 0.3.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.
@@ -1,5 +1,6 @@
1
1
  import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
2
2
  import { join, relative } from 'node:path';
3
+ import { isDeepStrictEqual } from 'node:util';
3
4
  import type { Tree } from 'nx/src/devkit-exports.js';
4
5
  import { getProjects, readJson, readProjectConfiguration, updateJson, writeJson } from 'nx/src/devkit-exports.js';
5
6
 
@@ -86,7 +87,7 @@ export function checkTypecheckTestConfig(
86
87
  const tsconfigTestPath = join(packagePath, 'tsconfig.test.json');
87
88
  const compilerOptions = recordProperty(tsconfigTest, 'compilerOptions');
88
89
 
89
- if (!compilerOptions || compilerOptions.noEmit !== true) {
90
+ if (compilerOptions?.noEmit !== true) {
90
91
  issues.push({
91
92
  path: tsconfigTestPath,
92
93
  message: 'compilerOptions.noEmit must be true',
@@ -169,7 +170,10 @@ export function applyTypecheckTestDefaults(
169
170
  // Copy relevant compiler options from lib tsconfig
170
171
  if (options.libCompilerOptions) {
171
172
  for (const key of ['baseUrl', 'module', 'moduleResolution', 'jsx', 'lib']) {
172
- if (Object.hasOwn(options.libCompilerOptions, key) && compilerOptions[key] !== options.libCompilerOptions[key]) {
173
+ if (
174
+ Object.hasOwn(options.libCompilerOptions, key) &&
175
+ !isDeepStrictEqual(compilerOptions[key], options.libCompilerOptions[key])
176
+ ) {
173
177
  compilerOptions[key] = options.libCompilerOptions[key];
174
178
  changed = true;
175
179
  }
@@ -68,7 +68,7 @@ function syncBunLockfileVersions(root: string): number {
68
68
  }
69
69
  lockfile = lockfile.replace(pattern, `$1${targetVersion}$3`);
70
70
  console.log(
71
- `fix: ${relativePath}: ${lockVersion} -> ${targetVersion}${targetVersion !== pkg.version ? ` (latest stable tag; package.json has ${pkg.version})` : ''}`
71
+ `fix: ${relativePath}: ${lockVersion} -> ${targetVersion}${targetVersion !== pkg.version ? ` (latest stable tag; package.json has ${pkg.version})` : ''}`,
72
72
  );
73
73
  updated++;
74
74
  }
@@ -76,7 +76,7 @@ function syncBunLockfileVersions(root: string): number {
76
76
  writeFileSync(lockfilePath, lockfile);
77
77
  }
78
78
  console.log(
79
- updated > 0 ? `Updated ${updated} workspace version(s) in bun.lock` : 'All workspace versions already in sync.'
79
+ updated > 0 ? `Updated ${updated} workspace version(s) in bun.lock` : 'All workspace versions already in sync.',
80
80
  );
81
81
  return updated;
82
82
  }
@@ -98,15 +98,22 @@ function workspacePackages(root: string): WorkspacePackage[] {
98
98
  if (!existsSync(packageJsonPath)) {
99
99
  return null;
100
100
  }
101
- const json = JSON.parse(readFileSync(packageJsonPath, 'utf8')) as Record<string, unknown>;
102
- if (typeof json.name !== 'string' || typeof json.version !== 'string') return null;
103
- const nx = json.nx != null && typeof json.nx === 'object' ? (json.nx as Record<string, unknown>) : null;
101
+ const parsed: unknown = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
102
+ if (!isRecord(parsed) || typeof parsed.name !== 'string' || typeof parsed.version !== 'string') {
103
+ return null;
104
+ }
105
+ const json = parsed;
106
+ const nx = isRecord(json.nx) ? json.nx : null;
104
107
  const projectName = (nx ? (typeof nx.name === 'string' ? nx.name : null) : null) ?? json.name;
105
108
  return { path, name: json.name, projectName, version: json.version };
106
109
  })
107
110
  .filter((pkg): pkg is WorkspacePackage => pkg !== null);
108
111
  }
109
112
 
113
+ function isRecord(value: unknown): value is Record<string, unknown> {
114
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
115
+ }
116
+
110
117
  function escapeRegex(value: string): string {
111
118
  return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
112
119
  }
@@ -10,9 +10,13 @@ import {
10
10
  applyWorkspaceConfig,
11
11
  applyWorkspaceConfigPolicy,
12
12
  applyWorkspaceConfigTree,
13
+ BUILD_OUTPUT_DEPENDENCIES,
13
14
  checkWorkspaceConfig,
14
15
  checkWorkspaceConfigPolicy,
15
16
  checkWorkspaceConfigTree,
17
+ LINUX_PLATFORM_TARGET_GLOBS,
18
+ MACOS_PLATFORM_TARGET_GLOBS,
19
+ PLATFORM_TARGET_GLOBS,
16
20
  } from './workspace-config-policy.js';
17
21
 
18
22
  // ---------------------------------------------------------------------------
@@ -21,21 +25,7 @@ import {
21
25
 
22
26
  function validNxJson(): Record<string, unknown> {
23
27
  return {
24
- plugins: [
25
- {
26
- plugin: '@nx/js/typescript',
27
- options: {
28
- typecheck: { targetName: 'typecheck' },
29
- build: {
30
- targetName: 'tsc-js',
31
- configName: 'tsconfig.lib.json',
32
- buildDepsName: 'build-deps',
33
- watchDepsName: 'watch-deps',
34
- },
35
- },
36
- },
37
- '@smoothbricks/nx-plugin',
38
- ],
28
+ plugins: ['@smoothbricks/nx-plugin'],
39
29
  targetDefaults: validTargetDefaults(),
40
30
  namedInputs: validNamedInputs(),
41
31
  };
@@ -63,27 +53,32 @@ function validNamedInputs(): Record<string, unknown> {
63
53
  }
64
54
 
65
55
  function validPlugins(): unknown[] {
66
- return [
67
- {
68
- plugin: '@nx/js/typescript',
69
- options: {
70
- typecheck: { targetName: 'typecheck' },
71
- build: {
72
- targetName: 'tsc-js',
73
- configName: 'tsconfig.lib.json',
74
- buildDepsName: 'build-deps',
75
- watchDepsName: 'watch-deps',
76
- },
77
- },
78
- },
79
- '@smoothbricks/nx-plugin',
80
- ];
56
+ return ['@smoothbricks/nx-plugin'];
81
57
  }
82
58
 
83
59
  // ---------------------------------------------------------------------------
84
60
  // Layer 1: Pure core function tests
85
61
  // ---------------------------------------------------------------------------
86
62
 
63
+ describe('target family conventions', () => {
64
+ it('separates platform-only families from ordinary build outputs', () => {
65
+ expect(MACOS_PLATFORM_TARGET_GLOBS).toEqual(['*-macos', '*-ios']);
66
+ expect(LINUX_PLATFORM_TARGET_GLOBS).toEqual(['*-linux']);
67
+ expect(PLATFORM_TARGET_GLOBS).toEqual(['*-macos', '*-ios', '*-linux']);
68
+ expect(BUILD_OUTPUT_DEPENDENCIES).toEqual([
69
+ '*-js',
70
+ '*-web',
71
+ '*-html',
72
+ '*-css',
73
+ '*-android',
74
+ '*-native',
75
+ '*-napi',
76
+ '*-bun',
77
+ '*-wasm',
78
+ ]);
79
+ });
80
+ });
81
+
87
82
  describe('pure core: checkWorkspaceConfig', () => {
88
83
  it('returns no issues for valid config', () => {
89
84
  const issues = checkWorkspaceConfig(validNxJson());
@@ -103,8 +98,7 @@ describe('pure core: checkWorkspaceConfig', () => {
103
98
  targetDefaults: validTargetDefaults(),
104
99
  namedInputs: validNamedInputs(),
105
100
  });
106
- expect(issues.length).toBe(2);
107
- expect(issues.some((i) => i.message.includes('Official Nx owns TypeScript library inference'))).toBe(true);
101
+ expect(issues.length).toBe(1);
108
102
  expect(issues.some((i) => i.message.includes('plugins must include @smoothbricks/nx-plugin'))).toBe(true);
109
103
  });
110
104
 
@@ -120,16 +114,13 @@ describe('pure core: checkWorkspaceConfig', () => {
120
114
  expect(issues.some((i) => i.message.includes('must not use colon target names'))).toBe(true);
121
115
  });
122
116
 
123
- it('detects wrong tsc-js target name', () => {
117
+ it('detects native TypeScript inference', () => {
124
118
  const issues = checkWorkspaceConfig({
125
- plugins: [
126
- { plugin: '@nx/js/typescript', options: { build: { targetName: 'build' } } },
127
- '@smoothbricks/nx-plugin',
128
- ],
119
+ plugins: ['@nx/js/typescript', '@smoothbricks/nx-plugin'],
129
120
  targetDefaults: validTargetDefaults(),
130
121
  namedInputs: validNamedInputs(),
131
122
  });
132
- expect(issues.some((i) => i.message.includes('build.targetName must be tsc-js'))).toBe(true);
123
+ expect(issues.some((issue) => issue.message.includes('plugins must not configure @nx/js/typescript'))).toBe(true);
133
124
  });
134
125
 
135
126
  it('detects imprecise production inputs', () => {
@@ -183,10 +174,8 @@ describe('pure core: applyWorkspaceConfig', () => {
183
174
  namedInputs: validNamedInputs(),
184
175
  };
185
176
  expect(applyWorkspaceConfig(nxJson)).toBe(true);
186
- const pluginNames = (nxJson.plugins as unknown[]).map((p: unknown) =>
187
- typeof p === 'string' ? p : (p as Record<string, unknown>).plugin,
188
- );
189
- expect(pluginNames).toContain('@nx/js/typescript');
177
+ const pluginNames = readPluginNames(nxJson.plugins);
178
+ expect(pluginNames).not.toContain('@nx/js/typescript');
190
179
  expect(pluginNames).toContain('@smoothbricks/nx-plugin');
191
180
  });
192
181
 
@@ -199,7 +188,7 @@ describe('pure core: applyWorkspaceConfig', () => {
199
188
  },
200
189
  };
201
190
  expect(applyWorkspaceConfig(nxJson)).toBe(true);
202
- expect((nxJson.targetDefaults as Record<string, unknown>)['build:wasm']).toBeUndefined();
191
+ expect(expectRecord(nxJson.targetDefaults)['build:wasm']).toBeUndefined();
203
192
  });
204
193
 
205
194
  it('fixes imprecise production inputs', () => {
@@ -212,7 +201,7 @@ describe('pure core: applyWorkspaceConfig', () => {
212
201
  },
213
202
  };
214
203
  expect(applyWorkspaceConfig(nxJson)).toBe(true);
215
- const namedInputs = nxJson.namedInputs as Record<string, unknown>;
204
+ const namedInputs = expectRecord(nxJson.namedInputs);
216
205
  expect(namedInputs.production).toEqual([
217
206
  '{projectRoot}/src/**/*',
218
207
  '{projectRoot}/package.json',
@@ -258,8 +247,7 @@ describe('Tree: checkWorkspaceConfigTree', () => {
258
247
  });
259
248
 
260
249
  const issues = checkWorkspaceConfigTree(tree);
261
- expect(issues.length).toBe(2);
262
- expect(issues.some((i) => i.message.includes('Official Nx owns TypeScript library inference'))).toBe(true);
250
+ expect(issues.length).toBe(1);
263
251
  expect(issues.some((i) => i.message.includes('plugins must include @smoothbricks/nx-plugin'))).toBe(true);
264
252
  });
265
253
 
@@ -289,10 +277,8 @@ describe('Tree: applyWorkspaceConfigTree', () => {
289
277
  expect(applyWorkspaceConfigTree(tree)).toBe(true);
290
278
 
291
279
  const nxJson = readJson(tree, 'nx.json');
292
- const pluginNames = (nxJson.plugins as unknown[]).map((p: unknown) =>
293
- typeof p === 'string' ? p : (p as Record<string, unknown>).plugin,
294
- );
295
- expect(pluginNames).toContain('@nx/js/typescript');
280
+ const pluginNames = readPluginNames(nxJson.plugins);
281
+ expect(pluginNames).not.toContain('@nx/js/typescript');
296
282
  expect(pluginNames).toContain('@smoothbricks/nx-plugin');
297
283
 
298
284
  // Tree version now passes check
@@ -317,11 +303,11 @@ describe('Tree: applyWorkspaceConfigTree', () => {
317
303
  expect(applyWorkspaceConfigTree(tree)).toBe(true);
318
304
 
319
305
  const nxJson = readJson(tree, 'nx.json');
320
- expect((nxJson.targetDefaults as Record<string, unknown>).build).toEqual({
306
+ expect(expectRecord(nxJson.targetDefaults).build).toEqual({
321
307
  cache: true,
322
308
  outputs: ['{projectRoot}/dist'],
323
309
  });
324
- expect((nxJson.targetDefaults as Record<string, unknown>).clean).toEqual({
310
+ expect(expectRecord(nxJson.targetDefaults).clean).toEqual({
325
311
  executor: '@smoothbricks/nx-plugin:clean-outputs',
326
312
  cache: false,
327
313
  });
@@ -349,11 +335,9 @@ describe('filesystem: checkWorkspaceConfigPolicy / applyWorkspaceConfigPolicy',
349
335
 
350
336
  expect(applyWorkspaceConfigPolicy(root)).toBe(true);
351
337
 
352
- const nxJson = JSON.parse(await readFile(join(root, 'nx.json'), 'utf8'));
353
- const pluginNames = (nxJson.plugins as unknown[]).map((p: unknown) =>
354
- typeof p === 'string' ? p : (p as Record<string, unknown>).plugin,
355
- );
356
- expect(pluginNames).toContain('@nx/js/typescript');
338
+ const nxJson = expectRecord(JSON.parse(await readFile(join(root, 'nx.json'), 'utf8')));
339
+ const pluginNames = readPluginNames(nxJson.plugins);
340
+ expect(pluginNames).not.toContain('@nx/js/typescript');
357
341
  expect(pluginNames).toContain('@smoothbricks/nx-plugin');
358
342
 
359
343
  // No issues after fix
@@ -386,3 +370,22 @@ async function writeJsonFile(path: string, value: unknown): Promise<void> {
386
370
  await mkdir(dirname(path), { recursive: true });
387
371
  await writeFile(path, `${JSON.stringify(value, null, 2)}\n`);
388
372
  }
373
+
374
+ function expectRecord(value: unknown): Record<string, unknown> {
375
+ if (!isRecord(value)) {
376
+ throw new Error('expected object');
377
+ }
378
+ return value;
379
+ }
380
+
381
+ function isRecord(value: unknown): value is Record<string, unknown> {
382
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
383
+ }
384
+
385
+ function readPluginNames(value: unknown): unknown[] {
386
+ if (!Array.isArray(value)) {
387
+ throw new Error('expected plugins array');
388
+ }
389
+ const plugins: unknown[] = value;
390
+ return plugins.map((plugin) => (typeof plugin === 'string' ? plugin : expectRecord(plugin).plugin));
391
+ }
@@ -13,13 +13,16 @@ export const BUILD_OUTPUT_DEPENDENCIES = [
13
13
  '*-web',
14
14
  '*-html',
15
15
  '*-css',
16
- '*-ios',
17
16
  '*-android',
18
17
  '*-native',
19
18
  '*-napi',
20
19
  '*-bun',
21
20
  '*-wasm',
22
- ];
21
+ ] as const;
22
+
23
+ export const MACOS_PLATFORM_TARGET_GLOBS = ['*-macos', '*-ios'] as const;
24
+ export const LINUX_PLATFORM_TARGET_GLOBS = ['*-linux'] as const;
25
+ export const PLATFORM_TARGET_GLOBS = [...MACOS_PLATFORM_TARGET_GLOBS, ...LINUX_PLATFORM_TARGET_GLOBS] as const;
23
26
 
24
27
  const nxJsTypescriptPlugin = '@nx/js/typescript';
25
28
  const smoothBricksNxPlugin = '@smoothbricks/nx-plugin';
@@ -66,20 +69,12 @@ export function checkWorkspaceConfig(nxJson: Record<string, unknown>): NxPolicyI
66
69
 
67
70
  // Plugin configuration
68
71
  const plugins = Array.isArray(nxJson.plugins) ? nxJson.plugins : [];
69
- const nxJsPlugin = plugins.find(isNxJsTypescriptPlugin);
70
- if (!nxJsPlugin) {
72
+ if (plugins.some(isNxJsTypescriptPlugin)) {
71
73
  issues.push({
72
74
  path: 'nx.json',
73
75
  message:
74
- `plugins must configure ${nxJsTypescriptPlugin}. ` +
75
- 'Official Nx owns TypeScript library inference; smoo configures it so tsconfig.lib.json produces tsc-js and leaves build available as an aggregate target.',
76
- });
77
- } else if (nxJsBuildTargetName(nxJsPlugin) !== 'tsc-js') {
78
- issues.push({
79
- path: 'nx.json',
80
- message:
81
- `${nxJsTypescriptPlugin} build.targetName must be tsc-js. ` +
82
- 'TypeScript library output is a concrete tool-output target; build is reserved for aggregate targets that depend on concrete build work.',
76
+ `plugins must not configure ${nxJsTypescriptPlugin}. ` +
77
+ 'Smoo owns transformer-aware TypeScript targets; Nx native inference only supports tsc/tsgo.',
83
78
  });
84
79
  }
85
80
  if (!plugins.includes(smoothBricksNxPlugin) && !plugins.some(isSmoothBricksNxPluginRecord)) {
@@ -104,10 +99,8 @@ export function applyWorkspaceConfig(nxJson: Record<string, unknown>): boolean {
104
99
  changed = applyCleanTargetDefault(nxJson) || changed;
105
100
  changed = applyNamedInputDefaults(nxJson) || changed;
106
101
  const currentPlugins = Array.isArray(nxJson.plugins) ? nxJson.plugins : [];
107
- const nextPlugins = upsertNxPlugin(
108
- upsertNxPlugin(currentPlugins, expectedNxJsTypescriptPlugin()),
109
- smoothBricksNxPlugin,
110
- );
102
+ const pluginsWithoutNxTypeScript = currentPlugins.filter((plugin) => !isNxJsTypescriptPlugin(plugin));
103
+ const nextPlugins = upsertNxPlugin(pluginsWithoutNxTypeScript, smoothBricksNxPlugin);
111
104
  if (JSON.stringify(currentPlugins) !== JSON.stringify(nextPlugins)) {
112
105
  nxJson.plugins = nextPlugins;
113
106
  changed = true;
@@ -181,7 +174,7 @@ function validateBuildTargetDefault(
181
174
  ): void {
182
175
  const targetDefaults = recordProperty(nxJson, 'targetDefaults');
183
176
  const build = targetDefaults ? recordProperty(targetDefaults, 'build') : null;
184
- if (!build || build.cache !== true) {
177
+ if (build?.cache !== true) {
185
178
  issues.push({ path: nxJsonPath, message: 'targetDefaults.build.cache must be true' });
186
179
  }
187
180
  const outputs = build?.outputs;
@@ -200,7 +193,7 @@ function validateCleanTargetDefault(
200
193
  if (!clean || clean.executor !== cleanOutputsExecutor) {
201
194
  issues.push({ path: nxJsonPath, message: `targetDefaults.clean.executor must be ${cleanOutputsExecutor}` });
202
195
  }
203
- if (!clean || clean.cache !== false) {
196
+ if (clean?.cache !== false) {
204
197
  issues.push({ path: nxJsonPath, message: 'targetDefaults.clean.cache must be false' });
205
198
  }
206
199
  }
@@ -311,21 +304,6 @@ function isPreciseProductionNamedInput(production: unknown[]): boolean {
311
304
  return hasPositiveProjectInput;
312
305
  }
313
306
 
314
- function expectedNxJsTypescriptPlugin(): Record<string, unknown> {
315
- return {
316
- plugin: nxJsTypescriptPlugin,
317
- options: {
318
- typecheck: { targetName: 'typecheck' },
319
- build: {
320
- targetName: 'tsc-js',
321
- configName: 'tsconfig.lib.json',
322
- buildDepsName: 'build-deps',
323
- watchDepsName: 'watch-deps',
324
- },
325
- },
326
- };
327
- }
328
-
329
307
  function upsertNxPlugin(plugins: readonly unknown[], plugin: string | Record<string, unknown>): unknown[] {
330
308
  const pluginName = typeof plugin === 'string' ? plugin : stringProperty(plugin, 'plugin');
331
309
  const next = plugins.filter((entry) => nxPluginName(entry) !== pluginName);
@@ -340,20 +318,16 @@ function nxPluginName(value: unknown): string | null {
340
318
  return isRecord(value) ? stringProperty(value, 'plugin') : null;
341
319
  }
342
320
 
343
- function isNxJsTypescriptPlugin(value: unknown): value is Record<string, unknown> {
344
- return isRecord(value) && stringProperty(value, 'plugin') === nxJsTypescriptPlugin;
321
+ function isNxJsTypescriptPlugin(value: unknown): boolean {
322
+ return (
323
+ value === nxJsTypescriptPlugin || (isRecord(value) && stringProperty(value, 'plugin') === nxJsTypescriptPlugin)
324
+ );
345
325
  }
346
326
 
347
327
  function isSmoothBricksNxPluginRecord(value: unknown): boolean {
348
328
  return isRecord(value) && stringProperty(value, 'plugin') === smoothBricksNxPlugin;
349
329
  }
350
330
 
351
- function nxJsBuildTargetName(plugin: Record<string, unknown>): string | null {
352
- const options = recordProperty(plugin, 'options');
353
- const build = options ? recordProperty(options, 'build') : null;
354
- return build ? stringProperty(build, 'targetName') : null;
355
- }
356
-
357
331
  function readJsonObject(path: string): Record<string, unknown> | null {
358
332
  if (!existsSync(path)) {
359
333
  return null;