Skip to content

Fix: infer publicPath from webpack output.publicPath for import map#61

Merged
titouanmathis merged 3 commits intodevelopfrom
feature/#60-import-map-infer-public-path
Mar 11, 2026
Merged

Fix: infer publicPath from webpack output.publicPath for import map#61
titouanmathis merged 3 commits intodevelopfrom
feature/#60-import-map-infer-public-path

Conversation

@titouanmathis
Copy link
Contributor

Summary

Fixes #60 — the publicPath preset option is now truly optional. When omitted, it is automatically inferred from webpack's output.publicPath.

Problem

resolveDependencies() was called in the preset handler before the webpack config was built. At that point, output.publicPath didn't exist yet — it was only set later when the consumer's webpack() callback ran. This forced consumers to duplicate the publicPath value in both the preset options and the webpack config.

Solution

  1. resolveDependencies() no longer accepts a publicPath parameter — self-hosted entries always get bare paths (e.g. /static/deps/…)
  2. In the preset's extendWebpack callback (which runs after all consumer config callbacks), the effective publicPath is resolved and used to mutate twigData.importMap — prefixing self-hosted entries. This works because the twig data object is a reference captured by the prototyping preset's closure.
  3. A new shared resolvePublicPath() utility is extracted and used by both the preset and PlaygroundDependenciesPlugin.

Changes

File Change
resolve-public-path.ts New shared utility (+ 11 tests)
resolve-dependencies.ts Removed publicPath param and normalizePublicPath()
playground.ts (preset) Deferred publicPath resolution + import map mutation in extendWebpack
PlaygroundDependenciesPlugin.ts Uses shared resolvePublicPath() instead of private method
Tests Updated to match new signatures; moved publicPath tests to dedicated file

Move publicPath resolution from resolveDependencies() (called before webpack
config is built) into the preset's extendWebpack callback, where the consumer's
output.publicPath is already available.

- Extract resolvePublicPath() into a shared utility
- Mutate twigData.importMap in extendWebpack to prefix self-hosted entries
- Remove publicPath param from resolveDependencies() (always bare paths)
- Update PlaygroundDependenciesPlugin to use shared resolvePublicPath()

Fixes #60

Co-authored-by: Claude <claude@anthropic.com>
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 11, 2026

Deploying studiometa-playground with  Cloudflare Pages  Cloudflare Pages

Latest commit: fe758c0
Status: ✅  Deploy successful!
Preview URL: https://ca9444b7.studiometa-playground.pages.dev
Branch Preview URL: https://feature--60-import-map-infer.studiometa-playground.pages.dev

View logs

@github-actions
Copy link

Export Size

Unchanged

@studiometa/playground

Name Size Diff
createPlayground 1 B -
unzip 1 B -
zip 1 B -

Co-authored-by: Claude <claude@anthropic.com>
@codecov
Copy link

codecov bot commented Mar 11, 2026

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 18.51%. Comparing base (db2aff6) to head (fe758c0).
⚠️ Report is 4 commits behind head on develop.

Files with missing lines Patch % Lines
packages/playground/src/lib/presets/playground.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop      #61      +/-   ##
===========================================
+ Coverage    17.82%   18.51%   +0.68%     
===========================================
  Files           53       54       +1     
  Lines          937      940       +3     
  Branches       165      165              
===========================================
+ Hits           167      174       +7     
+ Misses         757      753       -4     
  Partials        13       13              
Flag Coverage Δ
unittests 18.51% <94.73%> (+0.68%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

The plugin already owns self-hosted dep bundling, _headers emission, and
publicPath resolution. The import map mutation is the same concern and now
lives in the plugin's thisCompilation hook instead of the preset's
extendWebpack callback.

The plugin receives the twigData.importMap reference and prefixes
self-hosted entries during compilation — before HtmlWebpackPlugin
renders templates.

Co-authored-by: Claude <claude@anthropic.com>
@titouanmathis titouanmathis merged commit 852c63c into develop Mar 11, 2026
7 checks passed
@titouanmathis titouanmathis deleted the feature/#60-import-map-infer-public-path branch March 11, 2026 15:04
@titouanmathis titouanmathis mentioned this pull request Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

publicPath: import map doesn't infer publicPath from webpack output.publicPath

1 participant