Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@vue/component-compiler-utils

vuejs4.4mMIT3.3.0TypeScript support: included

Lower level utilities for compiling Vue single file components

vue, sfc, component, compiler

readme

@vue/component-compiler-utils Build Status

Lower level utilities for compiling Vue single file components

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 into JavaScript. It is used in vue-loader version 15 and above.

The API surface is intentionally minimal - the goal is to reuse as much as possible while being as flexible as possible.

Why isn't vue-template-compiler a peerDependency?

Since this package is more often used as a low-level utility, it is usually a transitive dependency in an actual Vue project. It is therefore the responsibility of the higher-level package (e.g. vue-loader) to inject vue-template-compiler via options when calling the parse and compileTemplate methods.

Not listing it as a peer depedency also allows tooling authors to use a non-default template compiler instead of vue-template-compiler without having to include it just to fullfil the peer dep requirement.

API

parse(ParseOptions): SFCDescriptor

Parse raw single file component source into a descriptor with source maps. The actual compiler (vue-template-compiler) must be passed in via the compiler option so that the specific version used can be determined by the end user.

interface ParseOptions {
  source: string
  filename?: string
  compiler: VueTemplateCompiler
  // https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#compilerparsecomponentfile-options
  // default: { pad: 'line' }
  compilerParseOptions?: VueTemplateCompilerParseOptions
  sourceRoot?: string
  needMap?: boolean
}

interface SFCDescriptor {
  template: SFCBlock | null
  script: SFCBlock | null
  styles: SFCBlock[]
  customBlocks: SFCCustomBlock[]
}

interface SFCCustomBlock {
  type: string
  content: string
  attrs: { [key: string]: string | true }
  start: number
  end: number
  map?: RawSourceMap
}

interface SFCBlock extends SFCCustomBlock {
  lang?: string
  src?: string
  scoped?: boolean
  module?: string | boolean
}

compileTemplate(TemplateCompileOptions): TemplateCompileResults

Takes raw template source and compile it into JavaScript code. The actual compiler (vue-template-compiler) must be passed in via the compiler option so that the specific version used can be determined by the end user.

It can also optionally perform pre-processing for any templating engine supported by consolidate.

interface TemplateCompileOptions {
  source: string
  filename: string

  compiler: VueTemplateCompiler
  // https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#compilercompiletemplate-options
  // default: {}
  compilerOptions?: VueTemplateCompilerOptions

  // Template preprocessor
  preprocessLang?: string
  preprocessOptions?: any

  // Transform asset urls found in the template into `require()` calls
  // This is off by default. If set to true, the default value is
  // {
  //   audio: 'src',
  //   video: ['src', 'poster'],
  //   source: 'src',
  //   img: 'src',
  //   image: ['xlink:href', 'href'],
  //   use: ['xlink:href', 'href']
  // }
  transformAssetUrls?: AssetURLOptions | boolean

  // For vue-template-es2015-compiler, which is a fork of Buble
  transpileOptions?: any

  isProduction?: boolean  // default: false
  isFunctional?: boolean  // default: false
  optimizeSSR?: boolean   // default: false

  // Whether prettify compiled render function or not (development only)
  // default: true
  prettify?: boolean
}

interface TemplateCompileResult {
  ast: Object | undefined
  code: string
  source: string
  tips: string[]
  errors: string[]
}

interface AssetURLOptions {
  [name: string]: string | string[]
}

Handling the Output

The resulting JavaScript code will look like this:

var render = function (h) { /* ... */}
var staticRenderFns = [function (h) { /* ... */}, function (h) { /* ... */}]

It does NOT assume any module system. It is your responsibility to handle the exports, if needed.

compileStyle(StyleCompileOptions)

Take input raw CSS and applies scoped CSS transform. It does NOT handle pre-processors. If the component doesn't use scoped CSS then this step can be skipped.

interface StyleCompileOptions {
  source: string
  filename: string
  id: string
  map?: any
  scoped?: boolean
  trim?: boolean
  preprocessLang?: string
  preprocessOptions?: any
  postcssOptions?: any
  postcssPlugins?: any[]
}

interface StyleCompileResults {
  code: string
  map: any | void
  rawResult: LazyResult | void // raw lazy result from PostCSS
  errors: string[]
}

compileStyleAsync(StyleCompileOptions)

Same as compileStyle(StyleCompileOptions) but it returns a Promise resolving to StyleCompileResults. It can be used with async postcss plugins.

changelog

3.2.2 (2021-06-15)

Reverts

  • Revert "fix: patch postcss 7 and bundle it in the published npm package (#111)" (a9e3afd), closes #111

Bug Fixes

3.2.1 (2021-06-09)

