svelte-leafletjs icon indicating copy to clipboard operation
svelte-leafletjs copied to clipboard

how to setup on sveltekit?

Open tibeemts opened this issue 3 years ago • 9 comments

could you guide me how to setup on sveltekit?

tibeemts avatar May 23 '21 08:05 tibeemts

TLDR:

  • use onMount() to dynamically import your component containing your svelte-leaflet related code
  • use {#if browser} to condition the rendering when in browser.
<script lang="ts">
	import { browser } from '$app/env';
	import { onMount } from 'svelte';

	let LeafletContainer;

	onMount(async () => {
		if (browser) {
			LeafletContainer = (await import('$lib/LeafletContainer.svelte')).default;
		}
	});
</script>

{#if browser}
<svelte:component this={LeafletContainer} />
{/if}

0gust1 avatar Dec 06 '21 20:12 0gust1

Instead of importing the library in onMount, I was able to get it working using vite-plugin-iso-import:

import { LeafletMap } from 'svelte-leafletjs?client'

timwis avatar Feb 09 '22 07:02 timwis

@timwis was a bit sparingly with his informations.

After installing vite-plugin-iso-import, you need to do three things:

  1. Configure vite in svelte.config.js to use it as plugin:
    import { isoImport } from 'vite-plugin-iso-import'
    
    const config = {
        kit: {
            vite: {
                plugins: [isoImport()]
            }
        }
    }
    
  2. Configure it in tsconfig.json or jsconfig.json:
    {
        "compilerOptions": {
            "plugins": [{ "name": "vite-plugin-iso-import" }]
        }
    }
    
  3. Use it with {#if browser} in your template and import svelte-leafletjs with ?client
    <div id="map" style="height: 80vh;">
    {#if browser}
        <LeafletMap options={mapOptions}>
            <TileLayer url={tileUrl} options={tileLayerOptions}/>
        </LeafletMap>
    {/if}
    </div>
    
    <script>
    import { browser } from '$app/env'
    import {LeafletMap, TileLayer} from 'svelte-leafletjs?client';
    import 'leaflet/dist/leaflet.css'
    
    const mapOptions = {
        center: [1.364917, 103.822872],
        zoom: 11,
    };
    const tileUrl = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
    const tileLayerOptions = {
        minZoom: 0,
        maxZoom: 20,
        maxNativeZoom: 19,
        attribution: "© OpenStreetMap contributors",
    };
    </script>
    

dritter avatar Jun 29 '22 18:06 dritter

Would someone happen to know how to install with plain Svelte + vitejs?

beebase avatar Jul 06 '22 18:07 beebase

I'm getting a 500 window is not defined.....does anyone know how to solve this,cant make it run

Erildo avatar Jul 17 '22 15:07 Erildo

@Erildo I'm also getting window not defined error, I think Leaflet 1.9.0 will resolve this in coming weeks:

...And maybe the enhanced ESM support added will obviate the need to implement one or more (or all) of the other methods listed on this thread? i.e. onMount((async () => { if (browser) {})) / {#if browser}...{/if} / vite-plugin-iso-import, etc.

baisong avatar Aug 18 '22 14:08 baisong

I have a serverless (sst) project, using svelte for the frontend. It uses vite to deal with the frontend stuff. (see https://sst.dev/examples/how-to-create-a-svelte-app-with-serverless.html for the general setup, as a side note - I really liked the experience so far)

I tried the mimic the approach as proposed by @dritter in the comment above, but to no avail. When I try to import leaflet, it complains about having no default import, when I add the ?client, I get

Failed to resolve import "svelte-leafletjs?client"

Any ideas? (Sorry, I'm rather new to vite/svelte)

Thanks!

nickboucart avatar Nov 17 '22 06:11 nickboucart

I've got this to run, I noticed that this line:

import { browser } from '$app/env';

should be

import { browser } from '$app/environment';

Other than that you need to install https://github.com/bluwy/vite-plugin-iso-import if the ?client import isn't working for you.

fhennig avatar Jan 02 '23 20:01 fhennig

thanks @dritter , this saved my day,

deekshithmr95 avatar Mar 20 '23 06:03 deekshithmr95