@nx/js 17.0.3 → 17.0.5

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.
Files changed (54) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +9 -4
  3. package/babel.js +36 -16
  4. package/executors.json +1 -1
  5. package/migrations.json +56 -0
  6. package/package.json +9 -9
  7. package/src/executors/node/node.impl.js +1 -1
  8. package/src/executors/node/schema.json +1 -1
  9. package/src/executors/release-publish/release-publish.impl.js +135 -32
  10. package/src/executors/release-publish/schema.d.ts +2 -0
  11. package/src/executors/release-publish/schema.json +1 -1
  12. package/src/executors/swc/schema.json +1 -1
  13. package/src/executors/swc/swc.impl.js +6 -7
  14. package/src/executors/verdaccio/schema.json +1 -1
  15. package/src/executors/verdaccio/verdaccio.impl.js +1 -1
  16. package/src/generators/convert-to-swc/schema.json +1 -1
  17. package/src/generators/init/init.js +3 -2
  18. package/src/generators/init/schema.d.ts +1 -0
  19. package/src/generators/init/schema.json +7 -1
  20. package/src/generators/library/files/lib/tsconfig.lib.json__tmpl__ +5 -1
  21. package/src/generators/library/files/{lib → readme}/README.md +1 -1
  22. package/src/generators/library/library.d.ts +1 -1
  23. package/src/generators/library/library.js +171 -29
  24. package/src/generators/library/schema.json +8 -2
  25. package/src/generators/release-version/release-version.d.ts +2 -1
  26. package/src/generators/release-version/release-version.js +341 -86
  27. package/src/generators/release-version/schema.json +26 -4
  28. package/src/generators/release-version/utils/resolve-local-package-dependencies.d.ts +7 -1
  29. package/src/generators/release-version/utils/resolve-local-package-dependencies.js +14 -4
  30. package/src/generators/release-version/utils/resolve-version-spec.js +4 -1
  31. package/src/generators/release-version/utils/update-lock-file.d.ts +5 -0
  32. package/src/generators/release-version/utils/update-lock-file.js +105 -0
  33. package/src/generators/setup-build/generator.js +3 -0
  34. package/src/generators/setup-build/schema.json +1 -1
  35. package/src/generators/setup-verdaccio/schema.json +1 -1
  36. package/src/utils/add-local-registry-scripts.js +26 -6
  37. package/src/utils/assets/copy-assets-handler.js +4 -4
  38. package/src/utils/buildable-libs-utils.js +7 -3
  39. package/src/utils/find-npm-dependencies.d.ts +2 -1
  40. package/src/utils/find-npm-dependencies.js +28 -11
  41. package/src/utils/inline.js +6 -4
  42. package/src/utils/npm-config.d.ts +25 -0
  43. package/src/utils/npm-config.js +90 -0
  44. package/src/utils/schema.d.ts +3 -0
  45. package/src/utils/swc/get-swcrc-path.d.ts +4 -1
  46. package/src/utils/swc/get-swcrc-path.js +6 -1
  47. package/src/utils/swc/inline.d.ts +1 -1
  48. package/src/utils/swc/inline.js +1 -2
  49. package/src/utils/typescript/ast-utils.js +2 -3
  50. package/src/utils/typescript/ts-config.js +1 -0
  51. package/src/utils/versions.d.ts +4 -4
  52. package/src/utils/versions.js +4 -4
  53. package/src/utils/minimal-publish-script.d.ts +0 -2
  54. package/src/utils/minimal-publish-script.js +0 -74
@@ -3,17 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.addLint = exports.libraryGeneratorInternal = exports.libraryGenerator = void 0;
4
4
  const devkit_1 = require("@nx/devkit");
5
5
  const project_name_and_root_utils_1 = require("@nx/devkit/src/generators/project-name-and-root-utils");
