Skip to content

Commit

Permalink
feat(vite): add option to generate a package.json to build executor (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalton9 authored May 8, 2023
1 parent ceabe46 commit 402a00f
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
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

1 comment on commit 402a00f

@vercel
Copy link

@vercel vercel bot commented on 402a00f May 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.