Skip to content

Commit

Permalink
feat(vite): add generatePackageJson option to vite executor (#16042)
Browse files Browse the repository at this point in the history
* moves getExtraDependencies helper from esbuild package to js package
  • Loading branch information
jensbodal committed Apr 2, 2023
1 parent 6ba60a0 commit ae0e5a0
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 9 deletions.
41 changes: 41 additions & 0 deletions docs/generated/packages/vite/executors/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down
2 changes: 1 addition & 1 deletion packages/esbuild/src/executors/esbuild/esbuild.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
copyAssets,
copyPackageJson,
CopyPackageJsonOptions,
getExtraDependencies,
printDiagnostics,
runTypeCheck as _runTypeCheck,
TypeCheckOptions,
Expand All @@ -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';

Expand Down
1 change: 1 addition & 0 deletions packages/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
48 changes: 46 additions & 2 deletions packages/vite/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
) {
Expand Down
17 changes: 12 additions & 5 deletions packages/vite/src/executors/build/schema.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
41 changes: 41 additions & 0 deletions packages/vite/src/executors/build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down

0 comments on commit ae0e5a0

Please sign in to comment.