6
- const ts_config_1 = require("../../utils/typescript/ts-config");
6
+ const add_build_target_defaults_1 = require("@nx/devkit/src/generators/add-build-target-defaults");
7
+ const log_show_project_command_1 = require("@nx/devkit/src/utils/log-show-project-command");
8
+ const find_matching_projects_1 = require("nx/src/utils/find-matching-projects");
7
9
  const path_1 = require("path");
8
- const minimal_publish_script_1 = require("../../utils/minimal-publish-script");
9
10
  const add_swc_config_1 = require("../../utils/swc/add-swc-config");
10
11
  const add_swc_dependencies_1 = require("../../utils/swc/add-swc-dependencies");
12
+ const create_ts_config_1 = require("../../utils/typescript/create-ts-config");
13
+ const ts_config_1 = require("../../utils/typescript/ts-config");
11
14
  const versions_1 = require("../../utils/versions");
12
15
  const init_1 = require("../init/init");
13
16
  const generator_1 = require("../setup-verdaccio/generator");
14
- const create_ts_config_1 = require("../../utils/typescript/create-ts-config");
17
+ const defaultOutputDirectory = 'dist';
15
18
  async function libraryGenerator(tree, schema) {
16
19
  return await libraryGeneratorInternal(tree, {
20
+ addPlugin: false,
17
21
  // provide a default projectNameAndRootFormat to avoid breaking changes
18
22
  // to external generators invoking this one
19
23
  projectNameAndRootFormat: 'derived',
@@ -22,7 +26,6 @@ async function libraryGenerator(tree, schema) {
22
26
  }
23
27
  exports.libraryGenerator = libraryGenerator;
24
28
  async function libraryGeneratorInternal(tree, schema) {
25
- const filesDir = (0, path_1.join)(__dirname, './files');
26
29
  const tasks = [];
27
30
  tasks.push(await (0, init_1.default)(tree, {
28
31
  ...schema,
@@ -30,9 +33,11 @@ async function libraryGeneratorInternal(tree, schema) {
30
33
  tsConfigName: schema.rootProject ? 'tsconfig.json' : 'tsconfig.base.json',
31
34
  }));
32
35
  const options = await normalizeOptions(tree, schema);
33
- createFiles(tree, options, `${filesDir}/lib`);
34
- addProject(tree, options);
35
- tasks.push(addProjectDependencies(tree, options));
36
+ createFiles(tree, options);
37
+ await addProject(tree, options);
38
+ if (!options.skipPackageJson) {
39
+ tasks.push(addProjectDependencies(tree, options));
40
+ }
36
41
  if (options.publishable) {
37
42
  tasks.push(await (0, generator_1.default)(tree, { ...options, skipFormat: true }));
38
43
  }
@@ -46,6 +51,7 @@ async function libraryGeneratorInternal(tree, schema) {
46
51
  includeLib: true,
47
52
  skipFormat: true,
48
53
  testEnvironment: options.testEnvironment,
54
+ addPlugin: options.addPlugin,
49
55
  });
50
56
  tasks.push(viteTask);
51
57
  createOrEditViteConfig(tree, {
@@ -63,7 +69,7 @@ async function libraryGeneratorInternal(tree, schema) {
63
69
  const jestCallback = await addJest(tree, options);
64
70
  tasks.push(jestCallback);
65
71
  if (options.bundler === 'swc' || options.bundler === 'rollup') {
66
- replaceJestConfig(tree, options, `${filesDir}/jest-config`);
72
+ replaceJestConfig(tree, options);
67
73
  }
68
74
  }
69
75
  else if (options.unitTestRunner === 'vitest' &&
@@ -73,7 +79,7 @@ async function libraryGeneratorInternal(tree, schema) {
73
79
  const vitestTask = await vitestGenerator(tree, {
74
80
  project: options.name,
75
81
  uiFramework: 'none',
76
- coverageProvider: 'c8',
82
+ coverageProvider: 'v8',
77
83
  skipFormat: true,
78
84
  testEnvironment: options.testEnvironment,
79
85
  });
@@ -96,10 +102,18 @@ async function libraryGeneratorInternal(tree, schema) {
96
102
  if (!options.skipFormat) {
97
103
  await (0, devkit_1.formatFiles)(tree);
98
104
  }
105
+ if (options.publishable) {
106
+ tasks.push(() => {
107
+ logNxReleaseDocsInfo();
108
+ });
109
+ }
110
+ tasks.push(() => {
111
+ (0, log_show_project_command_1.logShowProjectCommand)(options.name);
112
+ });
99
113
  return (0, devkit_1.runTasksInSerial)(...tasks);
100
114
  }
101
115
  exports.libraryGeneratorInternal = libraryGeneratorInternal;
102
- function addProject(tree, options) {
116
+ async function addProject(tree, options) {
103
117
  const projectConfiguration = {
104
118
  root: options.projectRoot,
105
119
  sourceRoot: (0, devkit_1.joinPathFragments)(options.projectRoot, 'src'),
@@ -111,8 +125,10 @@ function addProject(tree, options) {
111
125
  options.bundler !== 'none' &&
112
126
  options.config !== 'npm-scripts') {
113
127
  const outputPath = getOutputPath(options);
128
+ const executor = getBuildExecutor(options.bundler);
129
+ (0, add_build_target_defaults_1.addBuildTargetDefaults)(tree, executor);
114
130
  projectConfiguration.targets.build = {
115
- executor: getBuildExecutor(options.bundler),
131
+ executor,
116
132
  outputs: ['{options.outputPath}'],
117
133
  options: {
118
134
  outputPath,
@@ -140,11 +156,24 @@ function addProject(tree, options) {
140
156
  projectConfiguration.targets.build.options.assets.push((0, devkit_1.joinPathFragments)(options.projectRoot, '*.md'));
141
157
  }
142
158
  if (options.publishable) {
143
- const publishScriptPath = (0, minimal_publish_script_1.addMinimalPublishScript)(tree);
144
- projectConfiguration.targets.publish = {
145
- command: `node ${publishScriptPath} ${options.name} {args.ver} {args.tag}`,
146
- dependsOn: ['build'],
159
+ const packageRoot = (0, path_1.join)(defaultOutputDirectory, '{projectRoot}');
160
+ projectConfiguration.targets ??= {};
161
+ projectConfiguration.targets['nx-release-publish'] = {
162
+ options: {
163
+ packageRoot,
164
+ },
165
+ };
166
+ projectConfiguration.release = {
167
+ version: {
168
+ generatorOptions: {
169
+ packageRoot,
170
+ // using git tags to determine the current version is required here because
171
+ // the version in the package root is overridden with every build
172
+ currentVersionResolver: 'git-tag',
173
+ },
174
+ },
147
175
  };
176
+ await addProjectToNxReleaseConfig(tree, options, projectConfiguration);
148
177
  }
149
178
  }
150
179
  if (options.config === 'workspace' || options.config === 'project') {
@@ -160,11 +189,8 @@ function addProject(tree, options) {
160
189
  }
161
190
  async function addLint(tree, options) {
162
191
  const { lintProjectGenerator } = (0, devkit_1.ensurePackage)('@nx/eslint', versions_1.nxVersion);
163
- const { mapLintPattern } =
164
- // nx-ignore-next-line
165
- require('@nx/eslint/src/generators/lint-project/lint-project');
166
192
  const projectConfiguration = (0, devkit_1.readProjectConfiguration)(tree, options.name);
167
- const task = lintProjectGenerator(tree, {
193
+ const task = await lintProjectGenerator(tree, {
168
194
  project: options.name,
169
195
  linter: options.linter,
170
196
  skipFormat: true,
@@ -172,11 +198,9 @@ async function addLint(tree, options) {
172
198
  (0, devkit_1.joinPathFragments)(options.projectRoot, 'tsconfig.lib.json'),
173
199
  ],
174
200
  unitTestRunner: options.unitTestRunner,
175
- eslintFilePatterns: [
176
- mapLintPattern(options.projectRoot, options.js ? 'js' : 'ts', options.rootProject),
177
- ],
178
201
  setParserOptionsProject: options.setParserOptionsProject,
179
202
  rootProject: options.rootProject,
203
+ addPlugin: options.addPlugin,
180
204
  });
181
205
  const { addOverrideToLintConfig, lintConfigHasOverride, isEslintConfigSupported, updateOverrideInLintConfig,
182
206
  // nx-ignore-next-line
@@ -278,10 +302,10 @@ function addBabelRc(tree, options) {
278
302
  };
279
303
  (0, devkit_1.writeJson)(tree, (0, path_1.join)(options.projectRoot, filename), babelrc);
280
304
  }
281
- function createFiles(tree, options, filesDir) {
305
+ function createFiles(tree, options) {
282
306
  const { className, name, propertyName } = (0, devkit_1.names)(options.projectNames.projectFileName);
283
307
  createProjectTsConfigJson(tree, options);
284
- (0, devkit_1.generateFiles)(tree, filesDir, options.projectRoot, {
308
+ (0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, './files/lib'), options.projectRoot, {
285
309
  ...options,
286
310
  dot: '.',
287
311
  className,
@@ -295,6 +319,22 @@ function createFiles(tree, options, filesDir) {
295
319
  buildable: options.bundler && options.bundler !== 'none',
296
320
  hasUnitTestRunner: options.unitTestRunner !== 'none',
297
321
  });
322
+ if (!options.rootProject) {
323
+ (0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, './files/readme'), options.projectRoot, {
324
+ ...options,
325
+ dot: '.',
326
+ className,
327
+ name,
328
+ propertyName,
329
+ js: !!options.js,
330
+ cliCommand: 'nx',
331
+ strict: undefined,
332
+ tmpl: '',
333
+ offsetFromRoot: (0, devkit_1.offsetFromRoot)(options.projectRoot),
334
+ buildable: options.bundler && options.bundler !== 'none',
335
+ hasUnitTestRunner: options.unitTestRunner !== 'none',
336
+ });
337
+ }
298
338
  if (options.bundler === 'swc' || options.bundler === 'rollup') {
299
339
  (0, add_swc_dependencies_1.addSwcDependencies)(tree);
300
340
  (0, add_swc_config_1.addSwcConfig)(tree, options.projectRoot, options.bundler === 'swc' ? 'commonjs' : 'es6');
@@ -318,6 +358,9 @@ function createFiles(tree, options, filesDir) {
318
358
  if (json.private && (options.publishable || options.rootProject)) {
319
359
  delete json.private;
320
360
  }
361
+ if (!options.publishable && !options.rootProject) {
362
+ json.private = true;
363
+ }
321
364
  return {
322
365
  ...json,
323
366
  dependencies: {
@@ -329,12 +372,16 @@ function createFiles(tree, options, filesDir) {
329
372
  });
330
373
  }
331
374
  else {
332
- (0, devkit_1.writeJson)(tree, packageJsonPath, {
375
+ const packageJson = {
333
376
  name: options.importPath,
334
377
  version: '0.0.1',
335
378
  dependencies: determineDependencies(options),
336
379
  ...determineEntryFields(options),
337
- });
380
+ };
381
+ if (!options.publishable && !options.rootProject) {
382
+ packageJson.private = true;
383
+ }
384
+ (0, devkit_1.writeJson)(tree, packageJsonPath, packageJson);
338
385
  }
339
386
  if (options.config === 'npm-scripts') {
340
387
  (0, devkit_1.updateJson)(tree, packageJsonPath, (json) => {
@@ -371,7 +418,8 @@ async function addJest(tree, options) {
371
418
  : undefined,
372
419
  });
373
420
  }
374
- function replaceJestConfig(tree, options, filesDir) {
421
+ function replaceJestConfig(tree, options) {
422
+ const filesDir = (0, path_1.join)(__dirname, './files/jest-config');
375
423
  // the existing config has to be deleted otherwise the new config won't overwrite it
376
424
  const existingJestConfig = (0, devkit_1.joinPathFragments)(filesDir, `jest.config.${options.js ? 'js' : 'ts'}`);
377
425
  if (tree.exists(existingJestConfig)) {
@@ -388,6 +436,10 @@ function replaceJestConfig(tree, options, filesDir) {
388
436
  });
389
437
  }
390
438
  async function normalizeOptions(tree, options) {
439
+ const nxJson = (0, devkit_1.readNxJson)(tree);
440
+ const addPlugin = process.env.NX_ADD_PLUGINS !== 'false' &&
441
+ nxJson.useInferencePlugins !== false;
442
+ options.addPlugin ??= addPlugin;
391
443
  /**
392
444
  * We are deprecating the compiler and the buildable options.
393
445
  * However, we want to keep the existing behavior for now.
@@ -422,7 +474,7 @@ async function normalizeOptions(tree, options) {
422
474
  options.bundler = 'tsc';
423
475
  }
424
476
  }
425
- // This is to preserve old behaviour, buildable: false
477
+ // This is to preserve old behavior, buildable: false
426
478
  if (options.publishable === false && options.buildable === false) {
427
479
  options.bundler = 'none';
428
480
  }
@@ -514,7 +566,7 @@ function getBuildExecutor(bundler) {
514
566
  }
515
567
  }
516
568
  function getOutputPath(options) {
517
- const parts = ['dist'];
569
+ const parts = [defaultOutputDirectory];
518
570
  if (options.projectRoot === '.') {
519
571
  parts.push(options.name);
520
572
  }
@@ -606,4 +658,94 @@ function determineEntryFields(options) {
606
658
  }
607
659
  }
608
660
  }
661
+ function projectsConfigMatchesProject(projectsConfig, project) {
662
+ if (!projectsConfig) {
663
+ return false;
664
+ }
665
+ if (typeof projectsConfig === 'string') {
666
+ projectsConfig = [projectsConfig];
667
+ }
668
+ const graph = {
669
+ [project.name]: project,
670
+ };
671
+ const matchingProjects = (0, find_matching_projects_1.findMatchingProjects)(projectsConfig, graph);
672
+ return matchingProjects.includes(project.name);
673
+ }
674
+ async function addProjectToNxReleaseConfig(tree, options, projectConfiguration) {
675
+ const nxJson = (0, devkit_1.readNxJson)(tree);
676
+ const addPreVersionCommand = () => {
677
+ const pmc = (0, devkit_1.getPackageManagerCommand)();
678
+ nxJson.release = {
679
+ ...nxJson.release,
680
+ version: {
681
+ preVersionCommand: `${pmc.dlx} nx run-many -t build`,
682
+ ...nxJson.release?.version,
683
+ },
684
+ };
685
+ };
686
+ if (!nxJson.release || (!nxJson.release.projects && !nxJson.release.groups)) {
687
+ // skip adding any projects configuration since the new project should be
688
+ // automatically included by nx release's default project detection logic
689
+ addPreVersionCommand();
690
+ (0, devkit_1.writeJson)(tree, 'nx.json', nxJson);
691
+ return;
692
+ }
693
+ const project = {
694
+ name: options.name,
695
+ type: 'lib',
696
+ data: {
697
+ root: projectConfiguration.root,
698
+ tags: projectConfiguration.tags,
699
+ },
700
+ };
701
+ if (projectsConfigMatchesProject(nxJson.release.projects, project)) {
702
+ devkit_1.output.log({
703
+ title: `Project already included in existing release configuration`,
704
+ });
705
+ addPreVersionCommand();
706
+ (0, devkit_1.writeJson)(tree, 'nx.json', nxJson);
707
+ return;
708
+ }
709
+ if (Array.isArray(nxJson.release.projects)) {
710
+ nxJson.release.projects.push(options.name);
711
+ addPreVersionCommand();
712
+ (0, devkit_1.writeJson)(tree, 'nx.json', nxJson);
713
+ devkit_1.output.log({
714
+ title: `Added project to existing release configuration`,
715
+ });
716
+ }
717
+ if (nxJson.release.groups) {
718
+ const allGroups = Object.entries(nxJson.release.groups);
719
+ for (const [name, group] of allGroups) {
720
+ if (projectsConfigMatchesProject(group.projects, project)) {
721
+ addPreVersionCommand();
722
+ (0, devkit_1.writeJson)(tree, 'nx.json', nxJson);
723
+ return `Project already included in existing release configuration for group ${name}`;
724
+ }
725
+ }
726
+ devkit_1.output.warn({
727
+ title: `Could not find a release group that includes ${options.name}`,
728
+ bodyLines: [
729
+ `Ensure that ${options.name} is included in a release group's "projects" list in nx.json so it can be published with "nx release"`,
730
+ ],
731
+ });
732
+ addPreVersionCommand();
733
+ (0, devkit_1.writeJson)(tree, 'nx.json', nxJson);
734
+ return;
735
+ }
736
+ if (typeof nxJson.release.projects === 'string') {
737
+ nxJson.release.projects = [nxJson.release.projects, options.name];
738
+ addPreVersionCommand();
739
+ (0, devkit_1.writeJson)(tree, 'nx.json', nxJson);
740
+ devkit_1.output.log({
741
+ title: `Added project to existing release configuration`,
742
+ });
743
+ return;
744
+ }
745
+ }
746
+ function logNxReleaseDocsInfo() {
747
+ devkit_1.output.log({
748
+ title: `📦 To learn how to publish this library, see https://nx.dev/core-features/manage-releases.`,
749
+ });
750
+ }
609
751
  exports.default = libraryGenerator;
@@ -1,5 +1,5 @@
1
1
  {
2
- "$schema": "http://json-schema.org/schema",
2
+ "$schema": "https://json-schema.org/schema",
3
3
  "$id": "NxTypescriptLibrary",
4
4
  "cli": "nx",
5
5
  "title": "Create a TypeScript Library",
@@ -48,6 +48,12 @@
48
48
  "default": false,
49
49
  "x-priority": "internal"
50
50
  },
51
+ "skipPackageJson": {
52
+ "description": "Do not add dependencies to `package.json`.",
53
+ "type": "boolean",
54
+ "default": false,
55
+ "x-priority": "internal"
56
+ },
51
57
  "skipTsConfig": {
52
58
  "type": "boolean",
53
59
  "description": "Do not update tsconfig.json for development experience.",
@@ -83,7 +89,7 @@
83
89
  "publishable": {
84
90
  "type": "boolean",
85
91
  "default": false,
86
- "description": "Generate a publishable library.",
92
+ "description": "Configure the library ready for use with `nx release` (https://nx.dev/core-features/manage-releases).",
87
93
  "x-priority": "important"
88
94
  },
89
95
  "importPath": {
@@ -1,4 +1,5 @@
1
1
  import { Tree } from '@nx/devkit';
2
+ import { ReleaseVersionGeneratorResult } from 'nx/src/command-line/release/version';
2
3
  import { ReleaseVersionGeneratorSchema } from './schema';
3
- export declare function releaseVersionGenerator(tree: Tree, options: ReleaseVersionGeneratorSchema): Promise<void>;
4
+ export declare function releaseVersionGenerator(tree: Tree, options: ReleaseVersionGeneratorSchema): Promise<ReleaseVersionGeneratorResult>;
4
5
  export default releaseVersionGenerator;