source: node_modules/@vue/compiler-sfc/README.md@ a70b5a4

Last change on this file since a70b5a4 was 57e58a3, checked in by ste08 <sjovanoska@…>, 4 months ago

Initial commit

  • Property mode set to 100644
File size: 4.3 KB
Line 
1# @vue/compiler-sfc
2
3> Lower level utilities for compiling Vue Single File Components
4
5**Note: as of 3.2.13+, this package is included as a dependency of the main `vue` package and can be accessed as `vue/compiler-sfc`. This means you no longer need to explicitly install this package and ensure its version match that of `vue`'s. Just use the main `vue/compiler-sfc` deep import instead.**
6
7This package contains lower level utilities that you can use if you are writing a plugin / transform for a bundler or module system that compiles Vue Single File Components (SFCs) into JavaScript. It is used in [vue-loader](https://github.com/vuejs/vue-loader) and [@vitejs/plugin-vue](https://github.com/vitejs/vite-plugin-vue/tree/main/packages/plugin-vue).
8
9## API
10
11The API is intentionally low-level due to the various considerations when integrating Vue SFCs in a build system:
12
13- Separate hot-module replacement (HMR) for script, template and styles
14
15 - template updates should not reset component state
16 - style updates should be performed without component re-render
17
18- Leveraging the tool's plugin system for pre-processor handling. e.g. `<style lang="scss">` should be processed by the corresponding webpack loader.
19
20- In some cases, transformers of each block in an SFC do not share the same execution context. For example, when used with `thread-loader` or other parallelized configurations, the template sub-loader in `vue-loader` may not have access to the full SFC and its descriptor.
21
22The general idea is to generate a facade module that imports the individual blocks of the component. The trick is the module imports itself with different query strings so that the build system can handle each request as "virtual" modules:
23
24```
25 +--------------------+
26 | |
27 | script transform |
28 +----->+ |
29 | +--------------------+
30 |
31+--------------------+ | +--------------------+
32| | | | |
33| facade transform +----------->+ template transform |
34| | | | |
35+--------------------+ | +--------------------+
36 |
37 | +--------------------+
38 +----->+ |
39 | style transform |
40 | |
41 +--------------------+
42```
43
44Where the facade module looks like this:
45
46```js
47// main script
48import script from '/project/foo.vue?vue&type=script'
49// template compiled to render function
50import { render } from '/project/foo.vue?vue&type=template&id=xxxxxx'
51// css
52import '/project/foo.vue?vue&type=style&index=0&id=xxxxxx'
53
54// attach render function to script
55script.render = render
56
57// attach additional metadata
58// some of these should be dev only
59script.__file = 'example.vue'
60script.__scopeId = 'xxxxxx'
61
62// additional tooling-specific HMR handling code
63// using __VUE_HMR_API__ global
64
65export default script
66```
67
68### High Level Workflow
69
701. In facade transform, parse the source into descriptor with the `parse` API and generate the above facade module code based on the descriptor;
71
722. In script transform, use `compileScript` to process the script. This handles features like `<script setup>` and CSS variable injection. Alternatively, this can be done directly in the facade module (with the code inlined instead of imported), but it will require rewriting `export default` to a temp variable (a `rewriteDefault` convenience API is provided for this purpose) so additional options can be attached to the exported object.
73
743. In template transform, use `compileTemplate` to compile the raw template into render function code.
75
764. In style transform, use `compileStyle` to compile raw CSS to handle `<style scoped>`, `<style module>` and CSS variable injection.
77
78Options needed for these APIs can be passed via the query string.
79
80For detailed API references and options, check out the source type definitions. For actual usage of these APIs, check out [@vitejs/plugin-vue](https://github.com/vitejs/vite-plugin-vue/tree/main/packages/plugin-vue) or [vue-loader](https://github.com/vuejs/vue-loader/tree/next).
Note: See TracBrowser for help on using the repository browser.