feat(vite): add option to generate a package.json to build executor (#16428)
This commit is contained in:
parent
ceabe46e89
commit
402a00f841
@ -92,6 +92,14 @@
|
|||||||
"description": "Enable re-building when files change.",
|
"description": "Enable re-building when files change.",
|
||||||
"oneOf": [{ "type": "boolean" }, { "type": "object" }],
|
"oneOf": [{ "type": "boolean" }, { "type": "object" }],
|
||||||
"default": false
|
"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": {},
|
"definitions": {},
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import {
|
|||||||
newProject,
|
newProject,
|
||||||
promisifiedTreeKill,
|
promisifiedTreeKill,
|
||||||
readFile,
|
readFile,
|
||||||
|
readJson,
|
||||||
rmDist,
|
rmDist,
|
||||||
runCLI,
|
runCLI,
|
||||||
runCLIAsync,
|
runCLIAsync,
|
||||||
@ -144,6 +145,55 @@ describe('Vite Plugin', () => {
|
|||||||
expect(fileExists(`dist/apps/${myApp}/package.json`)).toBeFalsy();
|
expect(fileExists(`dist/apps/${myApp}/package.json`)).toBeFalsy();
|
||||||
rmDist();
|
rmDist();
|
||||||
}, 200_000);
|
}, 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', () => {
|
describe('convert @nx/web webpack app to vite using the vite:configuration generator', () => {
|
||||||
|
|||||||
@ -1,13 +1,18 @@
|
|||||||
import 'dotenv/config';
|
import 'dotenv/config';
|
||||||
import { ExecutorContext } from '@nx/devkit';
|
import { ExecutorContext, writeJsonFile } from '@nx/devkit';
|
||||||
import { build, InlineConfig, mergeConfig } from 'vite';
|
import { build, InlineConfig, mergeConfig } from 'vite';
|
||||||
import {
|
import {
|
||||||
getViteBuildOptions,
|
getViteBuildOptions,
|
||||||
getViteSharedConfig,
|
getViteSharedConfig,
|
||||||
} from '../../utils/options-utils';
|
} from '../../utils/options-utils';
|
||||||
import { ViteBuildExecutorOptions } from './schema';
|
import { ViteBuildExecutorOptions } from './schema';
|
||||||
import { copyAssets } from '@nx/js';
|
import {
|
||||||
import { existsSync } from 'fs';
|
copyAssets,
|
||||||
|
createLockFile,
|
||||||
|
createPackageJson,
|
||||||
|
getLockFileName,
|
||||||
|
} from '@nx/js';
|
||||||
|
import { existsSync, writeFileSync } from 'fs';
|
||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable';
|
import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable';
|
||||||
|
|
||||||
@ -37,8 +42,29 @@ export async function* viteBuildExecutor(
|
|||||||
const rootPackageJson = resolve(context.root, 'package.json');
|
const rootPackageJson = resolve(context.root, 'package.json');
|
||||||
const distPackageJson = resolve(normalizedOptions.outputPath, '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.
|
// For buildable libs, copy package.json if it exists.
|
||||||
if (
|
else if (
|
||||||
!existsSync(distPackageJson) &&
|
!existsSync(distPackageJson) &&
|
||||||
existsSync(libraryPackageJson) &&
|
existsSync(libraryPackageJson) &&
|
||||||
rootPackageJson !== libraryPackageJson
|
rootPackageJson !== libraryPackageJson
|
||||||
|
|||||||
@ -15,4 +15,6 @@ export interface ViteBuildExecutorOptions {
|
|||||||
ssr?: boolean | string;
|
ssr?: boolean | string;
|
||||||
watch?: object | boolean;
|
watch?: object | boolean;
|
||||||
target?: string | string[];
|
target?: string | string[];
|
||||||
|
generatePackageJson?: boolean;
|
||||||
|
includeDevDependenciesInPackageJson?: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -139,6 +139,14 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"default": false
|
"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": {},
|
"definitions": {},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user