[remix] Improve hueristics for detecting Remix + Vite (#11256)

In particular, when both `remix.config` and `vite.config` files exist,
run some additional checks to try to detect if the project is using the
Vite plugin or not.
This commit is contained in:
Nathan Rajlich
2024-03-12 15:05:32 -07:00
committed by GitHub
parent 4111fbaa89
commit 58ef91bfe8
15 changed files with 85 additions and 10 deletions

View File

@@ -1,9 +1,8 @@
import { build as buildVite } from './build-vite';
import { build as buildLegacy } from './build-legacy';
import { findConfig } from './utils';
import { isVite } from './utils';
import type { BuildV2 } from '@vercel/build-utils';
export const build: BuildV2 = opts => {
const isLegacy = findConfig(opts.workPath, 'remix.config');
return isLegacy ? buildLegacy(opts) : buildVite(opts);
return isVite(opts.workPath) ? buildVite(opts) : buildLegacy(opts);
};

View File

@@ -1,5 +1,5 @@
import semver from 'semver';
import { existsSync, promises as fs } from 'fs';
import { existsSync, readFileSync, promises as fs } from 'fs';
import { basename, dirname, join, relative, resolve, sep } from 'path';
import { pathToRegexp, Key } from 'path-to-regexp';
import { debug, type PackageJson } from '@vercel/build-utils';
@@ -420,3 +420,30 @@ export function logNftWarnings(warnings: Set<Error>, required?: string) {
}
}
}
export function isVite(dir: string): boolean {
const viteConfig = findConfig(dir, 'vite.config', ['.js', '.ts']);
if (!viteConfig) return false;
const remixConfig = findConfig(dir, 'remix.config');
if (!remixConfig) return true;
// `remix.config` and `vite.config` exist, so check a couple other ways
// Is `vite:build` found in the `package.json` "build" script?
const pkg: PackageJson = JSON.parse(
readFileSync(join(dir, 'package.json'), 'utf8')
);
if (pkg.scripts?.build && /\bvite:build\b/.test(pkg.scripts.build)) {
return true;
}
// Is `@remix-run/dev` package found in `vite.config`?
const viteConfigContents = readFileSync(viteConfig, 'utf8');
if (/['"]@remix-run\/dev['"]/.test(viteConfigContents)) {
return true;
}
// If none of those conditions matched, then treat it as a legacy project and print a warning
return false;
}