Home

Awesome

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

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 <!--update-min-br-size-->bundle minified+brotli size<!--/update-min-br-size--> <!--update-min-gz-size-->bundle minified+gzip size<!--/update-min-gz-size--> <!--update-min-size-->bundle minified size<!--/update-min-size-->

browser support

<!-- [![Vue3 compiler-sfc dependency version](https://img.shields.io/github/package-json/dependency-version/FranckFreiburger/vue3-sfc-loader/dev/@vue/compiler-sfc?label=embeds%20Vue3%20%40vue%2Fcompiler-sfc)](https://github.com/vuejs/vue-next/tree/master/packages/compiler-sfc) [![Vue2 vue-template-compiler dependency version](https://img.shields.io/github/package-json/dependency-version/FranckFreiburger/vue3-sfc-loader/dev/vue-template-compiler?label=embeds%20Vue2%20vue-template-compiler)](https://github.com/vuejs/vue-next/tree/master/packages/compiler-sfc) --> <br>

Vue3

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

<br>

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 here: https://stackoverflow.com/questions/ask?tags=vue3-sfc-loader (see [previous questions](https://stackoverflow.com/questions/tagged/vue3-sfc-loader)) -->

:speech_balloon: ask in Discussions tab

Tweet

Financial contributors

Many thanks to people that support this project !

<!--- const Fs = require('fs'); const Path = require('path'); const { blockList, replaceBlock } = require('./evalHtmlCommentsTools.js'); function fileSize(filename) { try { return Fs.statSync(Path.join(__dirname, filename)).size; } catch { return -1; } } const version = process.argv[3]; let result = ctx.wholeContent; result = replaceBlock(result, 'update-min-br-size', content => content.replace(/-(.*?)-/, '-' + Math.floor(fileSize('../dist/vue3-sfc-loader.js.br')/1024) + 'kB' + '-')); result = replaceBlock(result, 'update-min-gz-size', content => content.replace(/-(.*?)-/, '-' + Math.floor(fileSize('../dist/vue3-sfc-loader.js.gz')/1024) + 'kB' + '-')); result = replaceBlock(result, 'update-min-size', content => content.replace(/-(.*?)-/, '-' + Math.floor(fileSize('../dist/vue3-sfc-loader.js')/1024) + 'kB' + '-')); result; --->