You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
6.9 KiB

3 months ago
  1. # vue-loader [![ci](https://github.com/vuejs/vue-loader/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/vuejs/vue-loader/actions/workflows/ci.yml)
  2. > webpack loader for Vue Single-File Components
  3. - [Documentation](https://vue-loader.vuejs.org)
  4. ## v17.2.1+ Only Options
  5. - `experimentalInlineMatchResource: boolean`: enable [Inline matchResource](https://webpack.js.org/api/loaders/#inline-matchresource) for rule matching for vue-loader.
  6. ## v16+ Only Options
  7. - `reactivityTransform: boolean`: enable [Vue Reactivity Transform](https://github.com/vuejs/rfcs/discussions/369) (SFCs only).
  8. - ~~`refSugar: boolean`: **removed.** use `reactivityTransform` instead.~~
  9. - `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.
  10. - Default is `/\.ce\.vue$/`
  11. - Setting to `true` will process all `.vue` files in custom element mode.
  12. - `enableTsInTemplate: boolean` (16.8+): allow TS expressions in templates when `<script>` has `lang="ts"`. Defaults to `true`.
  13. - When used with `ts-loader`, due to `ts-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 to `false` (and avoid using TS expressions in templates).
  14. - Alternatively, leave this option on (by default) and use [`esbuild-loader`](https://github.com/privatenumber/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 or `vue-tsc`).
  15. ## What is Vue Loader?
  16. `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. ```vue
  18. <template>
  19. <div class="example">{{ msg }}</div>
  20. </template>
  21. <script>
  22. export default {
  23. data() {
  24. return {
  25. msg: 'Hello world!',
  26. }
  27. },
  28. }
  29. </script>
  30. <style>
  31. .example {
  32. color: red;
  33. }
  34. </style>
  35. ```
  36. There are many cool features provided by `vue-loader`:
  37. - Allows using other webpack loaders for each part of a Vue component, for example Sass for `<style>` and Pug for `<template>`;
  38. - Allows custom blocks in a `.vue` file that can have custom loader chains applied to them;
  39. - Treat static assets referenced in `<style>` and `<template>` as module dependencies and handle them with webpack loaders;
  40. - Simulate scoped CSS for each component;
  41. - State-preserving hot-reloading during development.
  42. 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.
  43. ## How It Works
  44. > 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.
  45. `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:
  46. 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:
  47. ```js
  48. // code returned from the main loader for 'source.vue'
  49. // import the <template> block
  50. import render from 'source.vue?vue&type=template'
  51. // import the <script> block
  52. import script from 'source.vue?vue&type=script'
  53. export * from 'source.vue?vue&type=script'
  54. // import <style> blocks
  55. import 'source.vue?vue&type=style&index=1'
  56. script.render = render
  57. export default script
  58. ```
  59. Notice how the code is importing `source.vue` itself, but with different request queries for each block.
  60. 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.
  61. 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
  62. ```js
  63. import script from 'source.vue?vue&type=script'
  64. ```
  65. Will expand to:
  66. ```js
  67. import script from 'babel-loader!vue-loader!source.vue?vue&type=script'
  68. ```
  69. Notice the `vue-loader` is also matched because `vue-loader` are applied to `.vue` files.
  70. Similarly, if you have configured `style-loader` + `css-loader` + `sass-loader` for `*.scss` files:
  71. ```html
  72. <style scoped lang="scss">
  73. ```
  74. Will be returned by `vue-loader` as:
  75. ```js
  76. import 'source.vue?vue&type=style&index=1&scoped&lang=scss'
  77. ```
  78. And webpack will expand it to:
  79. ```js
  80. import 'style-loader!css-loader!sass-loader!vue-loader!source.vue?vue&type=style&index=1&scoped&lang=scss'
  81. ```
  82. 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.
  83. 4. For the `<script>` block, this is pretty much it. For `<template>` and `<style>` blocks though, a few extra tasks need to be performed:
  84. - We need to compile the template using the Vue template compiler;
  85. - We need to post-process the CSS in `<style scoped>` blocks, **after** `css-loader` but **before** `style-loader`.
  86. 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:
  87. ```js
  88. // <template lang="pug">
  89. import 'vue-loader/template-loader!pug-loader!source.vue?vue&type=template'
  90. // <style scoped lang="scss">
  91. import 'style-loader!vue-loader/style-post-loader!css-loader!sass-loader!vue-loader!source.vue?vue&type=style&index=1&scoped&lang=scss'
  92. ```