### Example Usage of VueLoaderPlugin in Webpack Configuration
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/03-vue-loader-plugin.md
This example demonstrates how to integrate VueLoaderPlugin into a webpack.config.js file. Ensure both vue-loader and VueLoaderPlugin are installed.
```javascript
// webpack.config.js
const { VueLoaderPlugin } = require('vue-loader')
module.exports = {
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.js$/,
loader: 'babel-loader'
}
]
},
plugins: [
new VueLoaderPlugin()
]
}
```
--------------------------------
### Example: Template Inlining Conditions
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/08-resolve-script.md
Illustrates scenarios where a template can be inlined into a `
...
// Cannot be inlined - has preprocessor
...
// Cannot be inlined - external template
// Cannot be inlined - no script setup
...
```
--------------------------------
### Example Query Strings
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/04-select-block.md
Illustrative query strings demonstrating how different SFC blocks can be requested.
```none
foo.vue?vue&type=template&id=abc12345&scoped=true&ts=true
foo.vue?vue&type=script&lang=ts
foo.vue?vue&type=style&index=0&id=abc12345&module=true
foo.vue?vue&type=custom&index=0&blockType=i18n
```
--------------------------------
### StyleInlineLoader Result Example
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/07-style-loaders.md
Shows an example of the output generated by StyleInlineLoader when processing CSS.
```javascript
export default ".container { color: red; }"
```
--------------------------------
### Example Property Array for exportHelper
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/13-misc-utilities.md
Illustrates the format of the property array passed to `exportHelper` before and after processing.
```javascript
[
['__scopeId', '"data-v-a1b2c3d4"'],
['__file', '"/path/to/App.vue"'],
['__cssModules', 'cssModules'],
['render', 'render']
]
```
```javascript
[
['__scopeId', 'data-v-a1b2c3d4'],
['__file', '/path/to/App.vue'],
['__cssModules', { ... }],
['render', function render(...) { ... }]
]
```
--------------------------------
### Plugin Usage Example
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/09-utility-functions.md
Demonstrates checking for Webpack 5 compatibility and setting up Hot Module Replacement (HMR) hooks within a plugin.
```typescript
const isWebpack5 = testWebpack5(compiler)
if (needHMR(vueLoaderOptions, compiler.options)) {
// Setup HMR hooks
}
```
--------------------------------
### Vue Single-File Component Example
Source: https://github.com/vuejs/vue-loader/blob/main/README.md
A basic example of a Vue Single-File Component (SFC) demonstrating template, script, and style sections. This format is enabled by vue-loader.
```vue
{{ msg }}
```
--------------------------------
### Example Template Request URL
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/05-template-loader.md
Illustrates the format of a template request URL processed by the Vue Loader.
```text
src/App.vue?vue&type=template&id=a1b2c3d4&scoped=true
```
--------------------------------
### cleanQuery Examples
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/10-descriptor-cache.md
Demonstrates the usage of the `cleanQuery` function with different input strings, showing how it removes query parameters to normalize file paths for cache key purposes.
```typescript
cleanQuery('App.vue?vue&type=script') // 'App.vue'
```
```typescript
cleanQuery('App.vue') // 'App.vue'
```
```typescript
cleanQuery('foo?bar?baz') // 'foo'
```
--------------------------------
### resolveScript
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/08-resolve-script.md
Compiles and returns the script block, handling `
```
--------------------------------
### Basic Webpack Configuration for Vue Loader
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/README.md
This snippet shows the essential Webpack configuration to enable Vue Loader and its plugin. Ensure you have `vue-loader` and `vue-loader-plugin` installed.
```javascript
const { VueLoaderPlugin } = require('vue-loader')
module.exports = {
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
}
]
},
plugins: [
new VueLoaderPlugin()
]
}
```
--------------------------------
### Install VueLoaderPlugin in Webpack Config
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/03-vue-loader-plugin.md
This is a required step for vue-loader to work correctly. Ensure the plugin is included in your webpack configuration's plugins array.
```javascript
// This is required for vue-loader to work
plugins: [new VueLoaderPlugin()]
```
--------------------------------
### genHotReloadCode
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/11-hot-reload.md
Generates the HMR setup code injected at the end of the compiled component module. It handles creating or reloading component records and accepting template updates.
```APIDOC
## genHotReloadCode
### Description
Generates the HMR setup code injected at the end of the compiled component module. This function is crucial for enabling hot module replacement in Vue.js applications.
### Method
```typescript
function genHotReloadCode(id: string, templateRequest: string | undefined): string
```
### Parameters
#### Path Parameters
- **id** (string) - Required - Component scope ID (8-character hex hash)
- **templateRequest** (string) - Optional - Webpack request string for template block, undefined if no template
### Response
#### Success Response
- **string** - JavaScript code string to inject into module
### Generated Code Example (with template)
```javascript
/* hot reload */
if (module.hot) {
__exports__.__hmrId = "a1b2c3d4"
const api = __VUE_HMR_RUNTIME__
module.hot.accept()
if (!api.createRecord('a1b2c3d4', __exports__)) {
api.reload('a1b2c3d4', __exports__)
}
module.hot.accept("!vue-loader!./App.vue?vue&type=template", () => {
api.rerender('a1b2c3d4', render)
})
}
```
### Generated Code Example (without template)
```javascript
/* hot reload */
if (module.hot) {
__exports__.__hmrId = "a1b2c3d4"
const api = __VUE_HMR_RUNTIME__
module.hot.accept()
if (!api.createRecord('a1b2c3d4', __exports__)) {
api.reload('a1b2c3d4', __exports__)
}
}
```
```
--------------------------------
### Common Webpack and Vue SFC Type Imports
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/14-types-reference.md
Illustrates a common pattern for importing various types from 'webpack' and 'vue/compiler-sfc' modules. This setup is typical in vue-loader files for type safety.
```typescript
import type {
LoaderContext,
Compiler,
RuleSetRule,
} from 'webpack'
import type {
SFCDescriptor,
SFCBlock,
SFCScriptBlock,
TemplateCompiler,
CompilerOptions,
SFCTemplateCompileOptions,
SFCScriptCompileOptions,
CompilerError,
} from 'vue/compiler-sfc'
import type { LoaderDefinitionFunction } from 'webpack'
import type { VueLoaderOptions } from './index'
```
--------------------------------
### Generated CSS Modules Code With Custom Module Name
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/12-css-modules.md
This example demonstrates how Vue Loader generates code for CSS modules when a custom module name is specified instead of the default '$style'. The import request includes the custom name, and the assignment uses this name as the key.
```javascript
import style0 from 'foo.vue?vue&type=style&index=0&module=foo'
cssModules["foo"] = style0
```
--------------------------------
### Configuration Options Table
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/02-vue-loader-options.md
A detailed table listing each configuration option, its type, requirement, default value, and a description.
```APIDOC
## Configuration Options
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
| babelParserPlugins | string[] | No | [] | Babel parser plugins to enable for parsing `
```
--------------------------------
### Webpack Expanded Request with Pitching Loader for Template
Source: https://github.com/vuejs/vue-loader/blob/main/README.md
Demonstrates how vue-loader's pitching loader intercepts template block requests and injects necessary loaders like template-loader and pug-loader.
```javascript
import 'vue-loader/template-loader!pug-loader!source.vue?vue&type=template'
```
--------------------------------
### vue-loader with Custom Compiler Options
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/02-vue-loader-options.md
Pass custom options to the template compiler, such as defining custom elements.
```javascript
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
compilerOptions: {
isCustomElement: tag => tag.startsWith('ion-')
}
}
}
```
--------------------------------
### Multiple CSS Modules with Custom Names
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/12-css-modules.md
Demonstrates how to define and use multiple CSS modules within a single Vue component, each with a distinct name for its class scope.
```vue
```
```javascript
const cssModules = {}
import style0 from '...'
cssModules["ui"] = style0
import style1 from '...'
cssModules["theme"] = style1
```
--------------------------------
### Vue SFC Query String Formats
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/00-index.md
Details the various query string formats used to request specific blocks (template, script, style, custom) from a .vue file.
```plaintext
?vue&type=template&id=&scoped=true — Template block
?vue&type=script&lang=ts — Script block
?vue&type=style&index=0&id=&module=true — Style block
?vue&type=custom&index=0&blockType=i18n — Custom block
```
--------------------------------
### resolveTemplateTSOptions
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/09-utility-functions.md
Prepares TypeScript options for template compilation. It returns TypeScript compiler options or null if TypeScript is disabled in templates. It checks for `lang="ts"` or `lang="tsx"` in the script and updates expression plugins.
```APIDOC
## resolveTemplateTSOptions
### Description
Prepares TypeScript options for template compilation.
### Signature
```typescript
function resolveTemplateTSOptions(descriptor: SFCDescriptor, options: VueLoaderOptions): CompilerOptions | null
```
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Parameters
- **descriptor** (SFCDescriptor) - Required - Parsed SFC descriptor
- **options** (VueLoaderOptions) - Required - Loader configuration
### Returns
`CompilerOptions | null` — TypeScript compiler options or null if disabled
### Logic
1. Returns null if `enableTsInTemplate` is false
2. Checks if script has `lang="ts"` or `lang="tsx"`
3. Adds 'typescript' to expression plugins if needed
4. Returns object with `isTS` and updated `expressionPlugins`
### Example output
```typescript
{
isTS: true,
expressionPlugins: ['typescript', 'jsx']
}
```
### Usage
Called by templateLoader and resolveScript to enable TS syntax in templates.
```
--------------------------------
### Generic Type Parameter for Loader Options
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/14-types-reference.md
Illustrates using a generic type parameter to specify the type of options a loader accepts.
```typescript
function getOptions(
loaderContext: LoaderContext
): VueLoaderOptions
```
--------------------------------
### Vue SFC Processing Pipeline
Source: https://github.com/vuejs/vue-loader/blob/main/_autodocs/00-index.md
Illustrates the flow of a .vue file through the vue-loader processing pipeline, from input to final component export.
```plaintext
.vue file input
↓
parse() — @vue/compiler-sfc parses into SFCDescriptor
↓
selectBlock() — Extracts individual blocks for requestQuery
↓
Router by type:
• template → templateLoader → render function
• script → resolveScript → compiled script (handles