A lightweight library that enables loading Vue 3 Single File Components (.vue files) directly in the browser at runtime without requiring a build step. Supports TypeScript and JSX transformations for dynamic component loading.
npm install @skaldapp/loader-sfcVue3 Single File Component (SFC) loader. Load .vue files directly from your browser without any build step.
- 🚀 Load Vue 3 SFCs directly in the browser at runtime
- ⚡ No build step required - perfect for dynamic component loading
- 🛠️ Supports TypeScript and JSX
- 📦 Lightweight and efficient
- 🔧 Compatible with Vue's defineAsyncComponent
Install @skaldapp/loader-sfc with npm:
``bash`
npm install @skaldapp/loader-sfc
Or with yarn:
`bash`
yarn add @skaldapp/loader-sfc
Or with pnpm:
`bash`
pnpm add @skaldapp/loader-sfc
To load .vue files dynamically at runtime just use the loadModule function:
`vue
`
You can pass configuration options to customize the compilation process:
`javascript
import { defineAsyncComponent } from "vue";
import loadModule from "@skaldapp/loader-sfc";
const MyComponent = defineAsyncComponent(async () =>
loadModule(await (await fetch("./components/MyComponent.vue")).text(), {
scriptOptions: {
templateOptions: {
compilerOptions: {
expressionPlugins: ["typescript"], // Additional Babel plugins
},
},
},
parseOptions: {
// Options for the SFC parser
},
styleOptions: {
// Options for style compilation
},
}),
);
`
Loads and compiles a Vue SFC file at runtime.
#### Parameters
- filename (string): Path to the .vue file to loadoptions
- (LoadModuleOptions, optional): Configuration options for compilation
#### Options
- scriptOptions: Options for script compilationtemplateOptions
- : Options for template compilationcompilerOptions
- : Vue template compiler optionsexpressionPlugins
- : Additional Babel parser plugins (e.g., 'typescript', 'jsx')parseOptions
- : Options for SFC parsingstyleOptions
- : Options for style compilation (e.g., scss, less)
- Vue 3.x
- Modern browser that supports ES modules and dynamic imports
- The first load of a component will be slower as it needs to fetch and compile the SFC
- Subsequent loads of the same component will be faster due to browser caching
- Styles are injected into the document once per component and cached by a hash of the filename
- Consider using this loader for truly dynamic components, not for all components in your application
To build the project locally:
`bash`
npm run build
To lint the code:
`bash`
npm run lint
A simple example of using @skaldapp/loader-sfc` in a template Vue 3 + TypeScript + Vite application for dynamic loading and compilation of an SFC module during application runtime in the browser can be found in the repository: loader-sfc-example
This project is licensed under the AGPL-3.0-only License.