Skip to content

fix: preview support custom-import-map#1669

Merged
hexqi merged 1 commit intoopentiny:developfrom
chilingling:fix/preview-custom-imort-map
Oct 31, 2025
Merged

fix: preview support custom-import-map#1669
hexqi merged 1 commit intoopentiny:developfrom
chilingling:fix/preview-custom-imort-map

Conversation

@chilingling
Copy link
Copy Markdown
Member

@chilingling chilingling commented Oct 11, 2025

English | 简体中文

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • Built its own designer, fully self-validated

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

Background and solution

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features
    • Added support for an optional custom import map from configuration, enabling per-entry overrides of default imports.
    • Placeholder resolution now prioritizes user-defined mappings before falling back to defaults.
    • Improves flexibility and control over module resolution in previews without changing existing workflows.
    • No breaking changes; existing behavior remains intact when no custom map is provided.

@github-actions github-actions bot added the bug Something isn't working label Oct 11, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Oct 11, 2025

Walkthrough

Introduces 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

Cohort / File(s) Summary
Preview import map customization
packages/design-core/src/preview/src/preview/importMap.js
Import getMergeMeta; read optional engine.config.importMap; extend replacePlaceholder(k, ...); adjust getImportMap to pass key; apply custom map overrides before default placeholder replacement; no change to public API.

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
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I nibble on maps with careful delight,
Swapping placeholders under moonlight.
A custom crumb here, a merge there—so neat!
Keys whisper overrides, softly and sweet.
With twitchy nose and imports aligned,
I hop through preview, dependencies refined. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title specifies that this fix enables preview to support custom import maps, directly reflecting the main changes of introducing custom import map handling in the preview module. It is concise and clear without extraneous detail, and the conventional commit prefix ‘fix:’ aligns with established commit message conventions. This makes it easy for collaborators to understand the primary change at a glance.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 inside replacePlaceholder, which is invoked for every import entry. This results in repeated calls to getMergeMeta.

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 replacePlaceholder to 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

📥 Commits

Reviewing files that changed from the base of the PR and between f7dec37 and c11ff77.

📒 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 getMergeMeta is correctly added to support the new custom import map functionality.


47-54: LGTM!

The modification to pass the key k to replacePlaceholder correctly enables per-entry custom import map overrides.

@chilingling chilingling added this to the v2.9.0 milestone Oct 14, 2025
@hexqi hexqi merged commit 2a41f9f into opentiny:develop Oct 31, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants