fix: preview support custom-import-map#1669
Conversation
WalkthroughIntroduces optional per-entry import map overrides by reading engine.config.importMap via getMergeMeta. Enhances placeholder replacement to consult the custom import map before default substitutions. getImportMap now passes the map key to replacePlaceholder. No exported API changes. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Preview as Preview Runtime
participant IM as getImportMap
participant GM as getMergeMeta
participant MR as Meta Registry
participant RP as replacePlaceholder
Preview->>IM: build import map
IM->>GM: request merged meta
GM->>MR: fetch engine metadata
MR-->>GM: meta (may include engine.config.importMap)
GM-->>IM: merged meta
loop for each entry k
IM->>RP: replacePlaceholder(k, value, customImportMap?)
alt custom override exists for k
RP-->>IM: value with custom replacement
else default behavior
RP-->>IM: value with default placeholder replacement
end
end
IM-->>Preview: finalized import map
note over IM,RP: New: key-aware replacement and optional overrides
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/design-core/src/preview/src/preview/importMap.js (1)
31-31: Consider caching the custom import map retrieval.
getMergeMeta('engine.config')is called insidereplacePlaceholder, which is invoked for every import entry. This results in repeated calls togetMergeMeta.Consider retrieving and caching the custom import map once in
getImportMap:export const getImportMap = (scripts = {}) => { + const customImportMap = getMergeMeta('engine.config')?.importMap + importMap.imports = { - ...Object.fromEntries(Object.entries(importMapJSON.imports).map(([k, v]) => [k, replacePlaceholder(v, k)])), + ...Object.fromEntries(Object.entries(importMapJSON.imports).map(([k, v]) => [k, replacePlaceholder(v, k, customImportMap)])), ...scripts } return importMap }Then update
replacePlaceholderto accept it as a parameter:-function replacePlaceholder(v, k) { +function replacePlaceholder(v, k, customImportMap) { const { VITE_CDN_TYPE, VITE_CDN_DOMAIN, VITE_LOCAL_IMPORT_PATH = 'local-cdn-static', BASE_URL, VITE_LOCAL_IMPORT_MAPS } = useEnv() const isLocalBundle = VITE_LOCAL_IMPORT_MAPS === 'true' const versionDelimiter = VITE_CDN_TYPE === 'npmmirror' && !isLocalBundle ? '/' : '@' const fileDelimiter = VITE_CDN_TYPE === 'npmmirror' && !isLocalBundle ? '/files' : '' const cdnDomain = isLocalBundle ? BASE_URL + VITE_LOCAL_IMPORT_PATH : VITE_CDN_DOMAIN - const customImportMap = getMergeMeta('engine.config')?.importMap if (customImportMap?.imports?.[k]) { return customImportMap.imports[k] .replace('${VITE_CDN_DOMAIN}', cdnDomain) .replace('${versionDelimiter}', versionDelimiter) .replace('${fileDelimiter}', fileDelimiter) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/design-core/src/preview/src/preview/importMap.js(3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/design-core/src/preview/src/preview/importMap.js (2)
packages/build/vite-config/src/localCdnFile/copyPreviewImportMap.js (1)
importMap(17-17)packages/design-core/src/preview/src/preview/srcFiles.js (2)
versionDelimiter(30-30)fileDelimiter(31-31)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: push-check
🔇 Additional comments (2)
packages/design-core/src/preview/src/preview/importMap.js (2)
13-13: LGTM!The import of
getMergeMetais correctly added to support the new custom import map functionality.
47-54: LGTM!The modification to pass the key
ktoreplacePlaceholdercorrectly enables per-entry custom import map overrides.
English | 简体中文
PR
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
Background and solution
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit