Socket
Socket
Sign inDemoInstall

@vue/compiler-sfc

Package Overview
Dependencies
13
Maintainers
2
Versions
249
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @vue/compiler-sfc

@vue/compiler-sfc


Version published
Weekly downloads
6.2M
increased by0.03%
Maintainers
2
Install size
6.53 MB
Created
Weekly downloads
 

Package description

What is @vue/compiler-sfc?

The @vue/compiler-sfc package is designed for pre-compiling Vue Single File Components (SFCs). It provides parsing and compiling functionalities for Vue components, allowing developers to compile template syntax to render functions, extract custom blocks, and handle scoped CSS, among other things.

What are @vue/compiler-sfc's main functionalities?

Parsing Single File Components

This feature allows you to parse Vue SFCs into an object descriptor that contains the structure of the component, including its template, script, and styles.

const { parse } = require('@vue/compiler-sfc');
const { descriptor } = parse('<template><div>Hello World</div></template>');

Compiling Template to Render Function

This feature compiles the template part of a Vue SFC into a JavaScript render function, which can be used by Vue to render the component.

const { compileTemplate } = require('@vue/compiler-sfc');
const { code } = compileTemplate({ source: '<div>{{ message }}</div>', filename: 'example.vue' });

Handling Scoped CSS

This feature processes the style part of a Vue SFC, adding scoping attributes to the styles to ensure they only apply to the current component.

const { compileStyle } = require('@vue/compiler-sfc');
const { code } = compileStyle({ source: '.example { color: red; }', filename: 'example.vue', scoped: true });

Extracting Custom Blocks

This feature allows you to extract custom blocks from a Vue SFC, which can be used for documentation, testing, or other purposes.

const { parse } = require('@vue/compiler-sfc');
const { descriptor } = parse('<custom-block>Some content</custom-block>');
const customBlockContent = descriptor.customBlocks[0].content;

Other packages similar to @vue/compiler-sfc

Changelog

Source

3.4.28 (2024-06-14)

