Skip to content

Commit

Permalink
feat(vite): be able to use a custom build target for the preview serv…
Browse files Browse the repository at this point in the history
…er (#14951)
  • Loading branch information
dmitry-stepanenko committed Feb 13, 2023
1 parent 91117c9 commit 2f9978f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/generated/packages/vite/executors/preview-server.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
"clearScreen": {
"description": "Set to false to prevent Vite from clearing the terminal screen when logging certain messages.",
"type": "boolean"
},
"staticFilePath": {
"type": "string",
"description": "Path where the build artifacts are located. If not provided then it will be infered from the buildTarget executor options as outputPath",
"x-completion-type": "directory"
}
},
"definitions": {},
Expand Down
36 changes: 32 additions & 4 deletions packages/vite/src/executors/preview-server/preview-server.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,49 @@ import {
import { ViteBuildExecutorOptions } from '../build/schema';
import { VitePreviewServerExecutorOptions } from './schema';

interface CustomBuildTargetOptions {
outputPath: string;
}

export async function* vitePreviewServerExecutor(
options: VitePreviewServerExecutorOptions,
context: ExecutorContext
) {
const target = parseTargetString(options.buildTarget, context.projectGraph);
const targetConfiguration =
context.projectsConfigurations.projects[target.project]?.targets[
target.target
];
if (!targetConfiguration) {
throw new Error(`Invalid buildTarget: ${options.buildTarget}`);
}

const isCustomBuildTarget =
targetConfiguration.executor !== '@nrwl/vite:build';

// Retrieve the option for the configured buildTarget.
const buildTargetOptions: ViteBuildExecutorOptions = getNxTargetOptions(
const buildTargetOptions:
| ViteBuildExecutorOptions
| CustomBuildTargetOptions = getNxTargetOptions(
options.buildTarget,
context
);

const outputPath = options.staticFilePath ?? buildTargetOptions.outputPath;

if (!outputPath) {
throw new Error(
`Could not infer the "outputPath". It should either be a property of the "${options.buildTarget}" buildTarget or provided explicitly as a "staticFilePath" option.`
);
}

// Merge the options from the build and preview-serve targets.
// The latter takes precedence.
const mergedOptions = {
...{ watch: {} },
...buildTargetOptions,
...(isCustomBuildTarget ? {} : buildTargetOptions),
...options,
outputPath,
};

// Retrieve the server configuration.
Expand All @@ -51,8 +78,9 @@ export async function* vitePreviewServerExecutor(
process.once('exit', processOnExit);

// Launch the build target.
const target = parseTargetString(options.buildTarget, context.projectGraph);
const build = await runExecutor(target, mergedOptions, context);
// If customBuildTarget is set to true, do not provide any overrides to it
const buildTargetOverrides = isCustomBuildTarget ? {} : mergedOptions;
const build = await runExecutor(target, buildTargetOverrides, context);

for await (const result of build) {
if (result.success) {
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/executors/preview-server/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface VitePreviewServerExecutorOptions {
logLevel?: 'info' | 'warn' | 'error' | 'silent';
mode?: string;
clearScreen?: boolean;
staticFilePath?: string;
}
5 changes: 5 additions & 0 deletions packages/vite/src/executors/preview-server/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
"clearScreen": {
"description": "Set to false to prevent Vite from clearing the terminal screen when logging certain messages.",
"type": "boolean"
},
"staticFilePath": {
"type": "string",
"description": "Path where the build artifacts are located. If not provided then it will be infered from the buildTarget executor options as outputPath",
"x-completion-type": "directory"
}
},
"definitions": {},
Expand Down

1 comment on commit 2f9978f

@vercel
Copy link

@vercel vercel bot commented on 2f9978f Feb 13, 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-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.