Skip to content

Commit

Permalink
feat: improvement rawUrl logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Sep 24, 2023
1 parent 8509b58 commit 1148b7d
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -432,47 +432,50 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// so use imports[index].n to get the unescaped string
let specifier = importSpecifier.n

const rawUrl = source.slice(start, end)

// check import.meta usage
if (rawUrl === 'import.meta') {
const prop = source.slice(end, end + 4)
if (prop === '.hot') {
hasHMR = true
const endHot = end + 4 + (source[end + 4] === '?' ? 1 : 0)
if (source.slice(endHot, endHot + 7) === '.accept') {
// further analyze accepted modules
if (source.slice(endHot, endHot + 14) === '.acceptExports') {
const importAcceptedExports = (orderedAcceptedExports[index] =
new Set<string>())
lexAcceptedHmrExports(
source,
source.indexOf('(', endHot + 14) + 1,
importAcceptedExports,
)
isPartiallySelfAccepting = true
} else {
const importAcceptedUrls = (orderedAcceptedUrls[index] =
new Set<UrlPosition>())
if (
lexAcceptedHmrDeps(
if (!specifier) {
const rawUrl = source.slice(start, end)

// check import.meta usage
if (rawUrl === 'import.meta') {
const prop = source.slice(end, end + 4)
if (prop === '.hot') {
hasHMR = true
const endHot = end + 4 + (source[end + 4] === '?' ? 1 : 0)
if (source.slice(endHot, endHot + 7) === '.accept') {
// further analyze accepted modules
if (source.slice(endHot, endHot + 14) === '.acceptExports') {
const importAcceptedExports = (orderedAcceptedExports[
index
] = new Set<string>())
lexAcceptedHmrExports(
source,
source.indexOf('(', endHot + 7) + 1,
importAcceptedUrls,
source.indexOf('(', endHot + 14) + 1,
importAcceptedExports,
)
) {
isSelfAccepting = true
isPartiallySelfAccepting = true
} else {
const importAcceptedUrls = (orderedAcceptedUrls[index] =
new Set<UrlPosition>())
if (
lexAcceptedHmrDeps(
source,
source.indexOf('(', endHot + 7) + 1,
importAcceptedUrls,
)
) {
isSelfAccepting = true
}
}
}
} else if (prop === '.env') {
hasEnv = true
}
} else if (prop === '.env') {
hasEnv = true
return
} else if (templateLiteralRE.test(rawUrl)) {
// Only static template literal will into this branch.
// It has variables will processed in importMetaGlob.ts
specifier = rawUrl.replace(templateLiteralRE, '$1')
}
return
} else if (templateLiteralRE.test(rawUrl)) {
// Only static template literal will into this branch.
// It has variables will processed in importMetaGlob.ts
specifier = rawUrl.replace(templateLiteralRE, '$1')
}

const isDynamicImport = dynamicIndex > -1
Expand Down

0 comments on commit 1148b7d

Please sign in to comment.