You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.0 KiB

1 month ago
  1. # @vue/component-compiler-utils [![Build Status](https://circleci.com/gh/vuejs/component-compiler-utils/tree/master.svg?style=shield)](https://circleci.com/gh/vuejs/component-compiler-utils/)
  2. > Lower level utilities for compiling Vue single file components
  3. 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](https://github.com/vuejs/vue-loader) version 15 and above.
  4. The API surface is intentionally minimal - the goal is to reuse as much as possible while being as flexible as possible.
  5. ## Why isn't `vue-template-compiler` a peerDependency?
  6. 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.
  7. 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.
  8. ## API
  9. ### parse(ParseOptions): SFCDescriptor
  10. 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.
  11. ``` ts
  12. interface ParseOptions {
  13. source: string
  14. filename?: string
  15. compiler: VueTemplateCompiler
  16. // https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#compilerparsecomponentfile-options
  17. // default: { pad: 'line' }
  18. compilerParseOptions?: VueTemplateCompilerParseOptions
  19. sourceRoot?: string
  20. needMap?: boolean
  21. }
  22. interface SFCDescriptor {
  23. template: SFCBlock | null
  24. script: SFCBlock | null
  25. styles: SFCBlock[]
  26. customBlocks: SFCCustomBlock[]
  27. }
  28. interface SFCCustomBlock {
  29. type: string
  30. content: string
  31. attrs: { [key: string]: string | true }
  32. start: number
  33. end: number
  34. map?: RawSourceMap
  35. }
  36. interface SFCBlock extends SFCCustomBlock {
  37. lang?: string
  38. src?: string
  39. scoped?: boolean
  40. module?: string | boolean
  41. }
  42. ```
  43. ### compileTemplate(TemplateCompileOptions): TemplateCompileResults
  44. 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.
  45. It can also optionally perform pre-processing for any templating engine supported by [consolidate](https://github.com/tj/consolidate.js/).
  46. ``` ts
  47. interface TemplateCompileOptions {
  48. source: string
  49. filename: string
  50. compiler: VueTemplateCompiler
  51. // https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#compilercompiletemplate-options
  52. // default: {}
  53. compilerOptions?: VueTemplateCompilerOptions
  54. // Template preprocessor
  55. preprocessLang?: string
  56. preprocessOptions?: any
  57. // Transform asset urls found in the template into `require()` calls
  58. // This is off by default. If set to true, the default value is
  59. // {
  60. // audio: 'src',
  61. // video: ['src', 'poster'],
  62. // source: 'src',
  63. // img: 'src',
  64. // image: ['xlink:href', 'href'],
  65. // use: ['xlink:href', 'href']
  66. // }
  67. transformAssetUrls?: AssetURLOptions | boolean
  68. // For vue-template-es2015-compiler, which is a fork of Buble
  69. transpileOptions?: any
  70. isProduction?: boolean // default: false
  71. isFunctional?: boolean // default: false
  72. optimizeSSR?: boolean // default: false
  73. // Whether prettify compiled render function or not (development only)
  74. // default: true
  75. prettify?: boolean
  76. }
  77. interface TemplateCompileResult {
  78. ast: Object | undefined
  79. code: string
  80. source: string
  81. tips: string[]
  82. errors: string[]
  83. }
  84. interface AssetURLOptions {
  85. [name: string]: string | string[]
  86. }
  87. ```
  88. #### Handling the Output
  89. The resulting JavaScript code will look like this:
  90. ``` js
  91. var render = function (h) { /* ... */}
  92. var staticRenderFns = [function (h) { /* ... */}, function (h) { /* ... */}]
  93. ```
  94. It **does NOT** assume any module system. It is your responsibility to handle the exports, if needed.
  95. ### compileStyle(StyleCompileOptions)
  96. 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.
  97. ``` ts
  98. interface StyleCompileOptions {
  99. source: string
  100. filename: string
  101. id: string
  102. map?: any
  103. scoped?: boolean
  104. trim?: boolean
  105. preprocessLang?: string
  106. preprocessOptions?: any
  107. postcssOptions?: any
  108. postcssPlugins?: any[]
  109. }
  110. interface StyleCompileResults {
  111. code: string
  112. map: any | void
  113. rawResult: LazyResult | void // raw lazy result from PostCSS
  114. errors: string[]
  115. }
  116. ```
  117. ### compileStyleAsync(StyleCompileOptions)
  118. Same as `compileStyle(StyleCompileOptions)` but it returns a Promise resolving to `StyleCompileResults`. It can be used with async postcss plugins.