@nx/js 20.4.0-beta.1 → 20.4.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/js",
3
- "version": "20.4.0-beta.1",
3
+ "version": "20.4.0-beta.2",
4
4
  "private": false,
5
5
  "description": "The JS plugin for Nx contains executors and generators that provide the best experience for developing JavaScript and TypeScript projects. ",
6
6
  "repository": {
@@ -39,8 +39,8 @@
39
39
  "@babel/preset-env": "^7.23.2",
40
40
  "@babel/preset-typescript": "^7.22.5",
41
41
  "@babel/runtime": "^7.22.6",
42
- "@nx/devkit": "20.4.0-beta.1",
43
- "@nx/workspace": "20.4.0-beta.1",
42
+ "@nx/devkit": "20.4.0-beta.2",
43
+ "@nx/workspace": "20.4.0-beta.2",
44
44
  "@zkochan/js-yaml": "0.0.7",
45
45
  "babel-plugin-const-enum": "^1.0.1",
46
46
  "babel-plugin-macros": "^2.8.0",
@@ -5,7 +5,7 @@
5
5
  "rootDir": "src",
6
6
  "outDir": "dist",
7
7
  "tsBuildInfoFile": "dist/tsconfig.lib.tsbuildinfo",
8
- "emitDeclarationOnly": false,<% if (compilerOptions.length) { %>
8
+ "emitDeclarationOnly": <%= emitDeclarationOnly %>,<% if (compilerOptions.length) { %>
9
9
  <%- compilerOptions %>,<% } %>
10
10
  "types": ["node"]
11
11
  },
@@ -24,6 +24,7 @@ const versions_1 = require("../../utils/versions");
24
24
  const init_1 = require("../init/init");
25
25
  const generator_1 = require("../setup-verdaccio/generator");
26
26
  const sort_fields_1 = require("../../utils/package-json/sort-fields");
27
+ const get_import_path_1 = require("../../utils/get-import-path");
27
28
  const defaultOutputDirectory = 'dist';
28
29
  async function libraryGenerator(tree, schema) {
29
30
  return await libraryGeneratorInternal(tree, {
@@ -43,6 +44,11 @@ async function libraryGeneratorInternal(tree, schema) {
43
44
  formatter: (0, ts_solution_setup_1.isUsingTsSolutionSetup)(tree) ? 'none' : 'prettier',
44
45
  }));
45
46
  const options = await normalizeOptions(tree, schema);
47
+ // If we are using the new TS solution
48
+ // We need to update the workspace file (package.json or pnpm-workspaces.yaml) to include the new project
49
+ if (options.isUsingTsSolutionConfig) {
50
+ (0, ts_solution_setup_1.addProjectToTsSolutionWorkspace)(tree, options.projectRoot);
51
+ }
46
52
  createFiles(tree, options);
47
53
  await configureProject(tree, options);
48
54
  if (!options.skipPackageJson) {
@@ -86,7 +92,8 @@ async function libraryGeneratorInternal(tree, schema) {
86
92
  if (options.unitTestRunner === 'jest') {
87
93
  const jestCallback = await addJest(tree, options);
88
94
  tasks.push(jestCallback);
89
- if (options.bundler === 'swc' || options.bundler === 'rollup') {
95
+ if (!options.isUsingTsSolutionConfig &&
96
+ (options.bundler === 'swc' || options.bundler === 'rollup')) {
90
97
  replaceJestConfig(tree, options);
91
98
  }
92
99
  }
@@ -138,11 +145,6 @@ async function libraryGeneratorInternal(tree, schema) {
138
145
  return json;
139
146
  });
140
147
  }
141
- // If we are using the new TS solution
142
- // We need to update the workspace file (package.json or pnpm-workspaces.yaml) to include the new project
143
- if (options.isUsingTsSolutionConfig) {
144
- (0, ts_solution_setup_1.addProjectToTsSolutionWorkspace)(tree, options.projectRoot);
145
- }
146
148
  (0, sort_fields_1.sortPackageJsonFields)(tree, options.projectRoot);
147
149
  if (!options.skipFormat) {
148
150
  await (0, devkit_1.formatFiles)(tree);
@@ -243,8 +245,19 @@ async function configureProject(tree, options) {
243
245
  if (!projectConfiguration.tags?.length) {
244
246
  delete projectConfiguration.tags;
245
247
  }
248
+ // We want a minimal setup, so unless targets and tags are set, just skip the `nx` property in `package.json`.
249
+ if (options.isUsingTsSolutionConfig) {
250
+ delete projectConfiguration.projectType;
251
+ // SWC executor has logic around sourceRoot and `--strip-leading-paths`. If it is not set then dist will contain the `src` folder rather than being flat.
252
+ // TODO(leo): Look at how we can remove the dependency on sourceRoot for SWC.
253
+ if (options.bundler !== 'swc') {
254
+ delete projectConfiguration.sourceRoot;
255
+ }
256
+ }
246
257
  // empty targets are cleaned up automatically by `updateProjectConfiguration`
247
- (0, devkit_1.updateProjectConfiguration)(tree, options.name, projectConfiguration);
258
+ (0, devkit_1.updateProjectConfiguration)(tree, options.isUsingTsSolutionConfig
259
+ ? options.importPath ?? options.name
260
+ : options.name, projectConfiguration);
248
261
  }
249
262
  else if (options.config === 'workspace' || options.config === 'project') {
250
263
  (0, devkit_1.addProjectConfiguration)(tree, options.name, projectConfiguration);
@@ -432,10 +445,8 @@ function createFiles(tree, options) {
432
445
  },
433
446
  ...determineEntryFields(options),
434
447
  };
435
- if (options.bundler === 'none') {
436
- updatedPackageJson.type = 'module';
437
- }
438
- else if (options.bundler !== 'vite' && options.bundler !== 'rollup') {
448
+ if (options.isUsingTsSolutionConfig &&
449
+ !['none', 'rollup', 'vite'].includes(options.bundler)) {
439
450
  return (0, update_package_json_1.getUpdatedPackageJsonContent)(updatedPackageJson, {
440
451
  main: (0, path_1.join)(options.projectRoot, 'src/index.ts'),
441
452
  outputPath: (0, devkit_1.joinPathFragments)(options.projectRoot, 'dist'),
@@ -464,21 +475,17 @@ function createFiles(tree, options) {
464
475
  // https://docs.npmjs.com/cli/v10/configuring-npm/package-json#files
465
476
  packageJson.files = ['dist', '!**/*.tsbuildinfo'];
466
477
  }
467
- if (options.isUsingTsSolutionConfig) {
468
- if (options.bundler === 'none') {
469
- packageJson.type = 'module';
470
- }
471
- else if (options.bundler !== 'vite' && options.bundler !== 'rollup') {
472
- packageJson = (0, update_package_json_1.getUpdatedPackageJsonContent)(packageJson, {
473
- main: (0, path_1.join)(options.projectRoot, 'src/index.ts'),
474
- outputPath: (0, devkit_1.joinPathFragments)(options.projectRoot, 'dist'),
475
- projectRoot: options.projectRoot,
476
- rootDir: (0, path_1.join)(options.projectRoot, 'src'),
477
- generateExportsField: true,
478
- packageJsonPath,
479
- format: ['esm'],
480
- });
481
- }
478
+ if (options.isUsingTsSolutionConfig &&
479
+ !['none', 'rollup', 'vite'].includes(options.bundler)) {
480
+ packageJson = (0, update_package_json_1.getUpdatedPackageJsonContent)(packageJson, {
481
+ main: (0, path_1.join)(options.projectRoot, 'src/index.ts'),
482
+ outputPath: (0, devkit_1.joinPathFragments)(options.projectRoot, 'dist'),
483
+ projectRoot: options.projectRoot,
484
+ rootDir: (0, path_1.join)(options.projectRoot, 'src'),
485
+ generateExportsField: true,
486
+ packageJsonPath,
487
+ format: ['esm'],
488
+ });
482
489
  }
483
490
  (0, devkit_1.writeJson)(tree, packageJsonPath, packageJson);
484
491
  }
@@ -509,12 +516,12 @@ async function addJest(tree, options) {
509
516
  setupFile: 'none',
510
517
  supportTsx: false,
511
518
  skipSerializers: true,
512
- testEnvironment: options.testEnvironment,
519
+ testEnvironment: options.testEnvironment ?? 'node',
513
520
  skipFormat: true,
514
- compiler: options.bundler === 'swc' || options.bundler === 'tsc'
515
- ? options.bundler
516
- : options.bundler === 'rollup'
517
- ? 'swc'
521
+ compiler: options.shouldUseSwcJest
522
+ ? 'swc'
523
+ : options.bundler === 'tsc'
524
+ ? 'tsc'
518
525
  : undefined,
519
526
  runtimeTsconfigFileName: 'tsconfig.lib.json',
520
527
  });
@@ -635,16 +642,22 @@ async function normalizeOptions(tree, options) {
635
642
  options.minimal ??= false;
636
643
  // We default to generate a project.json file if the new setup is not being used
637
644
  options.useProjectJson ??= !isUsingTsSolutionConfig;
645
+ const shouldUseSwcJest = options.bundler === 'swc' ||
646
+ options.bundler === 'rollup' ||
647
+ isUsingTsSolutionConfig;
638
648
  return {
639
649
  ...options,
640
650
  fileName,
641
- name: projectName,
651
+ name: isUsingTsSolutionConfig
652
+ ? (0, get_import_path_1.getImportPath)(tree, projectName)
653
+ : projectName,
642
654
  projectNames,
643
655
  projectRoot,
644
656
  parsedTags,
645
657
  importPath,
646
658
  hasPlugin,
647
659
  isUsingTsSolutionConfig,
660
+ shouldUseSwcJest,
648
661
  };
649
662
  }
650
663
  function addProjectDependencies(tree, options) {
@@ -728,6 +741,7 @@ function createProjectTsConfigs(tree, options) {
728
741
  outDir: options.bundler === 'tsc'
729
742
  ? 'dist'
730
743
  : `out-tsc/${options.projectRoot.split('/').pop()}`,
744
+ emitDeclarationOnly: options.bundler === 'tsc' ? false : true,
731
745
  });
732
746
  // tsconfig.json
733
747
  if (options.isUsingTsSolutionConfig) {
@@ -889,6 +903,7 @@ function determineEntryFields(options) {
889
903
  case 'none': {
890
904
  if (options.isUsingTsSolutionConfig) {
891
905
  return {
906
+ type: 'module',
892
907
  main: options.js ? './src/index.js' : './src/index.ts',
893
908
  types: options.js ? './src/index.js' : './src/index.ts',
894
909
  exports: {
@@ -46,4 +46,5 @@ export interface NormalizedLibraryGeneratorOptions
46
46
  importPath?: string;
47
47
  hasPlugin: boolean;
48
48
  isUsingTsSolutionConfig: boolean;
49
+ shouldUseSwcJest: boolean;
49
50
  }
@@ -1,3 +1,4 @@
1
1
  import { type Tree } from '@nx/devkit';
2
2
  export declare const defaultExclude: string[];
3
- export declare function addSwcConfig(tree: Tree, projectDir: string, type?: 'commonjs' | 'es6'): void;
3
+ export declare function addSwcConfig(tree: Tree, projectDir: string, type?: 'commonjs' | 'es6', supportTsx?: boolean): void;
4
+ export declare function addSwcTestConfig(tree: Tree, projectDir: string, type?: 'commonjs' | 'es6', supportTsx?: boolean): void;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defaultExclude = void 0;
4
4
  exports.addSwcConfig = addSwcConfig;
5
+ exports.addSwcTestConfig = addSwcTestConfig;
5
6
  const path_1 = require("path");
6
7
  exports.defaultExclude = [
7
8
  'jest.config.ts',
@@ -11,17 +12,25 @@ exports.defaultExclude = [
11
12
  './**/jest-setup.ts$',
12
13
  '.*.js$',
13
14
  ];
14
- const swcOptionsString = (type = 'commonjs') => `{
15
+ const swcOptionsString = (type = 'commonjs', exclude, supportTsx) => `{
15
16
  "jsc": {
16
17
  "target": "es2017",
17
18
  "parser": {
18
19
  "syntax": "typescript",
19
20
  "decorators": true,
20
- "dynamicImport": true
21
+ "dynamicImport": true${supportTsx
22
+ ? `,
23
+ "tsx": true`
24
+ : ''}
21
25
  },
22
26
  "transform": {
23
27
  "decoratorMetadata": true,
24
- "legacyDecorator": true
28
+ "legacyDecorator": true${supportTsx
29
+ ? `,
30
+ "react": {
31
+ "runtime": "automatic"
32
+ }`
33
+ : ''}
25
34
  },
26
35
  "keepClassNames": true,
27
36
  "externalHelpers": true,
@@ -31,11 +40,18 @@ const swcOptionsString = (type = 'commonjs') => `{
31
40
  "type": "${type}"
32
41
  },
33
42
  "sourceMaps": true,
34
- "exclude": ${JSON.stringify(exports.defaultExclude)}
35
- }`;
36
- function addSwcConfig(tree, projectDir, type = 'commonjs') {
43
+ "exclude": ${JSON.stringify(exclude)}
44
+ }
45
+ `;
46
+ function addSwcConfig(tree, projectDir, type = 'commonjs', supportTsx = false) {
37
47
  const swcrcPath = (0, path_1.join)(projectDir, '.swcrc');
38
48
  if (tree.exists(swcrcPath))
39
49
  return;
40
- tree.write(swcrcPath, swcOptionsString(type));
50
+ tree.write(swcrcPath, swcOptionsString(type, exports.defaultExclude, supportTsx));
51
+ }
52
+ function addSwcTestConfig(tree, projectDir, type = 'commonjs', supportTsx = false) {
53
+ const swcrcPath = (0, path_1.join)(projectDir, '.spec.swcrc');
54
+ if (tree.exists(swcrcPath))
55
+ return;
56
+ tree.write(swcrcPath, swcOptionsString(type, [], supportTsx));
41
57
  }
@@ -5,3 +5,4 @@ export declare function assertNotUsingTsSolutionSetup(tree: Tree, pluginName: st
5
5
  export declare function findRuntimeTsConfigName(tree: Tree, projectRoot: string): string | null;
6
6
  export declare function updateTsconfigFiles(tree: Tree, projectRoot: string, runtimeTsconfigFileName: string, compilerOptions: Record<string, string | boolean | string[]>, exclude?: string[], rootDir?: string): void;
7
7
  export declare function addProjectToTsSolutionWorkspace(tree: Tree, projectDir: string): void;
8
+ export declare function getProjectType(tree: Tree, projectRoot: string, projectType?: 'library' | 'application'): 'library' | 'application';
@@ -6,6 +6,7 @@ exports.assertNotUsingTsSolutionSetup = assertNotUsingTsSolutionSetup;
6
6
  exports.findRuntimeTsConfigName = findRuntimeTsConfigName;
7
7
  exports.updateTsconfigFiles = updateTsconfigFiles;
8
8
  exports.addProjectToTsSolutionWorkspace = addProjectToTsSolutionWorkspace;
9
+ exports.getProjectType = getProjectType;
9
10
  const devkit_1 = require("@nx/devkit");
10
11
  const posix_1 = require("node:path/posix");
11
12
  const tree_1 = require("nx/src/generators/tree");
@@ -172,3 +173,19 @@ function addProjectToTsSolutionWorkspace(tree, projectDir) {
172
173
  }
173
174
  }
174
175
  }
176
+ function getProjectType(tree, projectRoot, projectType) {
177
+ if (projectType)
178
+ return projectType;
179
+ if (tree.exists((0, devkit_1.joinPathFragments)(projectRoot, 'tsconfig.lib.json')))
180
+ return 'library';
181
+ if (tree.exists((0, devkit_1.joinPathFragments)(projectRoot, 'tsconfig.app.json')))
182
+ return 'application';
183
+ // If there are no exports, assume it is an application since both buildable and non-buildable libraries have exports.
184
+ const packageJsonPath = (0, devkit_1.joinPathFragments)(projectRoot, 'package.json');
185
+ const packageJson = tree.exists(packageJsonPath)
186
+ ? (0, devkit_1.readJson)(tree, (0, devkit_1.joinPathFragments)(projectRoot, 'package.json'))
187
+ : null;
188
+ if (!packageJson?.exports)
189
+ return 'application';
190
+ return 'library';
191
+ }