0.9.5 • Published 3 months ago

vue3-sfc-loader v0.9.5

Weekly downloads
136
License
MIT
Repository
github
Last release
3 months ago

vue3-sfc-loader

API | FAQ | Examples | dist | Roadmap

Vue3/Vue2 Single File Component loader.
Load .vue files dynamically at runtime from your html/js. No node.js environment, no (webpack) build step needed.

Key Features

  • Supports Vue 3 and Vue 2 (see dist/)
  • Only requires Vue runtime-only build
  • esm and umd bundles available (example)
  • Embedded ES6 modules support ( including import() )
  • TypeScript support, JSX support
  • Custom CSS, HTML and Script language Support, see pug and stylus examples
  • SFC Custom Blocks support
  • Properly reports template, style or script errors through the log callback
  • Focuses on component compilation. Network, styles injection and cache are up to you (see example below)
  • Easily build your own version and customize browsers you need to support

Example

<html>
<body>
  <div id="app"></div>
  <script src="https://unpkg.com/vue@latest"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue3-sfc-loader/dist/vue3-sfc-loader.js"></script>
  <script>

    const options = {
      moduleCache: {
        vue: Vue
      },
      async getFile(url) {
        
        const res = await fetch(url);
        if ( !res.ok )
          throw Object.assign(new Error(res.statusText + ' ' + url), { res });
        return {
          getContentData: asBinary => asBinary ? res.arrayBuffer() : res.text(),
        }
      },
      addStyle(textContent) {

        const style = Object.assign(document.createElement('style'), { textContent });
        const ref = document.head.getElementsByTagName('style')[0] || null;
        document.head.insertBefore(style, ref);
      },
    }

    const { loadModule } = window['vue3-sfc-loader'];

    const app = Vue.createApp({
      components: {
        'my-component': Vue.defineAsyncComponent( () => loadModule('./myComponent.vue', options) )
      },
      template: '<my-component></my-component>'
    });

    app.mount('#app');

  </script>
</body>
</html>

More Examples

see all examples

Try It Online

https://codepen.io/franckfreiburger/project/editor/AqPyBr

Public API documentation

loadModule(path: string, options: Options): Promise<VueComponent>

dist/

latest bundle version bundle minified+brotli size bundle minified+gzip size bundle minified size

browser support

npm.io

Vue3

Build your own version

Example: enable IE11 support
npx webpack --config ./build/webpack.config.js --mode=production --env targetsBrowsers="> 1%, last 8 versions, Firefox ESR, not dead, IE 11" check

see package.json "build" script
see browserslist queries

preliminary steps:
1. clone vue3-sfc-loader 1. (install yarn: npm install --global yarn) 1. run yarn install

How It Works

vue3-sfc-loader.js = Webpack( @vue/compiler-sfc + @babel )

more details

  1. load the .vue file
  2. parse and compile template, script and style sections (@vue/compiler-sfc)
  3. transpile script and compiled template to es5 (@babel)
  4. parse scripts for dependencies (@babel/traverse)
  5. recursively resolve dependencies
  6. merge all and return the component

Any Questions

:speech_balloon: ask in Discussions tab

Tweet

Financial contributors

Many thanks to people that support this project !

npm.io

0.9.5

3 months ago

0.9.4

3 months ago

0.9.3

3 months ago

0.9.2

3 months ago

0.9.1

4 months ago

0.9.0

4 months ago

0.8.4

3 years ago

0.8.3

3 years ago

0.8.1

3 years ago

0.8.2

3 years ago

0.8.0

3 years ago

0.7.3

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.5.0

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.1

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.22

3 years ago

0.2.21

3 years ago

0.2.20

3 years ago

0.2.19

3 years ago

0.2.18

3 years ago

0.2.17

3 years ago

0.2.16

3 years ago

0.2.15

3 years ago

0.2.14

3 years ago

0.2.13

3 years ago

0.2.12

3 years ago

0.2.11

3 years ago

0.2.10

3 years ago

0.2.7

3 years ago

0.2.8

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago