Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite): add configDir to vite config #14559

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports[`@nrwl/vite:configuration library mode should add config for building li
import { join } from 'path';

export default defineConfig({
cacheDir: '../node_modules/.vite/my-lib',



Expand Down Expand Up @@ -67,6 +68,7 @@ exports[`@nrwl/vite:configuration library mode should set up non buildable libra
import { join } from 'path';

export default defineConfig({
cacheDir: '../../node_modules/.vite/react-lib-nonb-jest',



Expand Down Expand Up @@ -190,7 +192,7 @@ import { defineConfig } from 'vite';

// Configuration for building your library.
// See: https://vitejs.dev/guide/build.html#library-mode
build: {
cacheDir: '../../node_modules/.vite/react-lib-nonb-vitest',build: {
lib: {
// Could also be a dictionary or array of multiple entry points.
entry: 'src/index.ts',
Expand Down Expand Up @@ -382,6 +384,7 @@ exports[`@nrwl/vite:configuration transform React app to use Vite should create


export default defineConfig({
cacheDir: '../../node_modules/.vite/my-test-react-app',

server:{
port: 4200,
Expand Down Expand Up @@ -546,6 +549,7 @@ exports[`@nrwl/vite:configuration transform Web app to use Vite should create vi


export default defineConfig({
cacheDir: '../../node_modules/.vite/my-test-web-app',

server:{
port: 4200,
Expand Down Expand Up @@ -699,6 +703,7 @@ exports[`@nrwl/vite:configuration vitest should create a vitest configuration if


export default defineConfig({
cacheDir: '../../node_modules/.vite/my-test-react-app',

server:{
port: 4200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports[`vitest generator insourceTests should add the insourceSource option in


export default defineConfig({
cacheDir: '../../node_modules/.vite/my-test-react-app',



Expand Down Expand Up @@ -54,6 +55,7 @@ exports[`vitest generator vite.config should create correct vite.config.ts file


export default defineConfig({
cacheDir: '../../node_modules/.vite/my-test-react-app',



Expand Down Expand Up @@ -97,6 +99,7 @@ exports[`vitest generator vite.config should create correct vite.config.ts file


export default defineConfig({
cacheDir: '../../node_modules/.vite/react-lib-nonb-jest',



Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ensureBuildOptionsInViteConfig should add build and test options if defineConfig is empty 1`] = `
exports[`ensureViteConfigIsCorrect should add build and test options if defineConfig is empty 1`] = `
"import dts from 'vite-plugin-dts';
import { join } from 'path';

Expand Down Expand Up @@ -49,7 +49,7 @@ import { join } from 'path';
"
`;

exports[`ensureBuildOptionsInViteConfig should add build option but not update test option if test already setup 1`] = `
exports[`ensureViteConfigIsCorrect should add build option but not update test option if test already setup 1`] = `
"import dts from 'vite-plugin-dts';
import { join } from 'path';
import { defineConfig } from 'vite';
Expand Down Expand Up @@ -89,19 +89,22 @@ import { defineConfig } from 'vite';
],

test: {
...{
globals: true,
cache: {
dir: '../../node_modules/.vitest',
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
},
...{\\"globals\\":true,\\"cache\\":{\\"dir\\":\\"../node_modules/.vitest\\"},\\"environment\\":\\"jsdom\\",\\"include\\":[\\"src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}\\"]}
},

});
"
`;

exports[`ensureBuildOptionsInViteConfig should add build options if build options don't exist 1`] = `
exports[`ensureViteConfigIsCorrect should add build options if build options don't exist 1`] = `
"import dts from 'vite-plugin-dts';
import { join } from 'path';
import { defineConfig } from 'vite';
Expand Down Expand Up @@ -141,19 +144,22 @@ import { defineConfig } from 'vite';
],

test: {
...{
globals: true,
cache: {
dir: '../../node_modules/.vitest',
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
},
...{\\"globals\\":true,\\"cache\\":{\\"dir\\":\\"../node_modules/.vitest\\"},\\"environment\\":\\"jsdom\\",\\"include\\":[\\"src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}\\"]}
},

});
"
`;

exports[`ensureBuildOptionsInViteConfig should add build options if defineConfig is not used 1`] = `
exports[`ensureViteConfigIsCorrect should add build options if defineConfig is not used 1`] = `
"import dts from 'vite-plugin-dts';
import { join } from 'path';
import { defineConfig } from 'vite';
Expand Down Expand Up @@ -202,7 +208,7 @@ import { defineConfig } from 'vite';
"
`;

exports[`ensureBuildOptionsInViteConfig should add build options if it is using conditional config - do nothing for test 1`] = `
exports[`ensureViteConfigIsCorrect should add build options if it is using conditional config - do nothing for test 1`] = `
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
Expand All @@ -225,7 +231,7 @@ exports[`ensureBuildOptionsInViteConfig should add build options if it is using
"
`;

exports[`ensureBuildOptionsInViteConfig should add new build options if some build options already exist 1`] = `
exports[`ensureViteConfigIsCorrect should add new build options if some build options already exist 1`] = `
"import dts from 'vite-plugin-dts';
import { join } from 'path';
import { defineConfig } from 'vite';
Expand All @@ -248,13 +254,16 @@ import { defineConfig } from 'vite';
],

test: {
...{
globals: true,
cache: {
dir: '../../node_modules/.vitest',
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
},
...{\\"globals\\":true,\\"cache\\":{\\"dir\\":\\"../node_modules/.vitest\\"},\\"environment\\":\\"jsdom\\",\\"include\\":[\\"src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}\\"]}
},

build: {
...{
Expand All @@ -267,17 +276,18 @@ import { defineConfig } from 'vite';
"
`;

exports[`ensureBuildOptionsInViteConfig should not do anything if cannot understand syntax of vite config 1`] = `"console.log('Unknown syntax')"`;
exports[`ensureViteConfigIsCorrect should not do anything if cannot understand syntax of vite config 1`] = `"console.log('Unknown syntax')"`;

exports[`ensureBuildOptionsInViteConfig should not do anything if project has everything setup already 1`] = `
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
exports[`ensureViteConfigIsCorrect should not do anything if project has everything setup already 1`] = `
"import dts from 'vite-plugin-dts';
import { join } from 'path';
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';

export default defineConfig({
plugins: [
...[
dts({
tsConfigFilePath: join(__dirname, 'tsconfig.lib.json'),
// Faster builds by skipping tests. Set this to false to enable type checking.
Expand All @@ -288,10 +298,17 @@ exports[`ensureBuildOptionsInViteConfig should not do anything if project has ev
root: '../../../',
}),
],
dts({
tsConfigFilePath: join(__dirname, 'tsconfig.lib.json'),
// Faster builds by skipping tests. Set this to false to enable type checking.
skipDiagnostics: true,
}),
],

// Configuration for building your library.
// See: https://vitejs.dev/guide/build.html#library-mode
build: {
...{
lib: {
// Could also be a dictionary or array of multiple entry points.
entry: 'src/index.ts',
Expand All @@ -306,20 +323,25 @@ exports[`ensureBuildOptionsInViteConfig should not do anything if project has ev
external: ['react', 'react-dom', 'react/jsx-runtime'],
},
},
...{\\"lib\\":{\\"entry\\":\\"src/index.ts\\",\\"name\\":\\"my-app\\",\\"fileName\\":\\"index\\",\\"formats\\":[\\"es\\",\\"cjs\\"]},\\"rollupOptions\\":{\\"external\\":[\\"'react', 'react-dom', 'react/jsx-runtime'\\"]}}
},

test: {
...{
globals: true,
cache: {
dir: '../../../node_modules/.vitest',
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
},
...{\\"globals\\":true,\\"cache\\":{\\"dir\\":\\"../node_modules/.vitest\\"},\\"environment\\":\\"jsdom\\",\\"include\\":[\\"src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}\\"]}
},
});
"
`;

exports[`ensureBuildOptionsInViteConfig should update both test and build options - keep existing settings 1`] = `
exports[`ensureViteConfigIsCorrect should update both test and build options - keep existing settings 1`] = `
"import dts from 'vite-plugin-dts';
import { join } from 'path';
import { defineConfig } from 'vite';
Expand Down
12 changes: 10 additions & 2 deletions packages/vite/src/utils/generator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ViteDevServerExecutorOptions } from '../executors/dev-server/schema';
import { VitePreviewServerExecutorOptions } from '../executors/preview-server/schema';
import { VitestExecutorOptions } from '../executors/test/schema';
import { Schema } from '../generators/configuration/schema';
import { ensureBuildOptionsInViteConfig } from './vite-config-edit-utils';
import { ensureViteConfigIsCorrect } from './vite-config-edit-utils';

export type Target = 'build' | 'serve' | 'test' | 'preview';
export type TargetFlags = Partial<Record<Target, boolean>>;
Expand Down Expand Up @@ -550,6 +550,10 @@ export function createOrEditViteConfig(
// ],
// },`;

const cacheDir = `cacheDir: '${offsetFromRoot(
projectConfig.root
)}node_modules/.vite/${options.project}',`;

if (tree.exists(viteConfigPath)) {
handleViteConfigFileExists(
tree,
Expand All @@ -560,6 +564,7 @@ export function createOrEditViteConfig(
dtsImportLine,
pluginOption,
testOption,
cacheDir,
offsetFromRoot(projectConfig.root),
projectAlreadyHasViteTargets
);
Expand All @@ -574,6 +579,7 @@ export function createOrEditViteConfig(
${dtsImportLine}

export default defineConfig({
${cacheDir}
${devServerOption}
${previewServerOption}
${pluginOption}
Expand Down Expand Up @@ -725,6 +731,7 @@ function handleViteConfigFileExists(
dtsImportLine: string,
pluginOption: string,
testOption: string,
cacheDir: string,
offsetFromRoot: string,
projectAlreadyHasViteTargets?: TargetFlags
) {
Expand Down Expand Up @@ -757,7 +764,7 @@ function handleViteConfigFileExists(
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
};

const changed = ensureBuildOptionsInViteConfig(
const changed = ensureViteConfigIsCorrect(
tree,
viteConfigPath,
buildOption,
Expand All @@ -767,6 +774,7 @@ function handleViteConfigFileExists(
pluginOption,
testOption,
testOptionObject,
cacheDir,
projectAlreadyHasViteTargets
);

Expand Down
22 changes: 11 additions & 11 deletions packages/vite/src/utils/vite-config-edit-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import {
testOption,
testOptionObject,
} from './test-files/test-vite-configs';
import { ensureBuildOptionsInViteConfig } from './vite-config-edit-utils';
import { ensureViteConfigIsCorrect } from './vite-config-edit-utils';

describe('ensureBuildOptionsInViteConfig', () => {
describe('ensureViteConfigIsCorrect', () => {
let tree: Tree;

beforeEach(() => {
Expand All @@ -29,7 +29,7 @@ describe('ensureBuildOptionsInViteConfig', () => {

it("should add build options if build options don't exist", () => {
tree.write('apps/my-app/vite.config.ts', noBuildOptions);
ensureBuildOptionsInViteConfig(
ensureViteConfigIsCorrect(
tree,
'apps/my-app/vite.config.ts',
buildOption,
Expand All @@ -53,7 +53,7 @@ describe('ensureBuildOptionsInViteConfig', () => {

it('should add new build options if some build options already exist', () => {
tree.write('apps/my-app/vite.config.ts', someBuildOptions);
ensureBuildOptionsInViteConfig(
ensureViteConfigIsCorrect(
tree,
'apps/my-app/vite.config.ts',
buildOption,
Expand All @@ -77,7 +77,7 @@ describe('ensureBuildOptionsInViteConfig', () => {

it('should add build and test options if defineConfig is empty', () => {
tree.write('apps/my-app/vite.config.ts', noContentDefineConfig);
ensureBuildOptionsInViteConfig(
ensureViteConfigIsCorrect(
tree,
'apps/my-app/vite.config.ts',
buildOption,
Expand All @@ -101,7 +101,7 @@ describe('ensureBuildOptionsInViteConfig', () => {

it('should add build options if it is using conditional config - do nothing for test', () => {
tree.write('apps/my-app/vite.config.ts', conditionalConfig);
ensureBuildOptionsInViteConfig(
ensureViteConfigIsCorrect(
tree,
'apps/my-app/vite.config.ts',
buildOption,
Expand All @@ -125,7 +125,7 @@ describe('ensureBuildOptionsInViteConfig', () => {

it('should add build options if defineConfig is not used', () => {
tree.write('apps/my-app/vite.config.ts', configNoDefineConfig);
ensureBuildOptionsInViteConfig(
ensureViteConfigIsCorrect(
tree,
'apps/my-app/vite.config.ts',
buildOption,
Expand All @@ -149,7 +149,7 @@ describe('ensureBuildOptionsInViteConfig', () => {

it('should not do anything if cannot understand syntax of vite config', () => {
tree.write('apps/my-app/vite.config.ts', `console.log('Unknown syntax')`);
ensureBuildOptionsInViteConfig(
ensureViteConfigIsCorrect(
tree,
'apps/my-app/vite.config.ts',
buildOption,
Expand All @@ -167,7 +167,7 @@ describe('ensureBuildOptionsInViteConfig', () => {

it('should not do anything if project has everything setup already', () => {
tree.write('apps/my-app/vite.config.ts', hasEverything);
ensureBuildOptionsInViteConfig(
ensureViteConfigIsCorrect(
tree,
'apps/my-app/vite.config.ts',
buildOption,
Expand All @@ -185,7 +185,7 @@ describe('ensureBuildOptionsInViteConfig', () => {

it('should add build option but not update test option if test already setup', () => {
tree.write('apps/my-app/vite.config.ts', noBuildOptionsHasTestOption);
ensureBuildOptionsInViteConfig(
ensureViteConfigIsCorrect(
tree,
'apps/my-app/vite.config.ts',
buildOption,
Expand All @@ -203,7 +203,7 @@ describe('ensureBuildOptionsInViteConfig', () => {

it('should update both test and build options - keep existing settings', () => {
tree.write('apps/my-app/vite.config.ts', someBuildOptionsSomeTestOption);
ensureBuildOptionsInViteConfig(
ensureViteConfigIsCorrect(
tree,
'apps/my-app/vite.config.ts',
buildOption,
Expand Down
Loading