Skip to content

Commit

Permalink
feat(bundling): remove assets option from vite build
Browse files Browse the repository at this point in the history
- Does not work well with Vite build and dev-server
- User can put their files into public/ or add a plugin to copy for them
  • Loading branch information
jaysoo authored and Jack Hsu committed Nov 28, 2022
1 parent 3c60da7 commit 59c2c21
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 85 deletions.
34 changes: 0 additions & 34 deletions docs/generated/packages/vite.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,40 +254,6 @@
"x-completion-type": "file",
"x-completion-glob": "vite.config.@(js|ts)"
},
"assets": {
"type": "array",
"description": "List of static application assets.",
"default": [],
"items": {
"oneOf": [
{
"type": "object",
"properties": {
"glob": {
"type": "string",
"description": "The pattern to match."
},
"input": {
"type": "string",
"description": "The input directory path in which to apply 'glob'. Defaults to the project root."
},
"ignore": {
"description": "An array of globs to ignore.",
"type": "array",
"items": { "type": "string" }
},
"output": {
"type": "string",
"description": "Absolute path within the output."
}
},
"additionalProperties": false,
"required": ["glob", "input", "output"]
},
{ "type": "string" }
]
}
},
"fileReplacements": {
"description": "Replace files with other files in the build.",
"type": "array",
Expand Down
31 changes: 13 additions & 18 deletions packages/vite/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,29 @@ export default async function viteBuildExecutor(
context: ExecutorContext
) {
const projectRoot = context.workspace.projects[context.projectName].root;
let assets = options.assets;

// Copy package.json as an asset if it exists
// For buildable libs, copy package.json if it exists.
if (existsSync(join(projectRoot, 'package.json'))) {
assets ??= [];
assets.push({
input: '.',
output: '.',
glob: 'package.json',
});
}

logger.info(`NX Vite build starting ...`);
const buildResult = await runInstance(await getBuildConfig(options, context));
logger.info(`NX Vite build finished ...`);
logger.info(`NX Vite files available in ${options.outputPath}`);

// TODO(jack): handle watch once we add that option
if (assets) {
await copyAssets(
{
outputPath: options.outputPath,
assets: assets,
assets: [
{
input: '.',
output: '.',
glob: 'package.json',
},
],
},
context
);
}

logger.info(`NX Vite build starting ...`);
const buildResult = await runInstance(await getBuildConfig(options, context));
logger.info(`NX Vite build finished ...`);
logger.info(`NX Vite files available in ${options.outputPath}`);

return { success: true };
}

Expand Down
2 changes: 0 additions & 2 deletions packages/vite/src/executors/build/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import type { AssetGlob } from '@nrwl/workspace/src/utilities/assets';
import type { FileReplacement } from '../../plugins/rollup-replace-files.plugin';
export interface ViteBuildExecutorOptions {
outputPath: string;
baseHref?: string;
proxyConfig?: string;
tsConfig?: string;
configFile?: string;
assets?: AssetGlob[];
fileReplacements?: FileReplacement[];
sourcemap?: boolean | 'inline' | 'hidden';
minify?: boolean | 'esbuild' | 'terser';
Expand Down
8 changes: 0 additions & 8 deletions packages/vite/src/executors/build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@
"x-completion-type": "file",
"x-completion-glob": "vite.config.@(js|ts)"
},
"assets": {
"type": "array",
"description": "List of static application assets.",
"default": [],
"items": {
"$ref": "#/definitions/assetPattern"
}
},
"fileReplacements": {
"description": "Replace files with other files in the build.",
"type": "array",
Expand Down
27 changes: 4 additions & 23 deletions packages/vite/src/executors/dev-server/dev-server.impl.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { ExecutorContext } from '@nrwl/devkit';

import 'dotenv/config';
import { InlineConfig, createServer, mergeConfig, ViteDevServer } from 'vite';
import { ExecutorContext } from '@nrwl/devkit';
import { createServer, InlineConfig, mergeConfig, ViteDevServer } from 'vite';

import {
getBuildConfig,
getBuildTargetOptions,
getServerOptions,
} from '../../utils/options-utils';

import { copyAssets, CopyAssetsResult } from '@nrwl/js';

import { ViteDevServerExecutorOptions } from './schema';
import { ViteBuildExecutorOptions } from '../build/schema';

Expand All @@ -23,18 +20,6 @@ export default async function* viteDevServerExecutor(
...options,
} as ViteDevServerExecutorOptions & ViteBuildExecutorOptions;

let assetsResult: CopyAssetsResult;
if (mergedOptions.assets) {
assetsResult = await copyAssets(
{
outputPath: mergedOptions.outputPath,
assets: mergedOptions.assets ?? [],
watch: true,
},
context
);
}

const serverConfig: InlineConfig = mergeConfig(
await getBuildConfig(mergedOptions, context),
{
Expand All @@ -44,7 +29,7 @@ export default async function* viteDevServerExecutor(

const server = await createServer(serverConfig);

const baseUrl = await runViteDevServer(server, assetsResult);
const baseUrl = await runViteDevServer(server);

yield {
success: true,
Expand All @@ -55,16 +40,12 @@ export default async function* viteDevServerExecutor(
await new Promise<{ success: boolean }>(() => {});
}

async function runViteDevServer(
server: ViteDevServer,
assetsResult: CopyAssetsResult
): Promise<string> {
async function runViteDevServer(server: ViteDevServer): Promise<string> {
try {
await server.listen();
server.printUrls();

const processOnExit = () => {
assetsResult?.stop();
process.off('SIGINT', processOnExit);
process.off('SIGTERM', processOnExit);
process.off('exit', processOnExit);
Expand Down

0 comments on commit 59c2c21

Please sign in to comment.