fix(vite): add vite temp files to gitignore #28371 (#28443)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
When Vite's tries to handle a `.ts` config file it builds it to JS with
a `.timestamp-` suffix. These files are still picked up by Nx when they
shouldn't as it's a temp file
(https://github.com/vitejs/vite/issues/13267).



## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
Add these temp files to gitignore to prevent processing.


## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #28371
This commit is contained in:
Colum Ferry 2024-10-16 13:32:42 +01:00 committed by GitHub
parent 4b6c831a48
commit 5dbea2e16f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 80 additions and 6 deletions

View File

@ -1,13 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`app generated files content - as-provided - my-app general application should add nuxt entries in .gitignore 1`] = ` exports[`app generated files content - as-provided - my-app general application should add nuxt entries in .gitignore 1`] = `
" "# Nuxt dev/build outputs
# Nuxt dev/build outputs
.output .output
.data .data
.nuxt .nuxt
.nitro .nitro
.cache" .cache
**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*"
`; `;
exports[`app generated files content - as-provided - my-app general application should add the nuxt and vitest plugins 1`] = ` exports[`app generated files content - as-provided - my-app general application should add the nuxt and vitest plugins 1`] = `
@ -364,13 +364,13 @@ export default defineNuxtConfig({
`; `;
exports[`app generated files content - as-provided - myApp general application should add nuxt entries in .gitignore 1`] = ` exports[`app generated files content - as-provided - myApp general application should add nuxt entries in .gitignore 1`] = `
" "# Nuxt dev/build outputs
# Nuxt dev/build outputs
.output .output
.data .data
.nuxt .nuxt
.nitro .nitro
.cache" .cache
**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*"
`; `;
exports[`app generated files content - as-provided - myApp general application should add the nuxt and vitest plugins 1`] = ` exports[`app generated files content - as-provided - myApp general application should add the nuxt and vitest plugins 1`] = `

View File

@ -127,4 +127,20 @@ describe('@nx/vite:init', () => {
" "
`); `);
}); });
it(`should not add multiple instances of the same vite temp file glob to gitignore`, async () => {
// ARRANGE
tree.write(
'.gitignore',
'**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*'
);
// ACT
await initGenerator(tree, {});
// ASSERT
expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(
`"**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*"`
);
});
}); });

View File

@ -13,6 +13,7 @@ import { setupPathsPlugin } from '../setup-paths-plugin/setup-paths-plugin';
import { createNodesV2 } from '../../plugins/plugin'; import { createNodesV2 } from '../../plugins/plugin';
import { InitGeneratorSchema } from './schema'; import { InitGeneratorSchema } from './schema';
import { checkDependenciesInstalled, moveToDevDependencies } from './lib/utils'; import { checkDependenciesInstalled, moveToDevDependencies } from './lib/utils';
import { addViteTempFilesToGitIgnore } from '../../utils/add-vite-temp-files-to-gitignore';
export function updateNxJsonSettings(tree: Tree) { export function updateNxJsonSettings(tree: Tree) {
const nxJson = readNxJson(tree); const nxJson = readNxJson(tree);
@ -83,6 +84,7 @@ export async function initGeneratorInternal(
} }
updateNxJsonSettings(tree); updateNxJsonSettings(tree);
addViteTempFilesToGitIgnore(tree);
if (schema.setupPathsPlugin) { if (schema.setupPathsPlugin) {
await setupPathsPlugin(tree, { skipFormat: true }); await setupPathsPlugin(tree, { skipFormat: true });

View File

@ -0,0 +1,33 @@
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import addViteTempFilesToGitIgnore from './add-vite-temp-files-to-git-ignore';
describe('addViteTempFilesToGitIgnore', () => {
it('should update an existing .gitignore file to add the glob correctly', () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace();
tree.write('.gitignore', '.idea');
// ACT
addViteTempFilesToGitIgnore(tree);
// ASSERT
expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(`
".idea
**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*"
`);
});
it('should write a new .gitignore file to add the glob correctly', () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace();
tree.delete('.gitignore');
// ACT
addViteTempFilesToGitIgnore(tree);
// ASSERT
expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(
`"**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*"`
);
});
});

View File

@ -0,0 +1,6 @@
import { Tree } from '@nx/devkit';
import { addViteTempFilesToGitIgnore as _addViteTempFilesToGitIgnore } from '../../utils/add-vite-temp-files-to-gitignore';
export default function addViteTempFilesToGitIgnore(tree: Tree) {
_addViteTempFilesToGitIgnore(tree);
}

View File

@ -0,0 +1,16 @@
import { stripIndents, Tree } from '@nx/devkit';
export function addViteTempFilesToGitIgnore(tree: Tree) {
let newGitIgnoreContents = `**/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp*`;
if (tree.exists('.gitignore')) {
const gitIgnoreContents = tree.read('.gitignore', 'utf-8');
if (!gitIgnoreContents.includes(newGitIgnoreContents)) {
newGitIgnoreContents = stripIndents`${gitIgnoreContents}
${newGitIgnoreContents}`;
tree.write('.gitignore', newGitIgnoreContents);
}
} else {
tree.write('.gitignore', newGitIgnoreContents);
}
}

View File

@ -346,6 +346,7 @@ exports[`application generator should set up project correctly with given option
[ [
".eslintignore", ".eslintignore",
".eslintrc.json", ".eslintrc.json",
".gitignore",
".prettierignore", ".prettierignore",
".prettierrc", ".prettierrc",
".vscode/extensions.json", ".vscode/extensions.json",