npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-browser-import

v0.0.4

Published

[![NPM Package](https://img.shields.io/npm/v/svelte-browser-import.svg)](https://www.npmjs.com/package/svelte-browser-import) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/repalash/svelte-browser-import/blob/mast

Downloads

29

Readme

Svelte Browser Import

NPM Package License: MIT

This is a simple library that includes the svelte compiler and provides functions to import and render a Svelte App/Component (.svelte files) directly inside a browser without a build step. (Useful for development and testing)

Extracted out the Bundler from the Svelte REPL.

Working demos: https://repalash.com/svelte-browser-import/index.html

Usage

Use the importSvelte and render functions to render a svelte app inside the body of the current page. This uses eval to evaluate the code, so it is not recommended to use this in production.

importSvelte

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Svelte Browser Import</title>
    <script type="module">
        import { importSvelte } from 'https://unpkg.com/svelte-browser-import/dist/svelte-browser-import.es.js';
        const App = await importSvelte('./HelloWorld.svelte')
        const app = new App({
            target: document.getElementById('app'),
        })
        
        // app.$destroy() // destroy the app
        
        // or just 
        // renderSvelte('./HelloWorld.svelte')
    </script>
</head>
<body>
<div id="app"></div>
</body>
</html>

importSvelteUrls

To import multiple files which import each other, use the importSvelteUrls or importSvelteBundle function to import the files and then use the render function to render the app.

This needs a file named App.svelte which is the entry point, if not provided, the only file in the list will be renamed to App.svelte, if multiple files, then an App.svelte is created that renders all the files as components in the order they are provided.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Svelte Browser Import</title>
    <script type="module">
        import { importSvelteUrls } from 'https://unpkg.com/svelte-browser-import/dist/svelte-browser-import.es.js';
        const App = await importSvelteUrls([
            './App.svelte',
            './Nested.svelte',
        ])
        const app = new App({
            target: document.getElementById('app'),
        })
        
        // or just 
        // renderSvelteUrls([
        //     './App.svelte',
        //     './Nested.svelte',
        // ], {
        // target: document.getElementById('app'),
        // })
    </script>
</head>
<body>
<div id="app"></div>
</body>
</html>

importSvelteBundle

Use the importSvelteBundle function to get access to all the parameters. Check the file src/main.ts for more details.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Svelte Browser Import</title>
    <script type="module">
        import { importSvelteBundle } from 'https://unpkg.com/svelte-browser-import/dist/svelte-browser-import.es.js';
        const res = await importSvelteBundle({
            urls: [
                './App.svelte',
                './Nested.svelte',
            ],
            // files: [ // only one of urls or files can be provided
            //     {
            //         name: 'App',
            //         type: 'svelte',
            //         content: '...',
            //         modified: true,
            //     }
            // ],
            packagesUrl: 'https://unpkg.com',
            svelteUrl: 'https://unpkg.com/svelte',
            injectedJS: '',
            injectedCSS: '',
            onstatus: (val) => {
                console.log(val)
            },
        })
        console.log(res)
        const App = new res.render()
        const app = new App({
            target: document.getElementById('app'),
        })
    </script>
</head>
<body>
<div id="app"></div>
</body>
</html>

Non-module usage

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Svelte Browser Import</title>
    <script src="https://unpkg.com/svelte-browser-import"></script>
    <script>
        const { importSvelte } = window["svelte-browser-import"]

        importSvelte('./HelloWorld.svelte').then(App=> {
            const app = new App({
                target: document.getElementById('app'),
                props: {
                    name: 'custom app'
                }
            })
            // to destroy the app
            // app.$destroy()
        })
    </script>
</head>
<body>
    <div id="app"></div>
</body>
</html>