@nx/esbuild 20.0.8 → 20.1.0-beta.0

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/esbuild",
3
- "version": "20.0.8",
3
+ "version": "20.1.0-beta.0",
4
4
  "private": false,
5
5
  "description": "The Nx Plugin for esbuild contains executors and generators that support building applications using esbuild",
6
6
  "repository": {
@@ -31,8 +31,8 @@
31
31
  "migrations": "./migrations.json"
32
32
  },
33
33
  "dependencies": {
34
- "@nx/devkit": "20.0.8",
35
- "@nx/js": "20.0.8",
34
+ "@nx/devkit": "20.1.0-beta.0",
35
+ "@nx/js": "20.1.0-beta.0",
36
36
  "fast-glob": "3.2.7",
37
37
  "picocolors": "^1.1.0",
38
38
  "tsconfig-paths": "^4.1.2",
@@ -2,6 +2,6 @@ import * as esbuild from 'esbuild';
2
2
  import { ExecutorContext, ProjectGraphProjectNode } from '@nx/devkit';
3
3
  import { NormalizedEsBuildExecutorOptions } from '../schema';
4
4
  export declare function buildEsbuildOptions(format: 'cjs' | 'esm', options: NormalizedEsBuildExecutorOptions, context: ExecutorContext): esbuild.BuildOptions;
5
- export declare function getOutExtension(format: 'cjs' | 'esm', options: NormalizedEsBuildExecutorOptions): '.cjs' | '.mjs' | '.js';
5
+ export declare function getOutExtension(format: 'cjs' | 'esm', options: Pick<NormalizedEsBuildExecutorOptions, 'userDefinedBuildOptions'>): '.cjs' | '.mjs' | '.js';
6
6
  export declare function getOutfile(format: 'cjs' | 'esm', options: NormalizedEsBuildExecutorOptions, context: ExecutorContext): string;
7
7
  export declare function getRegisterFileContent(project: ProjectGraphProjectNode, paths: Record<string, string[]>, mainFile: string, outExtension?: string): string;
@@ -1,3 +1,3 @@
1
- import { EsBuildExecutorOptions, NormalizedEsBuildExecutorOptions } from '../schema';
2
- import { ExecutorContext } from '@nx/devkit';
1
+ import { type ExecutorContext } from '@nx/devkit';
2
+ import type { EsBuildExecutorOptions, NormalizedEsBuildExecutorOptions } from '../schema';
3
3
  export declare function normalizeOptions(options: EsBuildExecutorOptions, context: ExecutorContext): NormalizedEsBuildExecutorOptions;
@@ -1,25 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.normalizeOptions = normalizeOptions;
4
+ const devkit_1 = require("@nx/devkit");
5
+ const js_1 = require("@nx/js");
6
+ const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup");
4
7
  const fs = require("fs");
5
8
  const path = require("path");
6
- const devkit_1 = require("@nx/devkit");
7
9
  const pc = require("picocolors");
8
- const js_1 = require("@nx/js");
9
10
  function normalizeOptions(options, context) {
11
+ const isTsSolutionSetup = (0, ts_solution_setup_1.isUsingTsSolutionSetup)();
12
+ if (isTsSolutionSetup && options.generatePackageJson) {
13
+ throw new Error(`Setting 'generatePackageJson: true' is not supported with the current TypeScript setup. Update the 'package.json' file at the project root as needed and unset the 'generatePackageJson' option.`);
14
+ }
10
15
  const tsConfig = (0, js_1.readTsConfig)(options.tsConfig);
11
- // If we're not generating package.json file, then copy it as-is as an asset.
12
- const assets = options.generatePackageJson
13
- ? options.assets
16
+ // If we're not generating package.json file, then copy it as-is as an asset when not using ts solution setup.
17
+ const assets = options.generatePackageJson || isTsSolutionSetup
18
+ ? options.assets ?? []
14
19
  : [
15
20
  ...options.assets,
16
21
  (0, devkit_1.joinPathFragments)(context.projectGraph.nodes[context.projectName].data.root, 'package.json'),
17
22
  ];
18
23
  if (!options.bundle && options.thirdParty) {
19
- devkit_1.logger.info(pc.yellow(`Your build has conflicting options, ${pc.bold('bundle:false')} and ${pc.bold('thirdParty:true')}. Your package.json depedencies might not be generated correctly so we added an update ${pc.bold('thirdParty:false')}`));
24
+ devkit_1.logger.info(pc.yellow(`Your build has conflicting options, ${pc.bold('bundle:false')} and ${pc.bold('thirdParty:true')}. Your package.json dependencies might not be generated correctly so we added an update ${pc.bold('thirdParty:false')}`));
20
25
  }
21
26
  const thirdParty = !options.bundle ? false : options.thirdParty;
22
- const { root: projectRoot } = context.projectsConfigurations.projects[context.projectName];
23
27
  const declarationRootDir = options.declarationRootDir
24
28
  ? path.join(context.root, options.declarationRootDir)
25
29
  : undefined;
@@ -5,7 +5,7 @@ type Compiler = 'babel' | 'swc';
5
5
 
6
6
  export interface EsBuildExecutorOptions {
7
7
  additionalEntryPoints?: string[];
8
- assets: (AssetGlob | string)[];
8
+ assets?: (AssetGlob | string)[];
9
9
  bundle?: boolean;
10
10
  declaration?: boolean;
11
11
  declarationRootDir?: string;
@@ -32,7 +32,8 @@ export interface EsBuildExecutorOptions {
32
32
 
33
33
  export interface NormalizedEsBuildExecutorOptions
34
34
  extends Omit<EsBuildExecutorOptions, 'esbuildOptions' | 'esbuildConfig'> {
35
+ assets: (AssetGlob | string)[];
35
36
  singleEntry: boolean;
36
37
  external: string[];
37
- userDefinedBuildOptions: esbuild.BuildOptions;
38
+ userDefinedBuildOptions: esbuild.BuildOptions | undefined;
38
39
  }
@@ -2,19 +2,24 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.configurationGenerator = configurationGenerator;
4
4
  const devkit_1 = require("@nx/devkit");
5
+ const target_defaults_utils_1 = require("@nx/devkit/src/generators/target-defaults-utils");
6
+ const js_1 = require("@nx/js");
5
7
  const get_import_path_1 = require("@nx/js/src/utils/get-import-path");
6
8
  const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup");
9
+ const posix_1 = require("node:path/posix");
10
+ const devkit_internals_1 = require("nx/src/devkit-internals");
11
+ const build_esbuild_options_1 = require("../../executors/esbuild/lib/build-esbuild-options");
7
12
  const init_1 = require("../init/init");
8
- const target_defaults_utils_1 = require("@nx/devkit/src/generators/target-defaults-utils");
9
13
  async function configurationGenerator(tree, options) {
10
- (0, ts_solution_setup_1.assertNotUsingTsSolutionSetup)(tree, 'esbuild', 'configuration');
11
14
  const task = await (0, init_1.esbuildInitGenerator)(tree, {
12
15
  ...options,
13
16
  skipFormat: true,
14
17
  });
15
18
  options.buildTarget ??= 'build';
19
+ const isTsSolutionSetup = (0, ts_solution_setup_1.isUsingTsSolutionSetup)(tree);
16
20
  checkForTargetConflicts(tree, options);
17
- addBuildTarget(tree, options);
21
+ addBuildTarget(tree, options, isTsSolutionSetup);
22
+ updatePackageJson(tree, options, isTsSolutionSetup);
18
23
  await (0, devkit_1.formatFiles)(tree);
19
24
  return task;
20
25
  }
@@ -26,40 +31,44 @@ function checkForTargetConflicts(tree, options) {
26
31
  throw new Error(`Project "${options.project}" already has a ${options.buildTarget} target. Pass --skipValidation to ignore this error.`);
27
32
  }
28
33
  }
29
- function addBuildTarget(tree, options) {
34
+ function addBuildTarget(tree, options, isTsSolutionSetup) {
30
35
  (0, target_defaults_utils_1.addBuildTargetDefaults)(tree, '@nx/esbuild:esbuild', options.buildTarget);
31
36
  const project = (0, devkit_1.readProjectConfiguration)(tree, options.project);
32
- const packageJsonPath = (0, devkit_1.joinPathFragments)(project.root, 'package.json');
33
- if (!tree.exists(packageJsonPath)) {
34
- const importPath = options.importPath || (0, get_import_path_1.getImportPath)(tree, options.project);
35
- (0, devkit_1.writeJson)(tree, packageJsonPath, {
36
- name: importPath,
37
- version: '0.0.1',
38
- });
39
- }
40
37
  const prevBuildOptions = project.targets?.[options.buildTarget]?.options;
41
38
  const tsConfig = prevBuildOptions?.tsConfig ?? getTsConfigFile(tree, options);
39
+ let outputPath = prevBuildOptions?.outputPath;
40
+ if (!outputPath) {
41
+ outputPath = isTsSolutionSetup
42
+ ? (0, devkit_1.joinPathFragments)(project.root, 'dist')
43
+ : (0, devkit_1.joinPathFragments)('dist', project.root === '.' ? options.project : project.root);
44
+ }
42
45
  const buildOptions = {
43
46
  main: prevBuildOptions?.main ?? getMainFile(tree, options),
44
- outputPath: prevBuildOptions?.outputPath ??
45
- (0, devkit_1.joinPathFragments)('dist', project.root === '.' ? options.project : project.root),
47
+ outputPath,
46
48
  outputFileName: 'main.js',
47
49
  tsConfig,
48
- assets: [],
49
50
  platform: options.platform,
51
+ format: options.format,
50
52
  };
51
- if (options.platform === 'browser') {
52
- buildOptions.outputHashing = 'all';
53
- buildOptions.minify = true;
53
+ if (isTsSolutionSetup) {
54
+ buildOptions.declarationRootDir =
55
+ project.sourceRoot ?? tree.exists(`${project.root}/src`)
56
+ ? `${project.root}/src`
57
+ : project.root;
54
58
  }
55
- if (tree.exists((0, devkit_1.joinPathFragments)(project.root, 'README.md'))) {
56
- buildOptions.assets = [
57
- {
59
+ else {
60
+ buildOptions.assets = [];
61
+ if (tree.exists((0, devkit_1.joinPathFragments)(project.root, 'README.md'))) {
62
+ buildOptions.assets.push({
58
63
  glob: `${project.root}/README.md`,
59
64
  input: '.',
60
65
  output: '.',
61
- },
62
- ];
66
+ });
67
+ }
68
+ }
69
+ if (options.platform === 'browser') {
70
+ buildOptions.outputHashing = 'all';
71
+ buildOptions.minify = true;
63
72
  }
64
73
  (0, devkit_1.updateProjectConfiguration)(tree, options.project, {
65
74
  ...project,
@@ -82,6 +91,69 @@ function addBuildTarget(tree, options) {
82
91
  },
83
92
  });
84
93
  }
94
+ function updatePackageJson(tree, options, isTsSolutionSetup) {
95
+ const project = (0, devkit_1.readProjectConfiguration)(tree, options.project);
96
+ const packageJsonPath = (0, posix_1.join)(project.root, 'package.json');
97
+ let packageJson;
98
+ if (tree.exists(packageJsonPath)) {
99
+ if (!isTsSolutionSetup) {
100
+ return;
101
+ }
102
+ packageJson = (0, devkit_1.readJson)(tree, packageJsonPath);
103
+ }
104
+ else {
105
+ packageJson = {
106
+ name: (0, get_import_path_1.getImportPath)(tree, options.project),
107
+ version: '0.0.1',
108
+ };
109
+ }
110
+ if (isTsSolutionSetup) {
111
+ const nxJson = (0, devkit_1.readNxJson)(tree);
112
+ const projectTarget = project.targets[options.buildTarget];
113
+ const mergedTarget = (0, devkit_internals_1.mergeTargetConfigurations)(projectTarget, (projectTarget.executor
114
+ ? nxJson.targetDefaults?.[projectTarget.executor]
115
+ : undefined) ?? nxJson.targetDefaults?.[options.buildTarget]);
116
+ const { declarationRootDir = '.', main, outputPath, outputFileName,
117
+ // the executor option defaults to [esm]
118
+ format = ['esm'], esbuildOptions, } = mergedTarget.options;
119
+ // can't use the declarationRootDir as rootDir because it only affects the typings,
120
+ // not the runtime entry point
121
+ packageJson = (0, js_1.getUpdatedPackageJsonContent)(packageJson, {
122
+ main,
123
+ outputPath,
124
+ projectRoot: project.root,
125
+ generateExportsField: true,
126
+ packageJsonPath,
127
+ format,
128
+ outputFileName,
129
+ outputFileExtensionForCjs: (0, build_esbuild_options_1.getOutExtension)('cjs', {
130
+ // there's very little chance that the user would have defined a custom esbuild config
131
+ // since that's an Nx specific file that we're not generating here and we're setting up
132
+ // the build for esbuild now
133
+ userDefinedBuildOptions: esbuildOptions,
134
+ }),
135
+ outputFileExtensionForEsm: (0, build_esbuild_options_1.getOutExtension)('esm', {
136
+ userDefinedBuildOptions: esbuildOptions,
137
+ }),
138
+ });
139
+ if (declarationRootDir !== (0, posix_1.dirname)(main)) {
140
+ // the declaration file entry point will be output to a location
141
+ // different than the runtime entry point, adjust accodingly
142
+ const outputDir = (0, js_1.getOutputDir)({
143
+ main,
144
+ outputPath,
145
+ projectRoot: project.root,
146
+ packageJsonPath,
147
+ rootDir: declarationRootDir,
148
+ });
149
+ const mainFile = (0, posix_1.basename)(options.main).replace(/\.[tj]s$/, '');
150
+ const typingsFile = `${outputDir}${mainFile}.d.ts`;
151
+ packageJson.types = typingsFile;
152
+ packageJson.exports['.'].types = typingsFile;
153
+ }
154
+ }
155
+ (0, devkit_1.writeJson)(tree, packageJsonPath, packageJson);
156
+ }
85
157
  function getMainFile(tree, options) {
86
158
  const project = (0, devkit_1.readProjectConfiguration)(tree, options.project);
87
159
  const candidates = [
@@ -1,3 +1,5 @@
1
+ import type { SupportedFormat } from '@nx/js';
2
+
1
3
  export interface EsBuildProjectSchema {
2
4
  project: string;
3
5
  main?: string;
@@ -10,4 +12,5 @@ export interface EsBuildProjectSchema {
10
12
  esbuildConfig?: string;
11
13
  platform?: 'node' | 'browser' | 'neutral';
12
14
  buildTarget?: string;
15
+ format?: SupportedFormat[];
13
16
  }
@@ -60,6 +60,15 @@
60
60
  "description": "The build target to add.",
61
61
  "type": "string",
62
62
  "default": "build"
63
+ },
64
+ "format": {
65
+ "description": "The format to build the library (esm or cjs).",
66
+ "type": "array",
67
+ "items": {
68
+ "type": "string",
69
+ "enum": ["esm", "cjs"]
70
+ },
71
+ "default": ["esm"]
63
72
  }
64
73
  },
65
74
  "required": [],
@@ -2,11 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.esbuildInitGenerator = esbuildInitGenerator;
4
4
  const devkit_1 = require("@nx/devkit");
5
- const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup");
6
5
  const versions_1 = require("@nx/js/src/utils/versions");
7
6
  const versions_2 = require("../../utils/versions");
8
7
  async function esbuildInitGenerator(tree, schema) {
9
- (0, ts_solution_setup_1.assertNotUsingTsSolutionSetup)(tree, 'esbuild', 'init');
10
8
  let installTask = () => { };
11
9
  if (!schema.skipPackageJson) {
12
10
  installTask = (0, devkit_1.addDependenciesToPackageJson)(tree, {}, {