Socket
Socket
Sign inDemoInstall

@vue/compiler-sfc

Package Overview
Dependencies
13
Maintainers
2
Versions
248
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.3M
increased by1.06%
Maintainers
2
Install size
6.51 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.22 (2024-04-15)

Bug Fixes

  • compat: fix $options mutation + adjust private API initialization (d58d133), closes #10626 #10636
  • compile-sfc: analyze v-bind shorthand usage in template (#10518) (e5919d4), closes #10515
  • compiler-core: fix loc.source for end tags with whitespace before > (16174da), closes #10694 #10695
  • compiler-core: fix v-bind shorthand for component :is (04af950), closes #10469 #10471
  • compiler-sfc: :is() and :where() in compound selectors (#10522) (660cadc), closes #10511
  • compiler-sfc: also search for .tsx when type import's extension is omitted (#10637) (34106bc), closes #10635
  • compiler-sfc: fix defineModel coercion for boolean + string union types (#9603) (0cef65c), closes #9587 #10676
  • compiler-sfc: fix universal selector scope (#10551) (54a6afa), closes #10548
  • compiler-sfc: use options module name if options provide runtimeModuleName options (#10457) (e76d743), closes #10454
  • custom-element: avoid setting attr to null if it is removed (#9012) (b49306a), closes #9006 #10324
  • hydration: properly handle optimized mode during hydrate node (#10638) (2ec06fd), closes #10607
  • reactivity: computed should not be detected as true by isProxy (#10401) (9da34d7)
  • reactivity: fix hasOwnProperty key coercion edge cases (969c5fb)
  • reactivity: fix tracking when hasOwnProperty is called with non-string value (c3c5dc9), closes #10455 #10464
  • runtime-core: fix errorHandler causes an infinite loop during execution (#9575) (ab59bed)
  • runtime-core: handle invalid values in callWithAsyncErrorHandling (53d15d3)
  • runtime-core: show hydration mismatch details for non-rectified mismatches too when PROD_HYDRATION_MISMATCH_DETAILS is set (#10599) (0dea7f9)
  • runtime-dom: v-model string/number coercion for multiselect options (#10576) (db374e5)
  • runtime-dom: fix css v-bind for suspensed components (#8523) (67722ba), closes #8520
  • runtime-dom: force update v-model number with leading 0 (#10506) (15ffe8f), closes #10503 #10615
  • runtime-dom: sanitize wrongly passed string value as event handler (#8953) (7ccd453), closes #8818
  • ssr: don't render v-if comments in TransitionGroup (#6732) (5a96267), closes #6715
  • Transition: ensure the KeepAlive children unmount w/ out-in mode (#10632) (fc99e4d), closes #10620
  • TransitionGroup: avoid set transition hooks for comment nodes and text nodes (#9421) (140a768), closes #4621 #4622 #5153 #5168 #7898 #9067
  • types: avoid merging object union types when using withDefaults (#10596) (37ba93c), closes #10594

Performance Improvements

  • add __NO_SIDE_EFFECTS__ comments (#9053) (d46df6b)
  • optimize component props/slots internal object checks (6af733d)
  • ssr: avoid calling markRaw on component instance proxy (4bc9f39)
  • ssr: optimize setup context creation for ssr in v8 (ca84316)

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 15 Apr 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