Vue Markdown Loader
vue-markdown-loader is a Webpack loader designed to convert Markdown files into Vue 2 components, utilizing the popular `markdown-it` parsing library. The current stable version is 2.5.0. The package has seen limited updates and its README explicitly advises developers to consider "other better choices," suggesting it is in maintenance mode or effectively deprecated for new projects. It differentiates itself by enabling direct embedding of Vue `<script>` and `<style>` blocks within Markdown content, offering features like hot reloading, code highlighting, and flexible `markdown-it` configuration options. Its typical release cadence is infrequent, with the last major update (v2.0.0) focusing on automatic script/style extraction. It's primarily used for rendering dynamic documentation or content pages within Vue applications built with Webpack, requiring peer dependencies like `vue-loader` and `vue-template-compiler` for its operation.
Common errors
-
Module build failed (from ./node_modules/vue-markdown-loader/index.js): Error: Cannot find module 'vue-loader/lib/plugin'
cause `VueLoaderPlugin` is missing from your webpack plugins array, or `vue-loader` version is incompatible with your setup.fixAdd `const VueLoaderPlugin = require('vue-loader/lib/plugin');` to your webpack config and instantiate `new VueLoaderPlugin()` in the `plugins` array. Ensure `vue-loader` and `vue-template-compiler` versions are compatible. -
Markdown content is rendered as plain HTML/text without Vue component functionality or styles/scripts are not applied.
cause The loader is not correctly configured to pass compiled content to `vue-loader`, often due to missing `raw: true` option or incorrect loader path when using `vue-loader` v15+.fixVerify that your webpack rule for `.md` files uses `vue-markdown-loader/lib/markdown-compiler` and includes `options: { raw: true }`. Also, ensure `vue-loader` is applied after `vue-markdown-loader` in the chain. -
Embedded `<script>` or `<style>` tags within markdown are not correctly processed or cause syntax errors.
cause The automatic extraction feature (introduced in v2.0.0) might interfere with your embedded code, or the content is not correctly tokenized.fixIf you want to prevent automatic extraction and handle scripts/styles manually (e.g., through `vue-loader`), set the `preventExtract: true` option in `vue-markdown-loader`'s options.
Warnings
- deprecated The package's own README explicitly states 'You have other better choices' and lists alternative loaders. This indicates the project is not actively recommended for new development and might not receive future updates or active support.
- breaking Since `v2.0.0`, the loader automatically extracts script and style tags from HTML content embedded within Markdown. This might alter how your embedded Vue code or styles are processed.
- gotcha When used with `vue-loader` v15+, it is crucial to use the specific `vue-markdown-loader/lib/markdown-compiler` path and include the `raw: true` option to ensure the Markdown content is correctly processed and passed to `vue-loader`.
Install
-
npm install vue-markdown-loader -
yarn add vue-markdown-loader -
pnpm add vue-markdown-loader
Imports
- vue-markdown-loader (basic)
import { vueMarkdownLoader } from 'vue-markdown-loader'loader: 'vue-markdown-loader'
- vue-markdown-loader/lib/markdown-compiler
loader: 'vue-markdown-loader'
loader: 'vue-markdown-loader/lib/markdown-compiler'
- VueLoaderPlugin
import { VueLoaderPlugin } from 'vue-loader'const VueLoaderPlugin = require('vue-loader/lib/plugin');
Quickstart
const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
module.exports = {
mode: 'development',
entry: './src/main.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.md$/,
use: [
{
loader: 'vue-loader'
},
{
loader: 'vue-markdown-loader/lib/markdown-compiler',
options: {
raw: true, // Crucial for passing raw HTML to vue-loader
preventExtract: false, // Set to true to disable automatic script/style extraction
wrapper: 'article', // Customize the root wrapper tag
// Example markdown-it plugin usage
use: [
[require('markdown-it-anchor'), { permalink: true, permalinkBefore: true, permalinkSymbol: '#' }]
]
}
}
]
}
]
},
plugins: [
new VueLoaderPlugin()
],
resolve: {
extensions: ['.js', '.vue', '.json', '.md'],
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
}
};