Skip to content

Commit

Permalink
refactor: implement isExplicitImportRequired on client
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Aug 21, 2022
1 parent 6eb9641 commit 1a70a69
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
39 changes: 32 additions & 7 deletions packages/vite/src/client/client.ts
Expand Up @@ -388,12 +388,7 @@ export function removeStyle(id: string): void {
}
}

async function fetchUpdate({
path,
acceptedPath,
timestamp,
explicitImportRequired
}: Update) {
async function fetchUpdate({ path, acceptedPath, timestamp }: Update) {
const mod = hotModulesMap.get(path)
if (!mod) {
// In a code-splitting project,
Expand Down Expand Up @@ -426,6 +421,7 @@ async function fetchUpdate({
return deps.some((dep) => modulesToUpdate.has(dep))
})

const needImportQuery = isExplicitImportRequired(path)
await Promise.all(
Array.from(modulesToUpdate).map(async (dep) => {
const disposer = disposeMap.get(dep)
Expand All @@ -436,7 +432,7 @@ async function fetchUpdate({
/* @vite-ignore */
base +
path.slice(1) +
`?${explicitImportRequired ? 'import&' : ''}t=${timestamp}${
`?${needImportQuery ? 'import&' : ''}t=${timestamp}${
query ? `&${query}` : ''
}`
)
Expand Down Expand Up @@ -588,6 +584,35 @@ export function createHotContext(ownerPath: string): ViteHotContext {
return hot
}

const cssLangs = `\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)`
const cssLangRE = new RegExp(cssLangs)

const isCSSRequest = (request: string): boolean => cssLangRE.test(request)

const knownJsSrcRE = /\.((j|t)sx?|m[jt]s|vue|marko|svelte|astro)($|\?)/

const pathExtname = (url: string) => {
const parts = url.split('/')
const lastPart = parts[parts.length - 1]
const dotPos = lastPart.lastIndexOf('.')
return dotPos === -1 ? '' : lastPart.slice(dotPos)
}

const isJSRequest = (url: string): boolean => {
url = cleanUrl(url)
if (knownJsSrcRE.test(url)) {
return true
}
if (!pathExtname(url) && !url.endsWith('/')) {
return true
}
return false
}

function isExplicitImportRequired(url: string) {
return !isJSRequest(cleanUrl(url)) && !isCSSRequest(url)
}

/**
* urls here are dynamic import() urls that couldn't be statically analyzed
*/
Expand Down
5 changes: 0 additions & 5 deletions packages/vite/src/node/server/hmr.ts
Expand Up @@ -9,7 +9,6 @@ import { createDebugger, normalizePath, unique } from '../utils'
import type { ViteDevServer } from '..'
import { isCSSRequest } from '../plugins/css'
import { getAffectedGlobModules } from '../plugins/importMetaGlob'
import { isExplicitImportRequired } from '../plugins/importAnalysis'
import type { ModuleNode } from './moduleGraph'

export const debugHmr = createDebugger('vite:hmr')
Expand Down Expand Up @@ -157,10 +156,6 @@ export function updateModules(
type: `${boundary.type}-update` as const,
timestamp,
path: boundary.url,
explicitImportRequired:
boundary.type === 'js'
? isExplicitImportRequired(boundary.url)
: undefined,
acceptedPath: acceptedVia.url
}))
)
Expand Down
1 change: 0 additions & 1 deletion packages/vite/types/hmrPayload.d.ts
Expand Up @@ -20,7 +20,6 @@ export interface Update {
path: string
acceptedPath: string
timestamp: number
explicitImportRequired: boolean | undefined
}

export interface PrunePayload {
Expand Down

0 comments on commit 1a70a69

Please sign in to comment.