diff --git a/docs/generated/packages/vite/executors/build.json b/docs/generated/packages/vite/executors/build.json index 9f593e1fc8ae3..b0ec6d1d98def 100644 --- a/docs/generated/packages/vite/executors/build.json +++ b/docs/generated/packages/vite/executors/build.json @@ -88,6 +88,47 @@ "description": "Enable re-building when files change.", "oneOf": [{ "type": "boolean" }, { "type": "object" }], "default": false + }, + "main": { + "type": "string", + "description": "The path to the entry file, relative to project. Only needed if using generatePackageJson", + "alias": "entryFile", + "x-completion-type": "file", + "x-completion-glob": "**/*@(.js|.ts)" + }, + "tsConfig": { + "type": "string", + "description": "The path to tsconfig file. Only needed if using generatePacakgeJson", + "x-completion-type": "file", + "x-completion-glob": "tsconfig.*.json" + }, + "generatePackageJson": { + "type": "boolean", + "description": "Generates a `package.json` and pruned lock file with the project's `node_module` dependencies populated for installing in a container. If a `package.json` exists in the project's directory, it will be reused with dependencies populated.", + "default": false, + "buildableProjectDepsInPackageJsonType": { + "type": "string", + "description": "The type of dependency to use for buildable project dependencies in the generated `package.json`.", + "enum": ["dependencies", "devDependencies"], + "default": "dependencies" + }, + "excludeLibsInPackageJson": { + "type": "boolean", + "description": "Exclude libraries in the `package.json` file. This is useful if you are using a `package.json` file in the project's directory.", + "default": true + }, + "generateLockfile": { + "type": "boolean", + "description": "Generate a lock file for the generated `package.json`.", + "default": true + }, + "format": { + "type": "string", + "description": "List of module formats to output. Defaults to matching format from tsconfig (e.g. CJS for CommonJS, and ESM otherwise).", + "alias": "f", + "enum": ["esm", "cjs"], + "default": "esm" + } } }, "definitions": {}, diff --git a/packages/esbuild/src/executors/esbuild/esbuild.impl.ts b/packages/esbuild/src/executors/esbuild/esbuild.impl.ts index 26b07e0c68826..71058de03b1af 100644 --- a/packages/esbuild/src/executors/esbuild/esbuild.impl.ts +++ b/packages/esbuild/src/executors/esbuild/esbuild.impl.ts @@ -6,6 +6,7 @@ import { copyAssets, copyPackageJson, CopyPackageJsonOptions, + getExtraDependencies, printDiagnostics, runTypeCheck as _runTypeCheck, TypeCheckOptions, @@ -21,7 +22,6 @@ import { getOutExtension, getOutfile, } from './lib/build-esbuild-options'; -import { getExtraDependencies } from './lib/get-extra-dependencies'; import { DependentBuildableProjectNode } from '@nrwl/js/src/utils/buildable-libs-utils'; import { join } from 'path'; diff --git a/packages/js/src/index.ts b/packages/js/src/index.ts index 2597abbff3e5c..69211f2443df1 100644 --- a/packages/js/src/index.ts +++ b/packages/js/src/index.ts @@ -6,6 +6,7 @@ export * from './utils/compiler-helper-dependency'; export * from './utils/typescript/ts-config'; export * from './utils/typescript/create-ts-config'; export * from './utils/typescript/ast-utils'; +export * from './utils/get-extra-dependencies'; export * from './utils/package-json'; export * from './utils/assets'; export * from './utils/package-json/update-package-json'; diff --git a/packages/esbuild/src/executors/esbuild/lib/get-extra-dependencies.spec.ts b/packages/js/src/utils/get-extra-dependencies.spec.ts similarity index 100% rename from packages/esbuild/src/executors/esbuild/lib/get-extra-dependencies.spec.ts rename to packages/js/src/utils/get-extra-dependencies.spec.ts diff --git a/packages/esbuild/src/executors/esbuild/lib/get-extra-dependencies.ts b/packages/js/src/utils/get-extra-dependencies.ts similarity index 91% rename from packages/esbuild/src/executors/esbuild/lib/get-extra-dependencies.ts rename to packages/js/src/utils/get-extra-dependencies.ts index 5f00a6ace1e66..35be7922f991f 100644 --- a/packages/esbuild/src/executors/esbuild/lib/get-extra-dependencies.ts +++ b/packages/js/src/utils/get-extra-dependencies.ts @@ -1,5 +1,5 @@ import { ProjectGraph } from '@nrwl/devkit'; -import { DependentBuildableProjectNode } from '@nrwl/js/src/utils/buildable-libs-utils'; +import { DependentBuildableProjectNode } from './buildable-libs-utils'; export function getExtraDependencies( projectName: string, diff --git a/packages/vite/src/executors/build/build.impl.ts b/packages/vite/src/executors/build/build.impl.ts index 65439586f746b..9e1c040d14c23 100644 --- a/packages/vite/src/executors/build/build.impl.ts +++ b/packages/vite/src/executors/build/build.impl.ts @@ -6,7 +6,12 @@ import { getViteSharedConfig, } from '../../utils/options-utils'; import { ViteBuildExecutorOptions } from './schema'; -import { copyAssets } from '@nrwl/js'; +import { + copyAssets, + copyPackageJson, + CopyPackageJsonOptions, + getExtraDependencies, +} from '@nrwl/js'; import { existsSync } from 'fs'; import { resolve } from 'path'; import { createAsyncIterable } from '@nrwl/devkit/src/utils/async-iterable'; @@ -36,8 +41,47 @@ export async function* viteBuildExecutor( const libraryPackageJson = resolve(projectRoot, 'package.json'); const rootPackageJson = resolve(context.root, 'package.json'); + if (options.generatePackageJson) { + const { + buildableProjectDepsInPackageJsonType = 'dependencies', + excludeLibsInPackageJson = true, + format = 'esm', + generateLockfile = true, + } = options; + + if (!options.main) { + throw new Error( + 'Missing "main" option required for generating package.json' + ); + } + if (!options.tsConfig) { + throw new Error( + 'Missing "tsConfig" option required for generating package.json' + ); + } + + const externalDependencies = getExtraDependencies( + context.projectName, + context.projectGraph + ); + + const cpjOptions: CopyPackageJsonOptions = { + outputPath: options.outputPath, + buildableProjectDepsInPackageJsonType, + excludeLibsInPackageJson, + generateLockfile, + format: [format], + main: options.main, + watch: false, + skipTypings: true, + updateBuildableProjectDepsInPackageJson: externalDependencies.length > 0, + }; + + await copyPackageJson(cpjOptions, context); + } // For buildable libs, copy package.json if it exists. - if ( + // this is here for backwards compatibility, you'll likely want to generate a package JSON if creating a buildable lib + else if ( existsSync(libraryPackageJson) && rootPackageJson !== libraryPackageJson ) { diff --git a/packages/vite/src/executors/build/schema.d.ts b/packages/vite/src/executors/build/schema.d.ts index 5ce0b951a0440..cd17dbf609589 100644 --- a/packages/vite/src/executors/build/schema.d.ts +++ b/packages/vite/src/executors/build/schema.d.ts @@ -1,17 +1,24 @@ import type { FileReplacement } from '../../plugins/rollup-replace-files.plugin'; export interface ViteBuildExecutorOptions { outputPath: string; - emptyOutDir?: boolean; base?: string; + buildableProjectDepsInPackageJsonType?: 'dependencies' | 'peerDependencies'; configFile?: string; + emptyOutDir?: boolean; + excludeLibsInPackageJson?: boolean; fileReplacements?: FileReplacement[]; force?: boolean; - sourcemap?: boolean | 'inline' | 'hidden'; - minify?: boolean | 'esbuild' | 'terser'; - manifest?: boolean | string; - ssrManifest?: boolean | string; + format?: 'esm' | 'cjs'; + generateLockfile?: boolean; + generatePackageJson?: boolean; logLevel?: 'info' | 'warn' | 'error' | 'silent'; + main?: string; + manifest?: boolean | string; + minify?: boolean | 'esbuild' | 'terser'; mode?: string; + sourcemap?: boolean | 'inline' | 'hidden'; ssr?: boolean | string; + ssrManifest?: boolean | string; + tsConfig?: string; watch?: object | boolean; } diff --git a/packages/vite/src/executors/build/schema.json b/packages/vite/src/executors/build/schema.json index fd61dbcad9733..588d53a023dc9 100644 --- a/packages/vite/src/executors/build/schema.json +++ b/packages/vite/src/executors/build/schema.json @@ -135,6 +135,47 @@ } ], "default": false + }, + "main": { + "type": "string", + "description": "The path to the entry file, relative to project. Only needed if using generatePackageJson", + "alias": "entryFile", + "x-completion-type": "file", + "x-completion-glob": "**/*@(.js|.ts)" + }, + "tsConfig": { + "type": "string", + "description": "The path to tsconfig file. Only needed if using generatePacakgeJson", + "x-completion-type": "file", + "x-completion-glob": "tsconfig.*.json" + }, + "generatePackageJson": { + "type": "boolean", + "description": "Generates a `package.json` and pruned lock file with the project's `node_module` dependencies populated for installing in a container. If a `package.json` exists in the project's directory, it will be reused with dependencies populated.", + "default": false, + "buildableProjectDepsInPackageJsonType": { + "type": "string", + "description": "The type of dependency to use for buildable project dependencies in the generated `package.json`.", + "enum": ["dependencies", "devDependencies"], + "default": "dependencies" + }, + "excludeLibsInPackageJson": { + "type": "boolean", + "description": "Exclude libraries in the `package.json` file. This is useful if you are using a `package.json` file in the project's directory.", + "default": true + }, + "generateLockfile": { + "type": "boolean", + "description": "Generate a lock file for the generated `package.json`.", + "default": true + }, + "format": { + "type": "string", + "description": "List of module formats to output. Defaults to matching format from tsconfig (e.g. CJS for CommonJS, and ESM otherwise).", + "alias": "f", + "enum": ["esm", "cjs"], + "default": "esm" + } } }, "definitions": {},