vue3-sfc-loader
TypeScript icon, indicating that this package has built-in type declarations

0.9.5 • Public • Published

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


Vue3

esm version: dist/vue3-sfc-loader.esm.js
umd version: dist/vue3-sfc-loader.js


Vue2

esm version: dist/vue2-sfc-loader.esm.js
umd version: dist/vue2-sfc-loader.js

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
  2. (install yarn: npm install --global yarn)
  3. 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

💬 ask in Discussions tab

Tweet

Financial contributors

Many thanks to people that support this project !

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.9.51,077latest

Version History

VersionDownloads (Last 7 Days)Published
0.9.51,077
0.9.42
0.9.327
0.9.20
0.9.127
0.9.07
0.8.42,035
0.8.31
0.8.25
0.8.11
0.8.021
0.7.364
0.7.20
0.7.11
0.7.00
0.6.10
0.6.00
0.5.10
0.5.00
0.4.50
0.4.40
0.4.30
0.4.20
0.4.10
0.3.10
0.3.00
0.2.221
0.2.210
0.2.200
0.2.190
0.2.180
0.2.170
0.2.161
0.2.150
0.2.140
0.2.130
0.2.120
0.2.111
0.2.100
0.2.80
0.2.70
0.2.00
0.1.00

Package Sidebar

Install

npm i vue3-sfc-loader

Weekly Downloads

3,271

Version

0.9.5

License

MIT

Unpacked Size

34 MB

Total Files

47

Last publish

Collaborators

  • franck.freiburger