Skip to content

Commit

Permalink
fix: better dependency non-js type file handling
Browse files Browse the repository at this point in the history
- avoid appending version query to externalized non-js dep files
  • Loading branch information
yyx990803 committed Feb 4, 2021
1 parent 3fad3ba commit 1fdc710
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 2 additions & 0 deletions packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import path from 'path'

export const SUPPORTED_EXTS = ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json']

export const JS_TYPES_RE = /\.(j|t)sx?$|\.mjs$/

export const SPECIAL_QUERY_RE = /[\?&](worker|raw|url)\b/

export const DEP_CACHE_DIR = `.vite`
Expand Down
7 changes: 3 additions & 4 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path'
import glob from 'fast-glob'
import { ResolvedConfig } from '..'
import { Loader, Plugin } from 'esbuild'
import { KNOWN_ASSET_TYPES, SPECIAL_QUERY_RE } from '../constants'
import { KNOWN_ASSET_TYPES, JS_TYPES_RE, SPECIAL_QUERY_RE } from '../constants'
import {
createDebugger,
emptyDir,
Expand All @@ -25,7 +25,6 @@ import { ensureService } from '../plugins/esbuild'

const debug = createDebugger('vite:deps')

const jsTypesRE = /\.(j|t)sx?$|\.mjs$/
const htmlTypesRE = /\.(html|vue|svelte)$/

export async function scanImports(
Expand Down Expand Up @@ -301,7 +300,7 @@ function esbuildScanPlugin(
// for jsx/tsx, we need to access the content and check for
// presence of import.meta.glob, since it results in import relationships
// but isn't crawled by esbuild.
build.onLoad({ filter: jsTypesRE }, ({ path: id }) => {
build.onLoad({ filter: JS_TYPES_RE }, ({ path: id }) => {
let ext = path.extname(id).slice(1)
if (ext === 'mjs') ext = 'js'

Expand Down Expand Up @@ -363,7 +362,7 @@ export function shouldExternalizeDep(resolvedId: string, rawId?: string) {
return true
}
// resovled is not a js type
if (!jsTypesRE.test(resolvedId)) {
if (!JS_TYPES_RE.test(resolvedId)) {
return true
}
}
19 changes: 12 additions & 7 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import fs from 'fs'
import path from 'path'
import { Plugin } from '../plugin'
import chalk from 'chalk'
import { FS_PREFIX, SPECIAL_QUERY_RE, SUPPORTED_EXTS } from '../constants'
import {
FS_PREFIX,
JS_TYPES_RE,
SPECIAL_QUERY_RE,
SUPPORTED_EXTS
} from '../constants'
import {
isBuiltin,
bareImportRE,
Expand All @@ -23,7 +28,6 @@ import slash from 'slash'
import { createFilter } from '@rollup/pluginutils'
import { PartialResolvedId } from 'rollup'
import { resolve as _resolveExports } from 'resolve.exports'
import { isCSSRequest } from './css'

const altMainFields = [
'module',
Expand Down Expand Up @@ -381,21 +385,22 @@ export function tryNodeResolve(
return { id: resolved }
}
// if we reach here, it's a valid dep import that hasn't been optimzied.
const isJsType = JS_TYPES_RE.test(resolved)
const exclude = server.config.optimizeDeps?.exclude
if (
!isJsType ||
importer?.includes('node_modules') ||
exclude?.includes(pkgId) ||
exclude?.includes(id) ||
isCSSRequest(resolved) ||
server.config.assetsInclude(resolved) ||
resolved.endsWith('.json') ||
SPECIAL_QUERY_RE.test(resolved)
) {
// excluded from optimization
// Inject a version query to npm deps so that the browser
// can cache it without revalidation.
// can cache it without revalidation, but only do so for known js types.
// otherwise we may introduce duplicated modules for externalized files
// from pre-bundled deps.
const versionHash = server._optimizeDepsMetadata?.browserHash
if (versionHash) {
if (versionHash && isJsType) {
resolved = injectQuery(resolved, `v=${versionHash}`)
}
} else {
Expand Down

0 comments on commit 1fdc710

Please sign in to comment.