940755f1ca68ba973567ffcb16c2aed0c814bbc35c6da3ac12c53c8b32865bfde71e0eea15cb8bf541768d8edc81532a65ea05f935ee302af23cc89259115b 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # vue-loader [![Build Status](https://circleci.com/gh/vuejs/vue-loader/tree/master.svg?style=shield)](https://circleci.com/gh/vuejs/vue-loader/tree/master) [![Windows Build status](https://ci.appveyor.com/api/projects/status/8cdonrkbg6m4k1tm/branch/master?svg=true)](https://ci.appveyor.com/project/yyx990803/vue-loader/branch/master)
  2. > webpack loader for Vue Single-File Components
  3. **NOTE:** The master branch now hosts the code for v15! Legacy code is now in the [v14 branch](https://github.com/vuejs/vue-loader/tree/v14).
  4. - [Documentation](https://vue-loader.vuejs.org)
  5. - [Migrating from v14](https://vue-loader.vuejs.org/migrating.html)
  6. ## What is Vue Loader?
  7. `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):
  8. ``` vue
  9. <template>
  10. <div class="example">{{ msg }}</div>
  11. </template>
  12. <script>
  13. export default {
  14. data () {
  15. return {
  16. msg: 'Hello world!'
  17. }
  18. }
  19. }
  20. </script>
  21. <style>
  22. .example {
  23. color: red;
  24. }
  25. </style>
  26. ```
  27. There are many cool features provided by `vue-loader`:
  28. - Allows using other webpack loaders for each part of a Vue component, for example Sass for `<style>` and Pug for `<template>`;
  29. - Allows custom blocks in a `.vue` file that can have custom loader chains applied to them;
  30. - Treat static assets referenced in `<style>` and `<template>` as module dependencies and handle them with webpack loaders;
  31. - Simulate scoped CSS for each component;
  32. - State-preserving hot-reloading during development.
  33. 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.
  34. ## How It Works
  35. > 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.
  36. `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:
  37. 1. `vue-loader` parses the SFC source code into an *SFC Descriptor* using `@vue/component-compiler-utils`. It then generates an import for each language block so the actual returned module code looks like this:
  38. ``` js
  39. // code returned from the main loader for 'source.vue'
  40. // import the <template> block
  41. import render from 'source.vue?vue&type=template'
  42. // import the <script> block
  43. import script from 'source.vue?vue&type=script'
  44. export * from 'source.vue?vue&type=script'
  45. // import <style> blocks
  46. import 'source.vue?vue&type=style&index=1'
  47. script.render = render
  48. export default script
  49. ```
  50. Notice how the code is importing `source.vue` itself, but with different request queries for each block.
  51. 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.
  52. 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
  53. ``` js
  54. import script from 'source.vue?vue&type=script'
  55. ```
  56. Will expand to:
  57. ``` js
  58. import script from 'babel-loader!vue-loader!source.vue?vue&type=script'
  59. ```
  60. Notice the `vue-loader` is also matched because `vue-loader` are applied to `.vue` files.
  61. Similarly, if you have configured `style-loader` + `css-loader` + `sass-loader` for `*.scss` files:
  62. ``` html
  63. <style scoped lang="scss">
  64. ```
  65. Will be returned by `vue-loader` as:
  66. ``` js
  67. import 'source.vue?vue&type=style&index=1&scoped&lang=scss'
  68. ```
  69. And webpack will expand it to:
  70. ``` js
  71. import 'style-loader!css-loader!sass-loader!vue-loader!source.vue?vue&type=style&index=1&scoped&lang=scss'
  72. ```
  73. 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.
  74. 4. For the `<script>` block, this is pretty much it. For `<template>` and `<style>` blocks though, a few extra tasks need to be performed:
  75. - We need to compile the template using the Vue template compiler;
  76. - We need to post-process the CSS in `<style scoped>` blocks, **before** `css-loader`.
  77. 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:
  78. ``` js
  79. // <template lang="pug">
  80. import 'vue-loader/template-loader!pug-loader!vue-loader!source.vue?vue&type=template'
  81. // <style scoped lang="scss">
  82. import 'style-loader!css-loader!vue-loader/style-post-loader!sass-loader!vue-loader!source.vue?vue&type=style&index=1&scoped&lang=scss'
  83. ```