Skip to content

Commit

Permalink
refactor: isInNodeModules util (#12588)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 26, 2023
1 parent ff92f2f commit fb3245a
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 21 deletions.
5 changes: 3 additions & 2 deletions packages/vite/src/node/optimizer/scan.ts
Expand Up @@ -18,6 +18,7 @@ import {
createDebugger,
dataUrlRE,
externalRE,
isInNodeModules,
isObject,
isOptimizable,
moduleListContains,
Expand Down Expand Up @@ -360,7 +361,7 @@ function esbuildScanPlugin(
// If we can optimize this html type, skip it so it's handled by the
// bare import resolve, and recorded as optimization dep.
if (
resolved.includes('node_modules') &&
isInNodeModules(resolved) &&
isOptimizable(resolved, config.optimizeDeps)
)
return
Expand Down Expand Up @@ -501,7 +502,7 @@ function esbuildScanPlugin(
if (shouldExternalizeDep(resolved, id)) {
return externalUnlessEntry({ path: id })
}
if (resolved.includes('node_modules') || include?.includes(id)) {
if (isInNodeModules(resolved) || include?.includes(id)) {
// dependency or forced included, externalize and stop crawling
if (isOptimizable(resolved, config.optimizeDeps)) {
depImports[id] = resolved
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -32,6 +32,7 @@ import {
isBuiltin,
isDataUrl,
isExternalUrl,
isInNodeModules,
isJSRequest,
joinUrlSegments,
moduleListContains,
Expand Down Expand Up @@ -618,7 +619,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
staticImportedUrls.add({ url: hmrUrl, id: resolvedId })
}
} else if (!importer.startsWith(clientDir)) {
if (!importer.includes('node_modules')) {
if (!isInNodeModules(importer)) {
// check @vite-ignore which suppresses dynamic import warning
const hasViteIgnore = hasViteIgnoreRE.test(
// complete expression inside parens
Expand Down
6 changes: 2 additions & 4 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -11,6 +11,7 @@ import {
combineSourcemaps,
isDataUrl,
isExternalUrl,
isInNodeModules,
moduleListContains,
} from '../utils'
import type { Plugin } from '../plugin'
Expand Down Expand Up @@ -185,10 +186,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
},

async transform(source, importer) {
if (
importer.includes('node_modules') &&
!dynamicImportPrefixRE.test(source)
) {
if (isInNodeModules(importer) && !dynamicImportPrefixRE.test(source)) {
return
}

Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/preAlias.ts
Expand Up @@ -11,6 +11,7 @@ import { createIsConfiguredAsSsrExternal } from '../ssr/ssrExternal'
import {
bareImportRE,
cleanUrl,
isInNodeModules,
isOptimizable,
moduleListContains,
} from '../utils'
Expand Down Expand Up @@ -60,7 +61,7 @@ export function preAliasPlugin(config: ResolvedConfig): Plugin {
fs.existsSync(resolvedId) &&
!moduleListContains(optimizeDeps.exclude, id) &&
path.isAbsolute(resolvedId) &&
(resolvedId.includes('node_modules') ||
(isInNodeModules(resolvedId) ||
optimizeDeps.include?.includes(id)) &&
isOptimizable(resolvedId, optimizeDeps) &&
!(isBuild && ssr && isConfiguredAsExternal(id)) &&
Expand Down
14 changes: 6 additions & 8 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -26,6 +26,7 @@ import {
isBuiltin,
isDataUrl,
isExternalUrl,
isInNodeModules,
isNonDriveRelativeAbsolutePath,
isObject,
isOptimizable,
Expand Down Expand Up @@ -57,7 +58,6 @@ export const browserExternalId = '__vite-browser-external'
// special id for packages that are optional peer deps
export const optionalPeerDepId = '__vite-optional-peer-dep'

const nodeModulesInPathRE = /(?:^|\/)node_modules\//
const subpathImportsPrefix = '#'

const startsWithWordCharRE = /^\w/
Expand Down Expand Up @@ -454,9 +454,7 @@ function ensureVersionQuery(
// as if they would have been imported through a bare import
// Use the original id to do the check as the resolved id may be the real
// file path after symlinks resolution
const isNodeModule =
nodeModulesInPathRE.test(normalizePath(id)) ||
nodeModulesInPathRE.test(normalizePath(resolved))
const isNodeModule = isInNodeModules(id) || isInNodeModules(resolved)

if (isNodeModule && !resolved.match(DEP_VERSION_RE)) {
const versionHash = depsOptimizer.metadata.browserHash
Expand Down Expand Up @@ -497,7 +495,7 @@ function tryFsResolve(
// Dependencies like es5-ext use `#` in their paths. We don't support `#` in user
// source code so we only need to perform the check for dependencies.
// We don't support `?` in node_modules paths, so we only need to check in this branch.
if (postfixIndex >= 0 && fsPath.includes('node_modules')) {
if (postfixIndex >= 0 && isInNodeModules(fsPath)) {
const res = tryCleanFsResolve(
fsPath,
options,
Expand Down Expand Up @@ -748,7 +746,7 @@ export function tryNodeResolve(
return resolved
}
// don't external symlink packages
if (!allowLinkedExternal && !resolved.id.includes('node_modules')) {
if (!allowLinkedExternal && !isInNodeModules(resolved.id)) {
return resolved
}
const resolvedExt = path.extname(resolved.id)
Expand Down Expand Up @@ -783,7 +781,7 @@ export function tryNodeResolve(

if (
!options.ssrOptimizeCheck &&
(!resolved.includes('node_modules') || // linked
(!isInNodeModules(resolved) || // linked
!depsOptimizer || // resolving before listening to the server
options.scan) // initial esbuild scan phase
) {
Expand All @@ -805,7 +803,7 @@ export function tryNodeResolve(

const skipOptimization =
!isJsType ||
importer?.includes('node_modules') ||
(importer && isInNodeModules(importer)) ||
exclude?.includes(pkgId) ||
exclude?.includes(id) ||
SPECIAL_QUERY_RE.test(resolved) ||
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/splitVendorChunk.ts
Expand Up @@ -4,6 +4,7 @@ import type {
ManualChunkMeta,
OutputOptions,
} from 'rollup'
import { isInNodeModules } from '../utils'
import type { UserConfig } from '../../node'
import type { Plugin } from '../plugin'

Expand Down Expand Up @@ -41,7 +42,7 @@ export function splitVendorChunk(
const cache = options.cache ?? new SplitVendorChunkCache()
return (id, { getModuleInfo }) => {
if (
id.includes('node_modules') &&
isInNodeModules(id) &&
!isCSSRequest(id) &&
staticImportedByEntry(id, getModuleInfo, cache.cache)
) {
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -24,6 +24,7 @@ import type { InlineConfig, ResolvedConfig } from '../config'
import { isDepsOptimizerEnabled, resolveConfig } from '../config'
import {
diffDnsOrderChange,
isInNodeModules,
isParentDirectory,
mergeConfig,
normalizePath,
Expand Down Expand Up @@ -767,8 +768,7 @@ export function resolveServerOptions(
sourcemapIgnoreList:
raw?.sourcemapIgnoreList === false
? () => false
: raw?.sourcemapIgnoreList ||
((sourcePath) => sourcePath.includes('node_modules')),
: raw?.sourcemapIgnoreList || isInNodeModules,
middlewareMode: !!raw?.middlewareMode,
}
let allowDirs = server.fs?.allow
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/ssr/ssrExternal.ts
Expand Up @@ -9,6 +9,7 @@ import {
createFilter,
isBuiltin,
isDefined,
isInNodeModules,
lookupFile,
normalizePath,
} from '../utils'
Expand Down Expand Up @@ -259,7 +260,7 @@ function cjsSsrCollectExternals(
// no main entry, but deep imports may be allowed
const pkgDir = resolvePackageData(id, root)?.dir
if (pkgDir) {
if (pkgDir.includes('node_modules')) {
if (isInNodeModules(pkgDir)) {
ssrExternals.add(id)
} else {
depsToTrace.add(path.dirname(pkgDir))
Expand All @@ -276,7 +277,7 @@ function cjsSsrCollectExternals(
ssrExternals.add(id)
}
// trace the dependencies of linked packages
else if (!esmEntry.includes('node_modules')) {
else if (!isInNodeModules(esmEntry)) {
const pkgDir = resolvePackageData(id, root)?.dir
if (pkgDir) {
depsToTrace.add(pkgDir)
Expand Down
4 changes: 4 additions & 0 deletions packages/vite/src/node/utils.ts
Expand Up @@ -116,6 +116,10 @@ export function isBuiltin(id: string): boolean {
)
}

export function isInNodeModules(id: string): boolean {
return id.includes('node_modules')
}

export function moduleListContains(
moduleList: string[] | undefined,
id: string,
Expand Down

0 comments on commit fb3245a

Please sign in to comment.