Bug Fixes

  • patch postcss 7 and bundle it in the published npm package (#111) (cab504f)

3.2.0 (2020-07-22)

Features

  • asset handling for support vite dev. (#90) (ad83bdf)

3.1.2 (2020-04-08)

Bug Fixes

  • add parse options to cache key (#78) (f7b1f87)
  • LRUCache v5 has to be invoked with new (#83) (b541fef), closes #79

Others

  • make prettier dependency optional (aea1b79), closes #84

3.1.1 (2020-01-06)

Bug Fixes

  • upgrade postcss-selector-parser to ^6.0.2 (#76) (e566a19)

3.1.0 (2019-12-08)

Features

  • include filename in finalCompilerOptions (#74) (3dda72d)
  • support AST for template compile (#68) (ed44d6f)
  • support audio src in transformAssetUrls option by default (#72) (47f1341)

3.0.2 (2019-11-06)

Bug Fixes

  • also include "lib" folder for type definitions (dd42df1), closes #73

3.0.1 (2019-11-04)

Bug Fixes

  • should not crash when prettier failed (89e7900)
  • unpin prettier version (59a01bb)

3.0.0 (2019-04-11)

Features

BREAKING CHANGES

  • Using sass instead of node-sass package.

2.6.0 (2019-02-21)

Features

  • implement ::v-deep as a shadow piercing combinator (#54) (8b2c646)

2.6.0 (2019-02-21)

Features

  • implement ::v-deep as a shadow piercing combinator (#54) (8b2c646)

2.5.2 (2019-01-31)

Bug Fixes

  • fix sourceMap path separator on Windows, default sourceRoot to "" (#51) (df32cd9), closes #47
  • generate correct source-map when content is not padded (#52) (81be0ca)

2.5.1 (2019-01-25)

Bug Fixes

  • fix sourceMap path separator on Windows, default sourceRoot to "" (#51) (df32cd9), closes #47

2.5.0 (2019-01-08)

Features

  • add 'use' tag of SVG to 'transformAssetUrls' option as default (#45) (f4e3336)

2.4.0 (2019-01-02)

Bug Fixes

  • do not insert newline if style is already minified (2603ee2)
  • Forward preprocessor options to less (#25) (3b19c1e), closes #24
  • Move trim and scoped postcss plugins at the start of plugin list (#36) (0d52d86)
  • pin prettier version (5f138a6)
  • remove space after selector when inserting scoped attribute (5b299ed), closes vue-loader/#1370
  • should work with variable named render (close #23) (273827b)
  • support standalone pseudo element selectors (#33) (d6cfbbf)
  • Typings for SFCDescriptor and SFCCustomBlock (#29) (bb09115)

Features

  • scoped-css: support leading >>> or /deep/ in selectors (1a3b5bb)
  • add prettify option (#42) (db3655b)
  • Support stylus as <style> lang (#18) (986084e)

2.3.1 (2018-12-11)

Bug Fixes

  • do not insert newline if style is already minified (2603ee2)
  • Move trim and scoped postcss plugins at the start of plugin list (#36) (0d52d86)

2.3.0 (2018-10-22)

Bug Fixes

  • support standalone pseudo element selectors (#33) (d6cfbbf)
  • Typings for SFCDescriptor and SFCCustomBlock (#29) (bb09115)

2.2.0 (2018-08-16)

Features

  • scoped-css: support leading >>> or /deep/ in selectors (1a3b5bb)

2.1.2 (2018-08-09)

Bug Fixes

2.1.1 (2018-08-07)

Bug Fixes

2.1.0 (2018-07-03)

Bug Fixes

  • Forward preprocessor options to less (#25) (3b19c1e), closes #24
  • should work with variable named render (close #23) (273827b)

Features

2.0.0 (2018-06-03)

Features

  • Add async style compilation support (#13) (54464d6)
  • allow/require compiler to be passed in for parse (caa1538)

BREAKING CHANGES

  • vue template compiler must now be passed to parse via options.

1.3.1 (2018-05-28)

Bug Fixes

  • default parser was removed from prettier (#15) (598224d)

1.3.0 (2018-05-22)

Features

  • include href for <image> in transformAssetUrls (close #12) (86fddc2)
  • Provide installation instructions on missing language preprocessors (#10) (97e772c)

1.2.1 (2018-04-26)

Bug Fixes

1.2.0 (2018-04-26)

Bug Fixes

Features

  • accept postcss options and plugins (#7) (1456e3d)

1.1.0 (2018-04-24)

Bug Fixes

  • use more strict regex for matching css animation rules (4644727)

Features

  • adds stylus & less preprocessor (#5) (f2fd8b9)
  • preprocess scss/sass styles with node-sass (#4) (9204f16)