Single File Component loader for Vue2 and Vue3. Load .vue files directly from your HTML. No node.js environment, no build step.
JavaScript
1,358
668 commits
updated Sep 27, 2024
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.
import() )<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>
see all examples
https://codepen.io/franckfreiburger/project/editor/AqPyBr
loadModule(path: string, options: Options): Promise<VueComponent>
npm install vue3-sfc-loaderesm version: dist/vue3-sfc-loader.esm.js
umd version: dist/vue3-sfc-loader.js
npm install vue3-sfc-loader (use 'vue3-sfc-loader/dist/vue2-sfc-loader.js')esm version: dist/vue2-sfc-loader.esm.js
umd version: dist/vue2-sfc-loader.js
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:
vue3-sfc-loadernpm install --global yarn)yarn installvue3-sfc-loader.js = Webpack( @vue/compiler-sfc + @babel )
.vue file@vue/compiler-sfc)@babel)@babel/traverse)Many thanks to people that support this project !
Overall
Maintained
Code review
Latest release
Releases this year
141 followers · starred Jul 2021
647 followers · starred Feb 2022
253 followers · starred Apr 2021
5 followers · starred Jun 2022
Single File Component loader for Vue2 and Vue3. Load .vue files directly from your HTML. No node.js environment, no build step.
JavaScript
1,358
668 commits
updated Sep 27, 2024
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.
import() )<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>
see all examples
https://codepen.io/franckfreiburger/project/editor/AqPyBr
loadModule(path: string, options: Options): Promise<VueComponent>
npm install vue3-sfc-loaderesm version: dist/vue3-sfc-loader.esm.js
umd version: dist/vue3-sfc-loader.js
npm install vue3-sfc-loader (use 'vue3-sfc-loader/dist/vue2-sfc-loader.js')esm version: dist/vue2-sfc-loader.esm.js
umd version: dist/vue2-sfc-loader.js
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:
vue3-sfc-loadernpm install --global yarn)yarn installvue3-sfc-loader.js = Webpack( @vue/compiler-sfc + @babel )
.vue file@vue/compiler-sfc)@babel)@babel/traverse)Many thanks to people that support this project !
Overall
Maintained
Code review
Latest release
Releases this year
141 followers · starred Jul 2021
647 followers · starred Feb 2022
253 followers · starred Apr 2021
5 followers · starred Jun 2022