Skip to content

Commit ca58d38

Browse files
committed
fix: leave another app's workspace Vite config alone in ng add
detectSetup took a workspace-root vite.config for any project without its own, so ng add on a nested app that does not use Vite edited the root app's config. A root config now counts only when the project lives at the workspace root, or when the config names the project's root or sourceRoot. Otherwise the project's own server or CLI setup is used. A test covers a root Vite app next to a nested app without Vite.
1 parent 5d8ba86 commit ca58d38

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎packages/devtools/src/__tests__/schematics-ng-add.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,36 @@ describe('detectSetup', () => {
226226
);
227227
});
228228

229+
it('leaves a workspace-root Vite config to the root app when the nested project does not use Vite', () => {
230+
const angular = {
231+
projects: {
232+
site: { projectType: 'application', root: '', sourceRoot: 'src' },
233+
admin: {
234+
projectType: 'application',
235+
root: 'projects/admin',
236+
sourceRoot: 'projects/admin/src',
237+
architect: { build: { options: { browser: 'projects/admin/src/main.ts' } } },
238+
},
239+
},
240+
};
241+
const tree = workspace(
242+
{ 'vite.config.ts': VITE, 'projects/admin/src/main.ts': MAIN, 'src/main.ts': MAIN },
243+
angular,
244+
);
245+
expect(detectSetup(tree, { project: 'site' })).toEqual(
246+
expect.objectContaining({ kind: 'vite', viteConfig: 'vite.config.ts' }),
247+
);
248+
expect(detectSetup(tree, { project: 'admin' })).toEqual(
249+
expect.objectContaining({ kind: 'cli', main: 'projects/admin/src/main.ts' }),
250+
);
251+
const before = tree.readText('vite.config.ts');
252+
run(tree, { project: 'admin' });
253+
expect(tree.readText('vite.config.ts')).toBe(before);
254+
// A root config that names the nested project's folder belongs to it.
255+
const named = workspace({ 'vite.config.ts': `${VITE}\n// root: 'projects/admin'\n` }, angular);
256+
expect(detectSetup(named, { project: 'admin' }).kind).toBe('vite');
257+
});
258+
229259
it('works without angular.json', () => {
230260
expect(detectSetup(workspace({ 'vite.config.ts': VITE }, null))).toEqual(
231261
expect.objectContaining({ kind: 'vite', main: 'src/main.ts' }),

‎packages/devtools/src/schematics/ng-add/index.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ function pickProject(workspace: Json, name: string | undefined) {
156156
return chosen ? { name: chosen[0], project: chosen[1] } : undefined;
157157
}
158158

159+
/** Whether a config file names one of `folders` as a quoted path, such as `root: 'projects/shop'`. */
160+
function namesFolder(text: string, folders: string[]): boolean {
161+
return folders
162+
.filter((folder) => !!folder)
163+
.some((folder) => {
164+
const escaped = folder.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
165+
return new RegExp(`['"\`](?:\\./)?${escaped}/?['"\`]`).test(text);
166+
});
167+
}
168+
159169
/** Reads the setup from `angular.json`, the Vite config and `server.ts`. */
160170
export function detectSetup(tree: Tree, options: NgAddOptions = {}): Setup {
161171
let root = '';
@@ -200,9 +210,16 @@ export function detectSetup(tree: Tree, options: NgAddOptions = {}): Setup {
200210
tree.exists(path),
201211
);
202212
}
203-
const viteConfig = [root, '']
204-
.flatMap((dir) => VITE_CONFIGS.map((file) => join(dir, file)))
205-
.find((path) => tree.exists(path));
213+
const own = VITE_CONFIGS.map((file) => join(root, file)).find((path) => tree.exists(path));
214+
// A workspace-root Vite config belongs to the project only when the project
215+
// lives at the workspace root, or when the config names the project's folder.
216+
// Otherwise it serves another app, and editing it would wire the wrong one.
217+
const shared = own
218+
? undefined
219+
: VITE_CONFIGS.filter((file) => tree.exists(file)).find(
220+
(file) => !root || namesFolder(readText(tree, file) ?? '', [root, sourceRoot]),
221+
);
222+
const viteConfig = own ?? shared;
206223
if (viteConfig) return { kind: 'vite', project, main, viteConfig };
207224
if (server) {
208225
const text = readText(tree, server) ?? '';

0 commit comments

Comments
 (0)