@vue/component-compiler-utils

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 {
: string
source?: string
filename: VueTemplateCompiler
compiler// https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#compilerparsecomponentfile-options
// default: { pad: 'line' }
?: VueTemplateCompilerParseOptions
compilerParseOptions?: string
sourceRoot?: boolean
needMap
}
interface SFCDescriptor {
: SFCBlock | null
template: SFCBlock | null
script: SFCBlock[]
styles: SFCCustomBlock[]
customBlocks
}
interface SFCCustomBlock {
: string
type: string
content: { [key: string]: string | true }
attrs: number
start: number
end?: RawSourceMap
map
}
interface SFCBlock extends SFCCustomBlock {
?: string
lang?: string
src?: boolean
scoped?: string | boolean
module }
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 {
: string
source: string
filename
: VueTemplateCompiler
compiler// https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#compilercompiletemplate-options
// default: {}
?: VueTemplateCompilerOptions
compilerOptions
// Template preprocessor
?: string
preprocessLang?: any
preprocessOptions
// 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']
// }
?: AssetURLOptions | boolean
transformAssetUrls
// For vue-template-es2015-compiler, which is a fork of Buble
?: any
transpileOptions
?: boolean // default: false
isProduction?: boolean // default: false
isFunctional?: boolean // default: false
optimizeSSR
// Whether prettify compiled render function or not (development only)
// default: true
?: boolean
prettify
}
interface TemplateCompileResult {
: Object | undefined
ast: string
code: string
source: string[]
tips: string[]
errors
}
interface AssetURLOptions {
: string]: string | string[]
[name }
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 {
: string
source: string
filename: string
id?: any
map?: boolean
scoped?: boolean
trim?: string
preprocessLang?: any
preprocessOptions?: any
postcssOptions?: any[]
postcssPlugins
}
interface StyleCompileResults {
: string
code: any | void
map: LazyResult | void // raw lazy result from PostCSS
rawResult: string[]
errors }
compileStyleAsync(StyleCompileOptions)
Same as compileStyle(StyleCompileOptions)
but it returns
a Promise resolving to StyleCompileResults
. It can be used
with async postcss plugins.