Top Related Projects
Quick Overview
Vue Loader is a webpack loader for Vue.js components. It allows you to write single-file components (SFCs) in Vue.js, combining template, script, and style in one file. Vue Loader transforms these SFCs into plain JavaScript modules that can be processed by webpack.
Pros
- Enables writing Vue.js components in a more modular and maintainable way
- Supports hot-reloading for a smoother development experience
- Provides pre-processors and custom block support for enhanced flexibility
- Offers automatic style scoping for better CSS management
Cons
- Requires additional setup and configuration compared to using Vue.js without a build step
- Learning curve for developers new to webpack and build tools
- Potential build performance impact for large projects with many components
- Debugging can be more challenging due to the compilation process
Code Examples
- Basic Vue Single-File Component:
<template>
<div>{{ message }}</div>
</template>
<script>
export default {
data() {
return {
message: 'Hello, Vue!'
}
}
}
</script>
<style scoped>
div {
color: blue;
}
</style>
- Using custom blocks in a Vue SFC:
<template>
<div>{{ $t('greeting') }}</div>
</template>
<script>
export default {
// Component logic
}
</script>
<i18n>
{
"en": {
"greeting": "Hello!"
},
"fr": {
"greeting": "Bonjour!"
}
}
</i18n>
- Importing a Vue component:
import MyComponent from './MyComponent.vue'
export default {
components: {
MyComponent
}
// Rest of the component logic
}
Getting Started
To use Vue Loader in your project:
- Install the necessary dependencies:
npm install -D vue-loader vue-template-compiler webpack webpack-cli
- Configure webpack (webpack.config.js):
const { VueLoaderPlugin } = require('vue-loader')
module.exports = {
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
}
]
},
plugins: [
new VueLoaderPlugin()
]
}
- Now you can import and use .vue files in your project.
Competitor Comparisons
Next generation frontend tooling. It's fast!
Pros of Vite
- Faster development server startup and hot module replacement (HMR)
- Built-in support for TypeScript, JSX, and CSS pre-processors
- No-bundle dev server for improved performance during development
Cons of Vite
- Relatively newer project with a smaller ecosystem compared to Vue Loader
- May require additional configuration for complex build scenarios
- Limited compatibility with older browsers without polyfills
Code Comparison
Vue Loader:
// vue.config.js
module.exports = {
chainWebpack: config => {
config.module
.rule('vue')
.use('vue-loader')
.loader('vue-loader')
}
}
Vite:
// vite.config.js
import vue from '@vitejs/plugin-vue'
export default {
plugins: [vue()]
}
Summary
Vite offers faster development experience and modern features out of the box, while Vue Loader provides a more established ecosystem within the Vue.js community. Vite's configuration is generally simpler, but it may have limitations for complex projects or older browser support. Vue Loader, being more mature, offers greater flexibility but may require more setup. The choice between the two depends on project requirements, development priorities, and target environments.
🛠️ webpack-based tooling for Vue.js Development
Pros of vue-cli
- Provides a complete project scaffolding and build setup
- Offers a graphical user interface for project management
- Includes a plugin system for extending functionality
Cons of vue-cli
- Larger overhead for simple projects
- May include unnecessary dependencies for basic applications
- Steeper learning curve for beginners
Code Comparison
vue-cli (project creation):
vue create my-project
cd my-project
npm run serve
vue-loader (webpack configuration):
module.exports = {
module: {
rules: [
{ test: /\.vue$/, use: 'vue-loader' }
]
},
plugins: [
new VueLoaderPlugin()
]
}
vue-cli is a comprehensive tooling system for Vue.js development, providing project scaffolding, build setup, and management features. It offers a more complete solution for Vue.js projects, including a plugin system and graphical user interface.
vue-loader, on the other hand, is a loader for webpack that allows you to author Vue components in a format called Single-File Components (SFCs). It's more focused on the compilation and processing of .vue files.
While vue-cli includes vue-loader in its setup, it provides a higher-level abstraction and additional features. vue-loader can be used independently in custom webpack configurations for more granular control over the build process.
Choose vue-cli for a full-featured development environment, especially for larger projects. Opt for vue-loader if you need more control over your webpack configuration or are working on smaller, more focused applications.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
vue-loader
webpack loader for Vue Single-File Components
v17.2.1+ Only Options
experimentalInlineMatchResource: boolean
: enable Inline matchResource for rule matching for vue-loader.
v16+ Only Options
-
reactivityTransform: boolean
: enable Vue Reactivity Transform (SFCs only). -
refSugar: boolean
: removed. usereactivityTransform
instead. -
customElement: boolean | RegExp
: enable custom elements mode. An SFC loaded in custom elements mode inlines its<style>
tags as strings under the component'sstyles
option. When used withdefineCustomElement
from Vue core, the styles will be injected into the custom element's shadow root.- Default is
/\.ce\.vue$/
- Setting to
true
will process all.vue
files in custom element mode.
- Default is
-
enableTsInTemplate: boolean
(16.8+): allow TS expressions in templates when<script>
haslang="ts"
. Defaults totrue
.-
When used with
ts-loader
, due tots-loader
's cache invalidation behavior, it sometimes prevents the template from being hot-reloaded in isolation, causing the component to reload despite only the template being edited. If this is annoying, you can set this option tofalse
(and avoid using TS expressions in templates). -
Alternatively, leave this option on (by default) and use
esbuild-loader
to transpile TS instead, which doesn't suffer from this problem (it's also a lot faster). However, do note you will need to rely on TS type checking from other sources (e.g. IDE orvue-tsc
).
-
What is Vue Loader?
vue-loader
is a loader for webpack that allows you to author Vue components in a format called Single-File Components (SFCs):
<template>
<div class="example">{{ msg }}</div>
</template>
<script>
export default {
data() {
return {
msg: 'Hello world!',
}
},
}
</script>
<style>
.example {
color: red;
}
</style>
There are many cool features provided by vue-loader
:
- Allows using other webpack loaders for each part of a Vue component, for example Sass for
<style>
and Pug for<template>
; - Allows custom blocks in a
.vue
file that can have custom loader chains applied to them; - Treat static assets referenced in
<style>
and<template>
as module dependencies and handle them with webpack loaders; - Simulate scoped CSS for each component;
- State-preserving hot-reloading during development.
In a nutshell, the combination of webpack and vue-loader
gives you a modern, flexible and extremely powerful front-end workflow for authoring Vue.js applications.
How It Works
The following section is for maintainers and contributors who are interested in the internal implementation details of
vue-loader
, and is not required knowledge for end users.
vue-loader
is not a simple source transform loader. It handles each language blocks inside an SFC with its own dedicated loader chain (you can think of each block as a "virtual module"), and finally assembles the blocks together into the final module. Here's a brief overview of how the whole thing works:
-
vue-loader
parses the SFC source code into an SFC Descriptor using@vue/compiler-sfc
. It then generates an import for each language block so the actual returned module code looks like this:// code returned from the main loader for 'source.vue' // import the <template> block import render from 'source.vue?vue&type=template' // import the <script> block import script from 'source.vue?vue&type=script' export * from 'source.vue?vue&type=script' // import <style> blocks import 'source.vue?vue&type=style&index=1' script.render = render export default script
Notice how the code is importing
source.vue
itself, but with different request queries for each block. -
We want the content in
script
block to be treated like.js
files (and if it's<script lang="ts">
, we want to to be treated like.ts
files). Same for other language blocks. So we want webpack to apply any configured module rules that matches.js
also to requests that look likesource.vue?vue&type=script
. This is whatVueLoaderPlugin
(src/plugins.ts
) does: for each module rule in the webpack config, it creates a modified clone that targets corresponding Vue language block requests.Suppose we have configured
babel-loader
for all*.js
files. That rule will be cloned and applied to Vue SFC<script>
blocks as well. Internally to webpack, a request likeimport script from 'source.vue?vue&type=script'
Will expand to:
import script from 'babel-loader!vue-loader!source.vue?vue&type=script'
Notice the
vue-loader
is also matched becausevue-loader
are applied to.vue
files.Similarly, if you have configured
style-loader
+css-loader
+sass-loader
for*.scss
files:<style scoped lang="scss">
Will be returned by
vue-loader
as:import 'source.vue?vue&type=style&index=1&scoped&lang=scss'
And webpack will expand it to:
import 'style-loader!css-loader!sass-loader!vue-loader!source.vue?vue&type=style&index=1&scoped&lang=scss'
-
When processing the expanded requests, the main
vue-loader
will get invoked again. This time though, the loader notices that the request has queries and is targeting a specific block only. So it selects (src/select.ts
) the inner content of the target block and passes it on to the loaders matched after it. -
For the
<script>
block, this is pretty much it. For<template>
and<style>
blocks though, a few extra tasks need to be performed:- We need to compile the template using the Vue template compiler;
- We need to post-process the CSS in
<style scoped>
blocks, aftercss-loader
but beforestyle-loader
.
Technically, these are additional loaders (
src/templateLoader.ts
andsrc/stylePostLoader.ts
) that need to be injected into the expanded loader chain. It would be very complicated if the end users have to configure this themselves, soVueLoaderPlugin
also injects a global Pitching Loader (src/pitcher.ts
) that intercepts Vue<template>
and<style>
requests and injects the necessary loaders. The final requests look like the following:// <template lang="pug"> import 'vue-loader/template-loader!pug-loader!source.vue?vue&type=template' // <style scoped lang="scss"> import 'style-loader!vue-loader/style-post-loader!css-loader!sass-loader!vue-loader!source.vue?vue&type=style&index=1&scoped&lang=scss'
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot