You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`customElement: boolean | RegExp`: enable custom elements mode.
10
+
11
+
-`customElement: boolean | RegExp`: enable custom elements mode. An SFC loaded in custom elements mode inlines its `<style>` tags as strings under the component's `styles` option. When used with `defineCustomElement` from Vue core, the styles will be injected into the custom element's shadow root.
11
12
- Default is `/\.ce\.vue$/`
12
-
- Setting to `true` will load all `.vue` files as native Custom Elements.
13
+
- Setting to `true` will process all `.vue` files in custom element mode.
13
14
14
15
## What is Vue Loader?
15
16
16
17
`vue-loader` is a loader for [webpack](https://webpack.js.org/) that allows you to author Vue components in a format called [Single-File Components (SFCs)](./docs/spec.md):
17
18
18
-
```vue
19
+
```vue
19
20
<template>
20
21
<div class="example">{{ msg }}</div>
21
22
</template>
22
23
23
24
<script>
24
25
export default {
25
-
data() {
26
+
data() {
26
27
return {
27
-
msg: 'Hello world!'
28
+
msg: 'Hello world!',
28
29
}
29
-
}
30
+
},
30
31
}
31
32
</script>
32
33
@@ -53,72 +54,72 @@ In a nutshell, the combination of webpack and `vue-loader` gives you a modern, f
53
54
54
55
`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:
55
56
56
-
1.`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:
57
+
1.`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:
57
58
58
-
```js
59
-
// code returned from the main loader for 'source.vue'
59
+
```js
60
+
// code returned from the main loader for 'source.vue'
60
61
61
-
// import the <template> block
62
-
importrenderfrom'source.vue?vue&type=template'
63
-
// import the <script> block
64
-
importscriptfrom'source.vue?vue&type=script'
65
-
export*from'source.vue?vue&type=script'
66
-
// import <style> blocks
67
-
import'source.vue?vue&type=style&index=1'
62
+
// import the <template> block
63
+
importrenderfrom'source.vue?vue&type=template'
64
+
// import the <script> block
65
+
importscriptfrom'source.vue?vue&type=script'
66
+
export*from'source.vue?vue&type=script'
67
+
// import <style> blocks
68
+
import'source.vue?vue&type=style&index=1'
68
69
69
-
script.render= render
70
-
exportdefaultscript
71
-
```
70
+
script.render= render
71
+
exportdefaultscript
72
+
```
72
73
73
-
Notice how the code is importing `source.vue` itself, but with different request queries for each block.
74
+
Notice how the code is importing `source.vue` itself, but with different request queries for each block.
74
75
75
76
2. 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 like `source.vue?vue&type=script`. This is what `VueLoaderPlugin` (`src/plugins.ts`) does: for each module rule in the webpack config, it creates a modified clone that targets corresponding Vue language block requests.
76
77
77
-
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 like
78
+
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 like
78
79
79
-
```js
80
-
import script from 'source.vue?vue&type=script'
81
-
```
80
+
```js
81
+
importscriptfrom'source.vue?vue&type=script'
82
+
```
82
83
83
-
Will expand to:
84
+
Will expand to:
84
85
85
-
```js
86
-
import script from 'babel-loader!vue-loader!source.vue?vue&type=script'
3. 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.
110
111
111
112
4. For the `<script>` block, this is pretty much it. For `<template>` and `<style>` blocks though, a few extra tasks need to be performed:
112
113
113
-
- We need to compile the template using the Vue template compiler;
114
-
- We need to post-process the CSS in `<style scoped>` blocks, **after** `css-loader` but **before** `style-loader`.
114
+
- We need to compile the template using the Vue template compiler;
115
+
- We need to post-process the CSS in `<style scoped>` blocks, **after** `css-loader` but **before** `style-loader`.
115
116
116
-
Technically, these are additional loaders (`src/templateLoader.ts` and `src/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, so `VueLoaderPlugin` also injects a global [Pitching Loader](https://webpack.js.org/api/loaders/#pitching-loader) (`src/pitcher.ts`) that intercepts Vue `<template>` and `<style>` requests and injects the necessary loaders. The final requests look like the following:
117
+
Technically, these are additional loaders (`src/templateLoader.ts` and `src/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, so `VueLoaderPlugin` also injects a global [Pitching Loader](https://webpack.js.org/api/loaders/#pitching-loader) (`src/pitcher.ts`) that intercepts Vue `<template>` and `<style>` requests and injects the necessary loaders. The final requests look like the following:
0 commit comments