@nx/js 0.0.0-pr-29314-e664f92 → 0.0.0-pr-29314-bfd9d0c

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": "0.0.0-pr-29314-e664f92",
3
+ "version": "0.0.0-pr-29314-bfd9d0c",
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": "0.0.0-pr-29314-e664f92",
43
- "@nx/workspace": "0.0.0-pr-29314-e664f92",
42
+ "@nx/devkit": "0.0.0-pr-29314-bfd9d0c",
43
+ "@nx/workspace": "0.0.0-pr-29314-bfd9d0c",
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",
@@ -1 +1 @@
1
- export * from './lib/<%= fileName %>';
1
+ export * from './lib/<%= fileNameImport %>';
@@ -22,6 +22,7 @@ const ts_solution_setup_1 = require("../../utils/typescript/ts-solution-setup");
22
22
  const versions_1 = require("../../utils/versions");
23
23
  const init_1 = require("../init/init");
24
24
  const generator_1 = require("../setup-verdaccio/generator");
25
+ const ensure_typescript_1 = require("../../utils/typescript/ensure-typescript");
25
26
  const defaultOutputDirectory = 'dist';
26
27
  async function libraryGenerator(tree, schema) {
27
28
  return await libraryGeneratorInternal(tree, {
@@ -335,6 +336,19 @@ function addBabelRc(tree, options) {
335
336
  function createFiles(tree, options) {
336
337
  const { className, name, propertyName } = (0, devkit_1.names)(options.projectNames.projectFileName);
337
338
  createProjectTsConfigs(tree, options);
339
+ let fileNameImport = options.fileName;
340
+ if (options.bundler === 'vite') {
341
+ const tsConfig = (0, ts_config_1.readTsConfigFromTree)(tree, (0, path_1.join)(options.projectRoot, 'tsconfig.lib.json'));
342
+ const ts = (0, ensure_typescript_1.ensureTypescript)();
343
+ if (tsConfig.options.moduleResolution === ts.ModuleResolutionKind.Node16 ||
344
+ tsConfig.options.moduleResolution === ts.ModuleResolutionKind.NodeNext) {
345
+ // Node16 and NodeNext require explicit file extensions for relative
346
+ // import paths. Since we generate the file with the `.ts` extension,
347
+ // we import it from the same file with the `.js` extension.
348
+ // https://www.typescriptlang.org/docs/handbook/modules/reference.html#file-extension-substitution
349
+ fileNameImport = `${options.fileName}.js`;
350
+ }
351
+ }
338
352
  (0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, './files/lib'), options.projectRoot, {
339
353
  ...options,
340
354
  dot: '.',
@@ -348,6 +362,7 @@ function createFiles(tree, options) {
348
362
  offsetFromRoot: (0, devkit_1.offsetFromRoot)(options.projectRoot),
349
363
  buildable: options.bundler && options.bundler !== 'none',
350
364
  hasUnitTestRunner: options.unitTestRunner !== 'none',
365
+ fileNameImport,
351
366
  });
352
367
  if (!options.rootProject) {
353
368
  (0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, './files/readme'), options.projectRoot, {
@@ -686,6 +701,9 @@ function createProjectTsConfigs(tree, options) {
686
701
  .map(([k, v]) => `${JSON.stringify(k)}: ${JSON.stringify(v)}`)
687
702
  .join(',\n '),
688
703
  tmpl: '',
704
+ outDir: options.bundler === 'tsc'
705
+ ? 'dist'
706
+ : `out-tsc/${options.projectRoot.split('/').pop()}`,
689
707
  });
690
708
  // tsconfig.json
691
709
  if (options.isUsingTsSolutionConfig) {
@@ -793,14 +811,10 @@ function determineEntryFields(options) {
793
811
  };
794
812
  case 'vite':
795
813
  return {
796
- // Since we're publishing both formats, skip the type field.
797
- // Bundlers or Node will determine the entry point to use.
814
+ type: 'module',
798
815
  main: options.isUsingTsSolutionConfig
799
816
  ? './dist/index.js'
800
817
  : './index.js',
801
- module: options.isUsingTsSolutionConfig
802
- ? './dist/index.mjs'
803
- : './index.mjs',
804
818
  typings: options.isUsingTsSolutionConfig
805
819
  ? './dist/index.d.ts'
806
820
  : './index.d.ts',
@@ -325,7 +325,7 @@ function getTsConfigPathFromReferencePath(tree, ownerTsConfigPath, referencePath
325
325
  */
326
326
  function patchTsconfigJsonReferences(tree, tsconfigInfoCaches, tsconfigPath, updatedReferences) {
327
327
  const stringifiedJsonContents = readRawTsconfigContents(tree, tsconfigInfoCaches, tsconfigPath);
328
- const edits = (0, jsonc_parser_1.modify)(stringifiedJsonContents, ['references'], updatedReferences, { formattingOptions: { keepLines: false } });
328
+ const edits = (0, jsonc_parser_1.modify)(stringifiedJsonContents, ['references'], updatedReferences, { formattingOptions: { keepLines: true, insertSpaces: true, tabSize: 2 } });
329
329
  const updatedJsonContents = (0, jsonc_parser_1.applyEdits)(stringifiedJsonContents, edits);
330
330
  // The final contents will be formatted by formatFiles() later
331
331
  tree.write(tsconfigPath, updatedJsonContents);
@@ -1,6 +1,7 @@
1
1
  import { Tree } from '@nx/devkit';
2
2
  import type * as ts from 'typescript';
3
3
  export declare function readTsConfig(tsConfigPath: string, sys?: ts.System): ts.ParsedCommandLine;
4
+ export declare function readTsConfigFromTree(tree: Tree, tsConfigPath: string): ts.ParsedCommandLine;
4
5
  export declare function getRootTsConfigPathInTree(tree: Tree): string | null;
5
6
  export declare function getRelativePathToRootTsConfig(tree: Tree, targetPath: string): string;
6
7
  export declare function getRootTsConfigPath(): string | null;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.readTsConfig = readTsConfig;
4
+ exports.readTsConfigFromTree = readTsConfigFromTree;
4
5
  exports.getRootTsConfigPathInTree = getRootTsConfigPathInTree;
5
6
  exports.getRelativePathToRootTsConfig = getRelativePathToRootTsConfig;
6
7
  exports.getRootTsConfigPath = getRootTsConfigPath;
@@ -20,6 +21,16 @@ function readTsConfig(tsConfigPath, sys) {
20
21
  const readResult = tsModule.readConfigFile(tsConfigPath, sys.readFile);
21
22
  return tsModule.parseJsonConfigFileContent(readResult.config, sys, (0, path_1.dirname)(tsConfigPath));
22
23
  }
24
+ function readTsConfigFromTree(tree, tsConfigPath) {
25
+ if (!tsModule) {
26
+ tsModule = (0, ensure_typescript_1.ensureTypescript)();
27
+ }
28
+ const tsSysFromTree = {
29
+ ...tsModule.sys,
30
+ readFile: (path) => tree.read(path, 'utf-8'),
31
+ };
32
+ return readTsConfig(tsConfigPath, tsSysFromTree);
33
+ }
23
34
  function getRootTsConfigPathInTree(tree) {
24
35
  for (const path of ['tsconfig.base.json', 'tsconfig.json']) {
25
36
  if (tree.exists(path)) {
@@ -4,3 +4,4 @@ export declare function isUsingTsSolutionSetup(tree?: Tree): boolean;
4
4
  export declare function assertNotUsingTsSolutionSetup(tree: Tree, pluginName: string, generatorName: string): void;
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
+ export declare function addProjectToTsSolutionWorkspace(tree: Tree, projectDir: string): void;
@@ -5,6 +5,7 @@ exports.isUsingTsSolutionSetup = isUsingTsSolutionSetup;
5
5
  exports.assertNotUsingTsSolutionSetup = assertNotUsingTsSolutionSetup;
6
6
  exports.findRuntimeTsConfigName = findRuntimeTsConfigName;
7
7
  exports.updateTsconfigFiles = updateTsconfigFiles;
8
+ exports.addProjectToTsSolutionWorkspace = addProjectToTsSolutionWorkspace;
8
9
  const devkit_1 = require("@nx/devkit");
9
10
  const tree_1 = require("nx/src/generators/tree");
10
11
  const package_manager_workspaces_1 = require("../package-manager-workspaces");
@@ -66,7 +67,7 @@ function assertNotUsingTsSolutionSetup(tree, pluginName, generatorName) {
66
67
  `We're working hard to support the existing TypeScript setup with the ${artifactString}. We'll soon release a new version of Nx with support for it.`,
67
68
  ],
68
69
  });
69
- process.exit(1);
70
+ throw new Error(`The ${artifactString} doesn't yet support the existing TypeScript setup. See the error above.`);
70
71
  }
71
72
  function findRuntimeTsConfigName(tree, projectRoot) {
72
73
  if (tree.exists((0, devkit_1.joinPathFragments)(projectRoot, 'tsconfig.app.json')))
@@ -94,8 +95,20 @@ function updateTsconfigFiles(tree, projectRoot, runtimeTsconfigFileName, compile
94
95
  rootDir,
95
96
  ...compilerOptions,
96
97
  };
98
+ if (rootDir && rootDir !== '.') {
99
+ // when rootDir is different from '.', the tsbuildinfo file is output
100
+ // at `<outDir>/<relative path to config from rootDir>/`, so we need
101
+ // to set it explicitly to ensure it's output to the outDir
102
+ // https://www.typescriptlang.org/tsconfig/#tsBuildInfoFile
103
+ json.compilerOptions.tsBuildInfoFile = (0, posix_1.join)('out-tsc', projectRoot.split('/').at(-1), (0, posix_1.basename)(runtimeTsconfigFileName, '.json') + '.tsbuildinfo');
104
+ }
105
+ else if (json.compilerOptions.tsBuildInfoFile) {
106
+ // when rootDir is '.' or not set, it would be output to the outDir, so
107
+ // we don't need to set it explicitly
108
+ delete json.compilerOptions.tsBuildInfoFile;
109
+ }
97
110
  const excludeSet = json.exclude
98
- ? new Set(['dist', ...json.exclude, ...exclude])
111
+ ? new Set(['out-tsc', 'dist', ...json.exclude, ...exclude])
99
112
  : new Set(exclude);
100
113
  json.exclude = Array.from(excludeSet);
101
114
  return json;
@@ -135,3 +148,32 @@ function updateTsconfigFiles(tree, projectRoot, runtimeTsconfigFileName, compile
135
148
  });
136
149
  }
137
150
  }
151
+ function addProjectToTsSolutionWorkspace(tree, projectDir) {
152
+ // If dir is "libs/foo" then use "libs/**" so we don't need so many entries in the workspace file.
153
+ // If the dir is just "foo" then we have to add it as is.
154
+ const baseDir = (0, posix_1.dirname)(projectDir);
155
+ const pattern = baseDir === '.' ? projectDir : `${baseDir}/**`;
156
+ if (tree.exists('pnpm-workspace.yaml')) {
157
+ const { load, dump } = require('@zkochan/js-yaml');
158
+ const workspaceFile = tree.read('pnpm-workspace.yaml', 'utf-8');
159
+ const yamlData = load(workspaceFile);
160
+ if (!yamlData?.packages) {
161
+ yamlData.packages = [];
162
+ }
163
+ if (!yamlData.packages.includes(pattern)) {
164
+ yamlData.packages.push(pattern);
165
+ tree.write('pnpm-workspace.yaml', dump(yamlData, { indent: 2, quotingType: '"', forceQuotes: true }));
166
+ }
167
+ }
168
+ else {
169
+ // Update package.json
170
+ const packageJson = (0, devkit_1.readJson)(tree, 'package.json');
171
+ if (!packageJson.workspaces) {
172
+ packageJson.workspaces = [];
173
+ }
174
+ if (!packageJson.workspaces.includes(pattern)) {
175
+ packageJson.workspaces.push(pattern);
176
+ tree.write('package.json', JSON.stringify(packageJson, null, 2));
177
+ }
178
+ }
179
+ }