Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vite): add option to generate a package.json to build executor #16428

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/generated/packages/vite/executors/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@
"description": "Enable re-building when files change.",
"oneOf": [{ "type": "boolean" }, { "type": "object" }],
"default": false
},
"generatePackageJson": {
"description": "Generate a package.json for the build output.",
"type": "boolean"
},
"includeDevDependenciesInPackageJson": {
"description": "Include devDependencies in the generated package.json.",
"type": "boolean"
}
},
"definitions": {},
Expand Down
50 changes: 50 additions & 0 deletions e2e/vite/src/vite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
newProject,
promisifiedTreeKill,
readFile,
readJson,
rmDist,
runCLI,
runCLIAsync,
Expand Down Expand Up @@ -144,6 +145,55 @@ describe('Vite Plugin', () => {
expect(fileExists(`dist/apps/${myApp}/package.json`)).toBeFalsy();
rmDist();
}, 200_000);

it('should build application with new package json generation', async () => {
runCLI(`build ${myApp} --generatePackageJson`);
expect(readFile(`dist/apps/${myApp}/index.html`)).toBeDefined();
const fileArray = listFiles(`dist/apps/${myApp}/assets`);
const mainBundle = fileArray.find((file) => file.endsWith('.js'));
expect(
readFile(`dist/apps/${myApp}/assets/${mainBundle}`)
).toBeDefined();

const packageJson = readJson(`dist/apps/${myApp}/package.json`);
expect(packageJson).toEqual({
name: myApp,
version: '0.0.1',
type: 'module',
});
rmDist();
}, 200_000);

it('should build application with existing package json generation', async () => {
createFile(
`apps/${myApp}/package.json`,
JSON.stringify({
name: 'my-existing-app',
version: '1.0.1',
scripts: {
start: 'node server.js',
},
})
);
runCLI(`build ${myApp} --generatePackageJson`);
expect(readFile(`dist/apps/${myApp}/index.html`)).toBeDefined();
const fileArray = listFiles(`dist/apps/${myApp}/assets`);
const mainBundle = fileArray.find((file) => file.endsWith('.js'));
expect(
readFile(`dist/apps/${myApp}/assets/${mainBundle}`)
).toBeDefined();

const packageJson = readJson(`dist/apps/${myApp}/package.json`);
expect(packageJson).toEqual({
name: 'my-existing-app',
version: '1.0.1',
type: 'module',
scripts: {
start: 'node server.js',
},
});
rmDist();
}, 200_000);
});

describe('convert @nx/web webpack app to vite using the vite:configuration generator', () => {
Expand Down
34 changes: 30 additions & 4 deletions packages/vite/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import 'dotenv/config';
import { ExecutorContext } from '@nx/devkit';
import { ExecutorContext, writeJsonFile } from '@nx/devkit';
import { build, InlineConfig, mergeConfig } from 'vite';
import {
getViteBuildOptions,
getViteSharedConfig,
} from '../../utils/options-utils';
import { ViteBuildExecutorOptions } from './schema';
import { copyAssets } from '@nx/js';
import { existsSync } from 'fs';
import {
copyAssets,
createLockFile,
createPackageJson,
getLockFileName,
} from '@nx/js';
import { existsSync, writeFileSync } from 'fs';
import { resolve } from 'path';
import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable';

Expand Down Expand Up @@ -37,8 +42,29 @@ export async function* viteBuildExecutor(
const rootPackageJson = resolve(context.root, 'package.json');
const distPackageJson = resolve(normalizedOptions.outputPath, 'package.json');

// Generate a package.json if option has been set.
if (options.generatePackageJson) {
const builtPackageJson = createPackageJson(
context.projectName,
context.projectGraph,
{
target: context.targetName,
root: context.root,
isProduction: !options.includeDevDependenciesInPackageJson, // By default we remove devDependencies since this is a production build.
}
);

builtPackageJson.type = 'module';

writeJsonFile(`${options.outputPath}/package.json`, builtPackageJson);

const lockFile = createLockFile(builtPackageJson);
writeFileSync(`${options.outputPath}/${getLockFileName()}`, lockFile, {
encoding: 'utf-8',
});
}
// For buildable libs, copy package.json if it exists.
if (
else if (
!existsSync(distPackageJson) &&
existsSync(libraryPackageJson) &&
rootPackageJson !== libraryPackageJson
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/executors/build/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ export interface ViteBuildExecutorOptions {
ssr?: boolean | string;
watch?: object | boolean;
target?: string | string[];
generatePackageJson?: boolean;
includeDevDependenciesInPackageJson?: boolean;
}
8 changes: 8 additions & 0 deletions packages/vite/src/executors/build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@
}
],
"default": false
},
"generatePackageJson": {
"description": "Generate a package.json for the build output.",
"type": "boolean"
},
"includeDevDependenciesInPackageJson": {
"description": "Include devDependencies in the generated package.json.",
"type": "boolean"
}
},
"definitions": {},
Expand Down