Skip to content

Commit

Permalink
fix(vite): add configDir to vite config
Browse files Browse the repository at this point in the history
BREAKING CHANGE: \
  • Loading branch information
mandarini committed Jan 23, 2023
1 parent 559faa7 commit ef5eb85
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 23 deletions.
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 @@ -101,7 +101,7 @@ import { defineConfig } from 'vite';
"
`;

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 @@ -153,7 +153,7 @@ import { defineConfig } from 'vite';
"
`;

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 +202,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 +225,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 Down Expand Up @@ -267,9 +267,9 @@ 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`] = `
exports[`ensureViteConfigIsCorrect should not do anything if project has everything setup already 1`] = `
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
Expand Down Expand Up @@ -319,7 +319,7 @@ exports[`ensureBuildOptionsInViteConfig should not do anything if project has ev
"
`;

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
99 changes: 98 additions & 1 deletion packages/vite/src/utils/vite-config-edit-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ts = require('typescript');
import { tsquery } from '@phenomnomnominal/tsquery';
import { TargetFlags } from './generator-utils';

export function ensureBuildOptionsInViteConfig(
export function ensureViteConfigIsCorrect(
tree: Tree,
path: string,
buildConfigString: string,
Expand All @@ -14,6 +14,7 @@ export function ensureBuildOptionsInViteConfig(
pluginOption: string,
testConfigString: string,
testConfigObject: {},
cacheDir: string,
projectAlreadyHasViteTargets?: TargetFlags
): boolean {
const fileContent = tree.read(path, 'utf-8');
Expand Down Expand Up @@ -45,6 +46,12 @@ export function ensureBuildOptionsInViteConfig(
);
}

if (cacheDir?.length) {
updatedContent = handleCacheDirNode(
updatedContent ?? fileContent,
cacheDir
);
}
if (updatedContent) {
tree.write(path, updatedContent);
return true;
Expand Down Expand Up @@ -369,3 +376,93 @@ function handlePluginNode(
}
return undefined;
}

function handleCacheDirNode(
appFileContent: string,
cacheDir: string
): string | undefined {
const file = tsquery.ast(appFileContent);
const cacheDirNode = tsquery.query(
file,
'PropertyAssignment:has(Identifier[name="cacheDir"])'
);

let writeFile = false;

if (cacheDirNode.length) {
// CacheDir node already exists
// Do not change it
} else {
// cacheDir node does not exist yet
// So make one from scratch

const foundDefineConfig = tsquery.query(
file,
'CallExpression:has(Identifier[name="defineConfig"])'
);

if (foundDefineConfig.length) {
const conditionalConfig = tsquery.query(
foundDefineConfig[0],
'ArrowFunction'
);

if (conditionalConfig.length) {
// We are NOT transforming the conditional config
// with cacheDir
writeFile = false;
} else {
const propertyAssignments = tsquery.query(
foundDefineConfig[0],
'PropertyAssignment'
);

if (propertyAssignments.length) {
appFileContent = applyChangesToString(appFileContent, [
{
type: ChangeType.Insert,
index: propertyAssignments[0].getStart(),
text: cacheDir,
},
]);
writeFile = true;
} else {
appFileContent = applyChangesToString(appFileContent, [
{
type: ChangeType.Insert,
index: foundDefineConfig[0].getStart() + 14,
text: cacheDir,
},
]);
writeFile = true;
}
}
} else {
// cacheDir option does not exist and defineConfig is not used
// could also potentially be invalid syntax, so try-catch
try {
const defaultExport = tsquery.query(file, 'ExportAssignment');
const found = tsquery?.query(
defaultExport?.[0],
'ObjectLiteralExpression'
);
const startOfObject = found?.[0].getStart();
appFileContent = applyChangesToString(appFileContent, [
{
type: ChangeType.Insert,
index: startOfObject + 1,
text: cacheDir,
},
]);
writeFile = true;
} catch {
writeFile = false;
}
}
}

if (writeFile) {
return appFileContent;
}
return undefined;
}

0 comments on commit ef5eb85

Please sign in to comment.