Bug Fixes

  • compat: correctly transform non-identifier expressions in legacy filter syntax (#10896) (07b3c4b), closes #10852
  • compat: ensure proper handling of render fuction from SFC using Vue.extend (#7781) (c73847f), closes #7766
  • compat: only warn ATTR_FALSE_VALUE when enabled (04729ba), closes #11126
  • compile-sfc: register props destructure rest id as setup bindings (#10888) (b2b5f57), closes #10885
  • compile-sfc: Support project reference with folder, (#10908) (bdeac37), closes #10907
  • compiler-core: allow unicode to appear in simple identifiers (#6765) (3ea9644), closes #6367
  • compiler-core: change v-for key type to match Object.keys (#10963) (9fead52), closes #8819
  • compiler-core: emit TS-compatible function declaration when requested (#9363) (5d25850)
  • compiler-core: fix :key shorthand on v-for (#10942) (29425df), closes #10882 #10939
  • compiler-core: make ForIteratorExpression's returns property optional (#11011) (5b8c1af)
  • compiler-core: should set <math> tag as block to retain MathML namespace after patching (#10891) (87c5443)
  • compiler-core: v-for expression missing source with spaces should emit error (#5821) (b9ca202), closes #5819
  • compiler-sfc: improve type resolving for the keyof operator (#10921) (293cf4e), closes #10920 #11002
  • compiler-sfc: support as keyword with template literal types (#11100) (2594b1d), closes #10962
  • compiler-sfc: support type resolve for keyof for intersection & union types (#11132) (495263a), closes #11129
  • compiler-sfc: throw error when import macro as alias (#11041) (34a97ed)
  • correct the type of <details>'s onToggle event handler (#10938) (fd18ce7), closes #10928
  • custom-element: disconnect MutationObserver in nextTick in case that custom elements are moved (#10613) (bbb5be2), closes #10610
  • custom-elements: compatibility of createElement in older versions of Chrome (#9615) (a88295d), closes #9614
  • hmr: avoid infinite recursion when reloading hmr components (#6936) (36bd9b0), closes #6930
  • hydration: log hydration error even when using async components (#9403) (5afc76c), closes #9369
  • KeepAlive: properly cache nested Suspense subtree (#10912) (07764fe)
  • npm: explicitly add @vue/reactivity as dependency of @vue/runtime-dom (#10468) (ec424f6)
  • reactivity: pass oldValue in debug info when triggering refs (#8210) (3b0a56a), closes vuejs/pinia#2061
  • runtime-core: avoid traversing static children for vnodes w/ PatchFlags.BAIL (#11115) (b557d3f), closes #10547
  • runtime-core: do not fire mount/activated hooks if unmounted before mounted (#9370) (aa156ed), closes #8898 #9264 #9617
  • runtime-core: ensure suspense creates dep component's render effect with correct optimized flag (#7689) (c521f95), closes #7688
  • runtime-core: fix missed updates when passing text vnode to <component :is> (#8304) (b310ec3), closes #8298
  • runtime-core: fix stale v-memo after v-if toggle (#6606) (edf2638), closes #6593
  • runtime-core: fix Transition for components with root-level v-if (#7678) (ef2e737), closes #7649
  • runtime-dom: also set attribute for form element state (537a571), closes #6007 #6012
  • runtime-dom: support Symbol for input value bindings (#10608) (188f3ae), closes #10597
  • shared: ensure invokeArrayFns handles undefined arguments (#10869) (9b40d0f), closes #10863
  • ssr: directive binding.instance should respect exposed during ssr (df686ab), closes #7499 #7502
  • ssr: fix hydration for node with empty text node (#7216) (d1011c0)
  • ssr: fix the bug that multi slot scope id does not work on component (#6100) (4c74302), closes #6093
  • teleport: do not throw target warning when teleport is disabled (#9818) (15ee43f)
  • transition: ensure Transition enterHooks are updated after clone (#11066) (671cf29), closes #11061
  • types/apiWatch: correct type inference for reactive array (#11036) (aae2d78), closes #9416
  • types: improve app.provide type checking (#10603) (612bbf0), closes #10602
  • types: support generic argument in setup context expose method (#8507) (635a59b)
  • v-model: fix the lazy modifier is not reset by other modifications (#8547) (a52a02f), closes #8546 #6564 #6773
  • watch: support traversing symbol properties in deep watcher (#10969) (a3e8aaf), closes #402

Readme

Source

@vue/compiler-sfc

Lower level utilities for compiling Vue Single File Components

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.

This 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 and @vitejs/plugin-vue.

API

The API is intentionally low-level due to the various considerations when integrating Vue SFCs in a build system:

  • Separate hot-module replacement (HMR) for script, template and styles

    • template updates should not reset component state
    • style updates should be performed without component re-render
  • Leveraging the tool's plugin system for pre-processor handling. e.g. <style lang="scss"> should be processed by the corresponding webpack loader.

  • 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.

The 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:

                                  +--------------------+
                                  |                    |
                                  |  script transform  |
                           +----->+                    |
                           |      +--------------------+
                           |
+--------------------+     |      +--------------------+
|                    |     |      |                    |
|  facade transform  +----------->+ template transform |
|                    |     |      |                    |
+--------------------+     |      +--------------------+
                           |
                           |      +--------------------+
                           +----->+                    |
                                  |  style transform   |
                                  |                    |
                                  +--------------------+

Where the facade module looks like this:

// main script
import script from '/project/foo.vue?vue&type=script'
// template compiled to render function
import { render } from '/project/foo.vue?vue&type=template&id=xxxxxx'
// css
import '/project/foo.vue?vue&type=style&index=0&id=xxxxxx'

// attach render function to script
script.render = render

// attach additional metadata
// some of these should be dev only
script.__file = 'example.vue'
script.__scopeId = 'xxxxxx'

// additional tooling-specific HMR handling code
// using __VUE_HMR_API__ global

export default script

High Level Workflow

  1. In facade transform, parse the source into descriptor with the parse API and generate the above facade module code based on the descriptor;

  2. 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.

  3. In template transform, use compileTemplate to compile the raw template into render function code.

  4. In style transform, use compileStyle to compile raw CSS to handle <style scoped>, <style module> and CSS variable injection.

Options needed for these APIs can be passed via the query string.

For detailed API references and options, check out the source type definitions. For actual usage of these APIs, check out @vitejs/plugin-vue or vue-loader.

Keywords

FAQs

Last updated on 14 Jun 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc