Rspack Vue Loader
rspack-vue-loader is the official Rspack loader for processing Vue Single-File Components (SFCs), enabling developers to leverage Vue's component authoring format within an Rspack build pipeline. As of version `17.5.0`, it focuses exclusively on Rspack compatibility, explicitly removing dependencies on Webpack. The library is actively maintained with frequent fixes and contributions, demonstrating a stable yet evolving release cadence. Key features include the ability to apply other Rspack loaders to individual SFC blocks (e.g., Sass for <style>, Pug for <template>), support for custom blocks, automatic handling of static assets referenced in templates and styles as module dependencies, simulated scoped CSS, and state-preserving hot-reloading for an efficient development experience. It provides a dedicated and robust solution for integrating Vue.js 3 with Rspack, differentiating itself from its Webpack counterpart, `vue-loader`.
Common errors
-
Error: "rspack-vue-loader" requires "@rspack/core" to be installed. Please install it with the appropriate package manager (e.g., `npm install @rspack/core`).
cause The `@rspack/core` peer dependency is missing.fixInstall `@rspack/core` in your project: `npm install @rspack/core` or `pnpm install @rspack/core` or `yarn add @rspack/core`. -
Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type, currently no loaders are configured to process .vue files.
cause Rspack is attempting to process a `.vue` file but no loader rule is configured for it.fixAdd a rule to your `rspack.config.js` (or `.ts`) for `.vue` files, specifying `loader: 'rspack-vue-loader'`. Also ensure `new VueLoaderPlugin()` is in your `plugins` array. -
ReferenceError: VueLoaderPlugin is not defined
cause The `VueLoaderPlugin` was not imported or instantiated correctly in the Rspack configuration.fixEnsure you have `import { VueLoaderPlugin } from 'rspack-vue-loader';` at the top of your Rspack config file and `new VueLoaderPlugin()` in the `plugins` array. -
TypeError: this.getOptions is not a function (while processing a CSS preprocessor loader like `sass-loader` or `less-loader` within a .vue file)
cause This often occurs when Webpack-specific loader options or context are expected, or when Rspack's native CSS handling interferes with the expected loader chain. This can happen if using an older preprocessor loader version, or mixing Rspack's `type: 'css'` with `vue-loader`'s specific CSS processing requirements.fixEnsure your CSS preprocessor loaders are compatible with Rspack. If encountering this, try configuring `experiments: { css: false }` in your Rspack config and explicitly using `vue-style-loader` and `css-loader` in your rules for `css` or preprocessor files within `.vue` components.
Warnings
- breaking Starting with `v17.4.5`, `rspack-vue-loader` no longer supports Webpack 4. It is designed exclusively for Rspack environments. Attempting to use it with Webpack 4 or older versions will result in build failures.
- breaking The `refSugar` option, used for Vue's experimental ref sugar, was removed in `v16+`. Its functionality has been replaced by `reactivityTransform`.
- gotcha It is critical to specify the loader as `'rspack-vue-loader'` in your Rspack configuration's module rules. The `vue-loader` string is for Webpack and will not work correctly with Rspack.
- gotcha While `enableTsInTemplate` defaults to `true` (allowing TypeScript expressions in templates with `lang="ts"` scripts), using `ts-loader` for TypeScript compilation can sometimes disrupt Hot Module Replacement (HMR) for templates. Rspack's `builtin:swc-loader` or `esbuild-loader` are generally faster and avoid this HMR issue.
- gotcha The `customElement` option, which transforms SFCs into custom elements, defaults to processing files ending with `.ce.vue` (`/\.ce\.vue$/`). Setting it to `true` will process *all* `.vue` files in custom element mode, which might have unintended side effects if not all components are meant to be custom elements.
- gotcha Rspack offers native CSS handling, which can sometimes interact with `rspack-vue-loader`'s internal CSS processing. For advanced CSS setups (e.g., preprocessors like Less/Sass, CSS Modules with custom options, or `vue-style-loader`), you may need to explicitly configure CSS rules and potentially disable Rspack's `experiments.css` option if conflicts arise.
Install
-
npm install rspack-vue-loader -
yarn add rspack-vue-loader -
pnpm add rspack-vue-loader
Imports
- VueLoaderPlugin
const { VueLoaderPlugin } = require('rspack-vue-loader');import { VueLoaderPlugin } from 'rspack-vue-loader'; - Loader configuration string
loader: 'vue-loader'
loader: 'rspack-vue-loader'
- defineConfig
import { defineConfig } from '@rspack/cli';
Quickstart
import { defineConfig } from '@rspack/cli';
import { VueLoaderPlugin } from 'rspack-vue-loader';
import path from 'path';
export default defineConfig({
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
entry: {
main: './src/main.ts',
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.vue', '.json'],
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'rspack-vue-loader',
options: {
// Enable inline matchResource for better rule matching
experimentalInlineMatchResource: true,
},
},
{
test: /\.ts$/,
exclude: [/node_modules/],
loader: 'builtin:swc-loader',
options: {
jsc: {
parser: {
syntax: 'typescript',
},
// Target ESNext for modern browsers
target: 'esnext',
},
},
},
{
test: /\.css$/,
// Rspack natively supports CSS, so basic setup can be simple
// For preprocessors or advanced needs, use specific loaders like 'less-loader' or 'sass-loader'
type: 'css',
},
{
test: /\.(png|jpe?g|gif|webp|svg)$/,
type: 'asset/resource',
generator: {
filename: 'assets/[name][ext]'
}
}
],
},
plugins: [
// Make sure to include the VueLoaderPlugin!
new VueLoaderPlugin(),
],
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
clean: true,
},
devServer: {
port: 8080,
hot: true,
historyApiFallback: true,
},
});