Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add isDynamicEntry to metadata #14

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ export interface OptimizedDepInfo {
* data used both to define if interop is needed and when pre-bundling
*/
exportsData?: Promise<ExportsData>
/**
* Mark the chunk from dynamic import
*/
isDynamicEntry?: boolean
}

export interface DepOptimizationMetadata {
Expand Down Expand Up @@ -616,7 +620,7 @@ export function runOptimizeDeps(
)

for (const chunk of result.output) {
if (chunk.type === 'chunk' && chunk.isEntry) {
if (chunk.type === 'chunk') {
if (chunk.isEntry) {
const { exportsData, file, id, ...info } = Object.values(
depsInfo,
Expand Down Expand Up @@ -645,9 +649,7 @@ export function runOptimizeDeps(
),
})
} else {
const id = path
.relative(processingCacheDirOutputPath, chunk.fileName)
.replace(jsExtensionRE, '')
const id = chunk.fileName.replace(jsExtensionRE, '')
const file = getOptimizedDepPath(id, resolvedConfig, ssr)
if (
!findOptimizedDepInfoInRecord(
Expand All @@ -660,6 +662,7 @@ export function runOptimizeDeps(
file,
needsInterop: false,
browserHash: metadata.browserHash,
isDynamicEntry: chunk.isDynamicEntry,
})
}
}
Expand Down Expand Up @@ -1062,7 +1065,10 @@ function stringifyDepsOptimizerMetadata(
),
),
chunks: Object.fromEntries(
Object.values(chunks).map(({ id, file }) => [id, { file }]),
Object.values(chunks).map(({ id, file, isDynamicEntry }) => [
id,
{ file, isDynamicEntry },
]),
),
},
(key: string, value: string) => {
Expand Down
13 changes: 10 additions & 3 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ import { getDepOptimizationConfig } from '../config'
import type { ResolvedConfig } from '../config'
import type { Plugin } from '../plugin'
import { shouldExternalizeForSSR } from '../ssr/ssrExternal'
import { getDepsOptimizer, optimizedDepNeedsInterop } from '../optimizer'
import {
getDepsOptimizer,
optimizedDepInfoFromFile,
optimizedDepNeedsInterop,
} from '../optimizer'
import { checkPublicFile, urlRE } from './asset'
import { throwOutdatedRequest } from './optimizedDeps'
import { isCSSRequest, isDirectCSSRequest } from './css'
Expand All @@ -68,7 +72,6 @@ export const canSkipImportAnalysis = (id: string): boolean =>
skipRE.test(id) || isDirectCSSRequest(id)

const optimizedDepChunkRE = /\/chunk-[A-Z\d]{8}\.js/
const optimizedDepDynamicRE = /-[A-Z\d]{8}\.js/

const hasImportInQueryParamsRE = /[?&]import=?\b/

Expand Down Expand Up @@ -537,6 +540,10 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// page reload. We could return a 404 in that case but it is safe to return the request
const file = cleanUrl(resolvedId) // Remove ?v={hash}

const depInfo = optimizedDepInfoFromFile(
depsOptimizer.metadata,
file,
)
const needsInterop = await optimizedDepNeedsInterop(
depsOptimizer.metadata,
file,
Expand All @@ -548,7 +555,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// Non-entry dynamic imports from dependencies will reach here as there isn't
// optimize info for them, but they don't need es interop. If the request isn't
// a dynamic import, then it is an internal Vite error
if (!file.match(optimizedDepDynamicRE)) {
if (depInfo?.isDynamicEntry) {
config.logger.error(
colors.red(
`Vite Error, ${url} optimized info should be defined`,
Expand Down
Loading