GithubHelp home page GithubHelp logo

costlife / vue3-sfc-loader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from franckfreiburger/vue3-sfc-loader

0.0 0.0 0.0 2.9 MB

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

License: MIT License

JavaScript 49.08% TypeScript 43.92% HTML 7.00%

vue3-sfc-loader's Introduction

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() )
  • 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 ๐Ÿ“–

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

vue3-sfc-loader's People

Contributors

dependabot[bot] avatar franckfreiburger avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.