Replies: 4 comments
-
|
// vite.config.ts
import { defineConfig } from "vite";
export default defineConfig({
plugins: [
{
name: "externalize-leaflet",
enforce: "pre",
resolveId(source) {
if (source === "leaflet") {
return { id: "leaflet", external: true };
}
},
},
],
optimizeDeps: {
exclude: ["leaflet"],
},
});With this in place Vite leaves the bare If you ever want to externalize a wider set, |
Beta Was this translation helpful? Give feedback.
-
|
Building on the plugin approach above — here's how to make the Leaflet version dynamic based on a query parameter, which is the part of your original question not yet covered. The plugin above hard-codes <!-- index.html -->
<script>
const version = new URLSearchParams(location.search).get('leaflet') || '2';
const map = document.createElement('script');
map.type = 'importmap';
map.textContent = JSON.stringify({
imports: {
leaflet: `https://esm.sh/leaflet@${version}/dist/leaflet-src.esm.js`
}
});
document.currentScript.after(map);
</script>// vite.config.ts
export default defineConfig({
plugins: [
{
name: 'externalize-leaflet',
enforce: 'pre',
resolveId(source) {
if (source === 'leaflet') return { id: 'leaflet', external: true }
},
},
],
optimizeDeps: { exclude: ['leaflet'] },
})Then:
The import map must be injected before the module graph is evaluated, which is why the inline script inserts it synchronously. Works in both dev (Vite leaves the bare specifier alone) and production build (external in rollup options). |
Beta Was this translation helpful? Give feedback.
-
|
The two approaches in the comments are both valid — here's how to combine them for the cleanest solution for your use case. Step 1: Externalize
|
Beta Was this translation helpful? Give feedback.
-
|
Yes, you can keep Solution: Custom Plugin + optimizeDeps// vite.config.ts
import { defineConfig } from 'vite'
export default defineConfig({
// 1. Prevent Vite's pre-bundler from processing leaflet
optimizeDeps: {
exclude: ['leaflet'],
},
// 2. For production: keep leaflet external in the rollup bundle
build: {
rollupOptions: {
external: ['leaflet'],
},
},
plugins: [
{
name: 'externalize-leaflet',
enforce: 'pre',
// 3. In the dev server: intercept the import and mark it external
resolveId(source) {
if (source === 'leaflet') {
return { id: 'leaflet', external: true }
}
},
},
],
})How It Works
Your Test Page with Import MapWith this setup, your test page can use an import map to swap between Leaflet versions: <!DOCTYPE html>
<html>
<head>
<!-- Swap this URL to test Leaflet v1 vs v2 -->
<script type="importmap">
{
"imports": {
"leaflet": "https://esm.sh/leaflet@1.9.4"
}
}
</script>
</head>
<body>
<div id="map" style="height: 400px"></div>
<script type="module" src="/src/test-page.ts"></script>
</body>
</html>For Leaflet v2: change the URL to Important: Import Map Browser SupportImport maps are supported in all modern browsers. If you need to support older browsers in your test environment, add the es-module-shims polyfill: <script async src="https://ga.jspm.io/npm:es-module-shims@1.8.0/dist/es-module-shims.js"></script>(This must appear before the Alternative: Query Param Driven Dynamic ImportIf you don't want to rely on import maps, you can make the test page dynamically import based on a query param: // test-page.ts
const version = new URLSearchParams(location.search).get('v') ?? '1'
const leafletUrl = version === '2'
? 'https://esm.sh/leaflet@2.0.0'
: 'https://esm.sh/leaflet@1.9.4'
const L = await import(/* @vite-ignore */ leafletUrl)
// use L...The |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am writing a Leaflet plugin. My plugin imports Leaflet as
import * as L from "leaflet". My library includes a test HTML page that embeds a Leaflet map with my plugin.I need to test my plugin with Leaflet 1 and Leaflet 2. I would like to duplicate as little code as possible to achieve this.
My idea was that I could create a dynamic import map in my test page that resolves
"leaflet"to either Leaflet 1 or Leaflet 2 from esm.sh, depending on a query parameter that the test page is opened with. However, this would require Vite to leave the"leaflet"in all imports of Leaflet untouched. Throughbuild.rollupOptions.externalI can configure this for the production bundle, but not for the dev server.Is there a way to mark dependencies as external for the dev server? Or are there any alternative approaches to achieve this?
Beta Was this translation helpful? Give feedback.
All reactions