Skip to content

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

License

Notifications You must be signed in to change notification settings

RobenQ/vue3-sfc-loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

vue3-sfc-loader

Vue3 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

  • Only requires Vue3 runtime
  • Focuses on component compilation. Network, styles injection and cache are up to you (see example below)
  • Embedded ES6 modules support ( including import() )
  • SFC Custom Blocks support
  • Support custom CSS, HTML and Script language, see pug and stylus examples
  • Properly reports template, style or script errors through the log callback
  • You can build your own version and easily customize browsers you need to support

Example

<html> <body> <div id="app"></div> <script src="https://unpkg.com/vue@next"></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(url+' '+res.statusText), { res }); return await 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 examples

Try It Online πŸ§ͺ

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

Public API documentation πŸ“–

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

dist/ πŸ“¦

latest bundle version bundle minified size bundle minified+zip size bundle minified+bz2 size Snyk Vulnerabilities for vue3-sfc-loader browser support compiler-sfc dependency version

latest minified version at :

Build your own version πŸ› οΈ

webpack --config ./build/webpack.config.js --mode=production --env targetsBrowsers="> 1%, last 2 versions, Firefox ESR, not dead, not ie 11"

see package.json "build" script
see browserslist queries

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

Previous version (for vue2)

see http-vue-loader πŸ’€

Tweet

About

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

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 55.9%
  • TypeScript 38.3%
  • HTML 5.8%