feat(vite): add incremental builds support to Vite plugin build executor (#17433)
Co-authored-by: Katerina Skroumpelou <sk.katherine@gmail.com>
This commit is contained in:
parent
f55241bc7d
commit
a9f8cc61fa
@ -16,6 +16,11 @@
|
|||||||
"x-completion-type": "directory",
|
"x-completion-type": "directory",
|
||||||
"x-priority": "important"
|
"x-priority": "important"
|
||||||
},
|
},
|
||||||
|
"buildLibsFromSource": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Read buildable libraries from source instead of building them separately.",
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
"base": {
|
"base": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Base public path when served in development or production.",
|
"description": "Base public path when served in development or production.",
|
||||||
|
|||||||
@ -18,6 +18,11 @@
|
|||||||
"description": "Target which builds the application. Only used to retrieve the configuration as the dev-server does not build the code.",
|
"description": "Target which builds the application. Only used to retrieve the configuration as the dev-server does not build the code.",
|
||||||
"x-priority": "important"
|
"x-priority": "important"
|
||||||
},
|
},
|
||||||
|
"buildLibsFromSource": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Read buildable libraries from source instead of building them separately.",
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
"proxyConfig": {
|
"proxyConfig": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Path to the proxy configuration file.",
|
"description": "Path to the proxy configuration file.",
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import { build, InlineConfig, mergeConfig } from 'vite';
|
|||||||
import {
|
import {
|
||||||
getViteBuildOptions,
|
getViteBuildOptions,
|
||||||
getViteSharedConfig,
|
getViteSharedConfig,
|
||||||
|
registerPaths,
|
||||||
} from '../../utils/options-utils';
|
} from '../../utils/options-utils';
|
||||||
import { ViteBuildExecutorOptions } from './schema';
|
import { ViteBuildExecutorOptions } from './schema';
|
||||||
import {
|
import {
|
||||||
@ -16,8 +17,6 @@ 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';
|
||||||
|
|
||||||
import { registerTsConfigPaths } from '@nx/js/src/internal';
|
|
||||||
|
|
||||||
export async function* viteBuildExecutor(
|
export async function* viteBuildExecutor(
|
||||||
options: ViteBuildExecutorOptions,
|
options: ViteBuildExecutorOptions,
|
||||||
context: ExecutorContext
|
context: ExecutorContext
|
||||||
@ -25,7 +24,7 @@ export async function* viteBuildExecutor(
|
|||||||
const projectRoot =
|
const projectRoot =
|
||||||
context.projectsConfigurations.projects[context.projectName].root;
|
context.projectsConfigurations.projects[context.projectName].root;
|
||||||
|
|
||||||
registerTsConfigPaths(resolve(projectRoot, 'tsconfig.json'));
|
registerPaths(projectRoot, options, context);
|
||||||
|
|
||||||
const normalizedOptions = normalizeOptions(options);
|
const normalizedOptions = normalizeOptions(options);
|
||||||
|
|
||||||
|
|||||||
@ -18,4 +18,5 @@ export interface ViteBuildExecutorOptions {
|
|||||||
generatePackageJson?: boolean;
|
generatePackageJson?: boolean;
|
||||||
includeDevDependenciesInPackageJson?: boolean;
|
includeDevDependenciesInPackageJson?: boolean;
|
||||||
cssCodeSplit?: boolean;
|
cssCodeSplit?: boolean;
|
||||||
|
buildLibsFromSource?: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,11 @@
|
|||||||
"x-completion-type": "directory",
|
"x-completion-type": "directory",
|
||||||
"x-priority": "important"
|
"x-priority": "important"
|
||||||
},
|
},
|
||||||
|
"buildLibsFromSource": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Read buildable libraries from source instead of building them separately.",
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
"base": {
|
"base": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Base public path when served in development or production.",
|
"description": "Base public path when served in development or production.",
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import {
|
|||||||
getNxTargetOptions,
|
getNxTargetOptions,
|
||||||
getViteServerOptions,
|
getViteServerOptions,
|
||||||
getViteBuildOptions,
|
getViteBuildOptions,
|
||||||
|
registerPaths,
|
||||||
} from '../../utils/options-utils';
|
} from '../../utils/options-utils';
|
||||||
|
|
||||||
import { ViteDevServerExecutorOptions } from './schema';
|
import { ViteDevServerExecutorOptions } from './schema';
|
||||||
@ -14,6 +15,11 @@ import { ViteBuildExecutorOptions } from '../build/schema';
|
|||||||
import { registerTsConfigPaths } from '@nx/js/src/internal';
|
import { registerTsConfigPaths } from '@nx/js/src/internal';
|
||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
|
|
||||||
|
import {
|
||||||
|
calculateProjectDependencies,
|
||||||
|
createTmpTsConfig,
|
||||||
|
} from '@nx/js/src/utils/buildable-libs-utils';
|
||||||
|
|
||||||
export async function* viteDevServerExecutor(
|
export async function* viteDevServerExecutor(
|
||||||
options: ViteDevServerExecutorOptions,
|
options: ViteDevServerExecutorOptions,
|
||||||
context: ExecutorContext
|
context: ExecutorContext
|
||||||
@ -21,7 +27,7 @@ export async function* viteDevServerExecutor(
|
|||||||
const projectRoot =
|
const projectRoot =
|
||||||
context.projectsConfigurations.projects[context.projectName].root;
|
context.projectsConfigurations.projects[context.projectName].root;
|
||||||
|
|
||||||
registerTsConfigPaths(resolve(projectRoot, 'tsconfig.json'));
|
registerPaths(projectRoot, options, context);
|
||||||
|
|
||||||
// Retrieve the option for the configured buildTarget.
|
// Retrieve the option for the configured buildTarget.
|
||||||
const buildTargetOptions: ViteBuildExecutorOptions = getNxTargetOptions(
|
const buildTargetOptions: ViteBuildExecutorOptions = getNxTargetOptions(
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
export interface ViteDevServerExecutorOptions {
|
export interface ViteDevServerExecutorOptions {
|
||||||
buildTarget: string;
|
buildTarget: string;
|
||||||
|
buildLibsFromSource?: boolean;
|
||||||
proxyConfig?: string;
|
proxyConfig?: string;
|
||||||
port?: number;
|
port?: number;
|
||||||
host?: string | boolean;
|
host?: string | boolean;
|
||||||
|
|||||||
@ -21,6 +21,11 @@
|
|||||||
"description": "Target which builds the application. Only used to retrieve the configuration as the dev-server does not build the code.",
|
"description": "Target which builds the application. Only used to retrieve the configuration as the dev-server does not build the code.",
|
||||||
"x-priority": "important"
|
"x-priority": "important"
|
||||||
},
|
},
|
||||||
|
"buildLibsFromSource": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Read buildable libraries from source instead of building them separately.",
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
"proxyConfig": {
|
"proxyConfig": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Path to the proxy configuration file.",
|
"description": "Path to the proxy configuration file.",
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import {
|
|||||||
readTargetOptions,
|
readTargetOptions,
|
||||||
} from '@nx/devkit';
|
} from '@nx/devkit';
|
||||||
import { existsSync } from 'fs';
|
import { existsSync } from 'fs';
|
||||||
import { relative } from 'path';
|
import { relative, resolve } from 'path';
|
||||||
import {
|
import {
|
||||||
BuildOptions,
|
BuildOptions,
|
||||||
InlineConfig,
|
InlineConfig,
|
||||||
@ -19,6 +19,11 @@ import { ViteDevServerExecutorOptions } from '../executors/dev-server/schema';
|
|||||||
import { VitePreviewServerExecutorOptions } from '../executors/preview-server/schema';
|
import { VitePreviewServerExecutorOptions } from '../executors/preview-server/schema';
|
||||||
import replaceFiles from '../../plugins/rollup-replace-files.plugin';
|
import replaceFiles from '../../plugins/rollup-replace-files.plugin';
|
||||||
import { ViteBuildExecutorOptions } from '../executors/build/schema';
|
import { ViteBuildExecutorOptions } from '../executors/build/schema';
|
||||||
|
import { registerTsConfigPaths } from '@nx/js/src/internal';
|
||||||
|
import {
|
||||||
|
calculateProjectDependencies,
|
||||||
|
createTmpTsConfig,
|
||||||
|
} from '@nx/js/src/utils/buildable-libs-utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the path to the vite config file or undefined when not found.
|
* Returns the path to the vite config file or undefined when not found.
|
||||||
@ -188,3 +193,32 @@ export function getNxTargetOptions(target: string, context: ExecutorContext) {
|
|||||||
const targetObj = parseTargetString(target, context.projectGraph);
|
const targetObj = parseTargetString(target, context.projectGraph);
|
||||||
return readTargetOptions(targetObj, context);
|
return readTargetOptions(targetObj, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function registerPaths(
|
||||||
|
projectRoot: string,
|
||||||
|
options: ViteBuildExecutorOptions | ViteDevServerExecutorOptions,
|
||||||
|
context: ExecutorContext
|
||||||
|
) {
|
||||||
|
const tsConfig = resolve(projectRoot, 'tsconfig.json');
|
||||||
|
options.buildLibsFromSource ??= true;
|
||||||
|
|
||||||
|
if (!options.buildLibsFromSource) {
|
||||||
|
const { dependencies } = calculateProjectDependencies(
|
||||||
|
context.projectGraph,
|
||||||
|
context.root,
|
||||||
|
context.projectName,
|
||||||
|
context.targetName,
|
||||||
|
context.configurationName
|
||||||
|
);
|
||||||
|
const tmpTsConfig = createTmpTsConfig(
|
||||||
|
tsConfig,
|
||||||
|
context.root,
|
||||||
|
projectRoot,
|
||||||
|
dependencies
|
||||||
|
);
|
||||||
|
|
||||||
|
registerTsConfigPaths(tmpTsConfig);
|
||||||
|
} else {
|
||||||
|
registerTsConfigPaths(tsConfig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user