fix(vite): replaceFile and fileReplacement fixes (#21077)

This commit is contained in:
Katerina Skroumpelou 2024-01-10 18:53:34 +02:00 committed by GitHub
parent 5e9a362c11
commit 7da53c026e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 53 deletions

View File

@ -32,28 +32,6 @@
"x-completion-type": "file", "x-completion-type": "file",
"x-completion-glob": "vite.config.@(js|ts)" "x-completion-glob": "vite.config.@(js|ts)"
}, },
"fileReplacements": {
"description": "Replace files with other files in the build.",
"type": "array",
"items": {
"type": "object",
"properties": {
"replace": {
"type": "string",
"description": "The file to be replaced.",
"x-completion-type": "file"
},
"with": {
"type": "string",
"description": "The file to replace with.",
"x-completion-type": "file"
}
},
"additionalProperties": false,
"required": ["replace", "with"]
},
"default": []
},
"watch": { "watch": {
"description": "Enable re-building when files change.", "description": "Enable re-building when files change.",
"oneOf": [{ "type": "boolean" }, { "type": "object" }], "oneOf": [{ "type": "boolean" }, { "type": "object" }],

View File

@ -216,3 +216,28 @@ export default defineConfig({
``` ```
In that config file, you can configure Vite as you would normally do. For more information, see the [Vite.js documentation](https://vitejs.dev/config/). In that config file, you can configure Vite as you would normally do. For more information, see the [Vite.js documentation](https://vitejs.dev/config/).
## Set up file replacements
You can use the `replaceFiles()` plugin (`@nx/vite/plugins/rollup-replace-files.plugin`) to replace files in your build. You can import the plugin from `@nx/vite/plugins/rollup-replace-files.plugin`. And you can set it up like this:
```ts {% fileName="apps/my-app/vite.config.ts" %}
...
import { replaceFiles } from '@nx/vite/plugins/rollup-replace-files.plugin';
export default defineConfig({
...
plugins: [
...
replaceFiles([
{
replace: 'apps/my-app/src/environments/environment.ts',
with: 'apps/my-app/src/environments/environment.prod.ts',
},
]),
],
...
});
```

View File

@ -5,7 +5,7 @@
* @param {FileReplacement[]} replacements * @param {FileReplacement[]} replacements
* @return {({name: "rollup-plugin-replace-files", enforce: "pre" | "post" | undefined, Promise<resolveId>})} * @return {({name: "rollup-plugin-replace-files", enforce: "pre" | "post" | undefined, Promise<resolveId>})}
*/ */
export default function replaceFiles(replacements: FileReplacement[]): { export function replaceFiles(replacements: FileReplacement[]): {
name: string; name: string;
enforce: 'pre' | 'post' | undefined; enforce: 'pre' | 'post' | undefined;
resolveId( resolveId(

View File

@ -1,11 +1,9 @@
import type { FileReplacement } from '../../plugins/rollup-replace-files.plugin';
export interface ViteBuildExecutorOptions { export interface ViteBuildExecutorOptions {
outputPath?: string; outputPath?: string;
buildLibsFromSource?: boolean;
skipTypeCheck?: boolean; skipTypeCheck?: boolean;
configFile?: string; configFile?: string;
fileReplacements?: FileReplacement[];
watch?: boolean; watch?: boolean;
generatePackageJson?: boolean; generatePackageJson?: boolean;
includeDevDependenciesInPackageJson?: boolean; includeDevDependenciesInPackageJson?: boolean;
buildLibsFromSource?: boolean;
} }

View File

@ -34,28 +34,6 @@
"x-completion-type": "file", "x-completion-type": "file",
"x-completion-glob": "vite.config.@(js|ts)" "x-completion-glob": "vite.config.@(js|ts)"
}, },
"fileReplacements": {
"description": "Replace files with other files in the build.",
"type": "array",
"items": {
"type": "object",
"properties": {
"replace": {
"type": "string",
"description": "The file to be replaced.",
"x-completion-type": "file"
},
"with": {
"type": "string",
"description": "The file to replace with.",
"x-completion-type": "file"
}
},
"additionalProperties": false,
"required": ["replace", "with"]
},
"default": []
},
"watch": { "watch": {
"description": "Enable re-building when files change.", "description": "Enable re-building when files change.",
"oneOf": [ "oneOf": [

View File

@ -39,10 +39,10 @@ export default async function updateBuildDir(tree: Tree) {
configContents = updateTestConfig(configContents, projectConfig, config); configContents = updateTestConfig(configContents, projectConfig, config);
if (options.fileReplacements?.length > 0) { if (options['fileReplacements']?.length > 0) {
configContents = addFileReplacements( configContents = addFileReplacements(
configContents, configContents,
options.fileReplacements, options['fileReplacements'],
config config
); );
} }

View File

@ -212,9 +212,6 @@ export function addOrChangeBuildTarget(
project.targets ??= {}; project.targets ??= {};
if (project.targets[target]) { if (project.targets[target]) {
buildOptions.fileReplacements =
project.targets[target].options?.fileReplacements;
if (project.targets[target].executor === '@nxext/vite:build') { if (project.targets[target].executor === '@nxext/vite:build') {
buildOptions['base'] = project.targets[target].options?.baseHref; buildOptions['base'] = project.targets[target].options?.baseHref;
buildOptions['sourcemap'] = project.targets[target].options?.sourcemaps; buildOptions['sourcemap'] = project.targets[target].options?.sourcemaps;