Skip to content

Commit caa1538

Browse files
committed
feat: allow/require compiler to be passed in for parse
BREAKING CHANGE: vue template compiler must now be passed to `parse` via options.
1 parent 9ce8522 commit caa1538

File tree

6 files changed

+46
-13
lines changed

6 files changed

+46
-13
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,26 @@ This package contains lower level utilities that you can use if you are writing
66

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

9+
## Why isn't `vue-template-compiler` a peerDependency?
10+
11+
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.
12+
13+
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.
14+
915
## API
1016

1117
### parse(ParseOptions): SFCDescriptor
1218

13-
Parse raw single file component source into a descriptor with source maps.
19+
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.
1420

1521
``` ts
1622
interface ParseOptions {
1723
source: string
1824
filename?: string
25+
compiler: VueTemplateCompiler
26+
// https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#compilerparsecomponentfile-options
27+
// defualt: { pad: 'line' }
28+
compilerParseOptions?: VueTemplateCompilerParseOptions
1929
sourceRoot?: string
2030
needMap?: boolean
2131
}
@@ -46,7 +56,7 @@ interface SFCBlock extends SFCCustomBlock {
4656

4757
### compileTemplate(TemplateCompileOptions): TemplateCompileResults
4858

49-
Takes raw template source and compile it into JavaScript code. The actual compiler (`vue-template-compiler`) must be passed so that the specific version used can be determined by the end user.
59+
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.
5060

5161
It can also optionally perform pre-processing for any templating engine supported by [consolidate](https://github.com/tj/consolidate.js/).
5262

@@ -55,8 +65,9 @@ interface TemplateCompileOptions {
5565
source: string
5666
filename: string
5767

58-
// See https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler
5968
compiler: VueTemplateCompiler
69+
https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#compilercompiletemplate-options
70+
// default: {}
6071
compilerOptions?: VueTemplateCompilerOptions
6172

6273
// Template preprocessor

lib/parse.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { RawSourceMap } from './types'
1+
import {
2+
RawSourceMap,
3+
VueTemplateCompiler,
4+
VueTemplateCompilerParseOptions
5+
} from './types'
26

37
const hash = require('hash-sum')
48
const cache = require('lru-cache')(100)
5-
const compiler = require('vue-template-compiler')
69
const { SourceMapGenerator } = require('source-map')
710

811
const splitRE = /\r?\n/g
@@ -11,6 +14,8 @@ const emptyRE = /^(?:\/\/)?\s*$/
1114
export interface ParseOptions {
1215
source: string
1316
filename?: string
17+
compiler: VueTemplateCompiler
18+
compilerParseOptions?: VueTemplateCompilerParseOptions
1419
sourceRoot?: string
1520
needMap?: boolean
1621
}
@@ -42,13 +47,15 @@ export function parse(options: ParseOptions): SFCDescriptor {
4247
const {
4348
source,
4449
filename = '',
50+
compiler,
51+
compilerParseOptions = { pad: 'line' },
4552
sourceRoot = process.cwd(),
4653
needMap = true
4754
} = options
4855
const cacheKey = hash(filename + source)
4956
let output: SFCDescriptor = cache.get(cacheKey)
5057
if (output) return output
51-
output = compiler.parseComponent(source, { pad: 'line' })
58+
output = compiler.parseComponent(source, compilerParseOptions)
5259
if (needMap) {
5360
if (output.script && !output.script.src) {
5461
output.script.map = generateSourceMap(

lib/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { SFCDescriptor } from './parse'
2+
13
export interface StartOfSourceMap {
24
file?: string;
35
sourceRoot?: string;
@@ -12,6 +14,11 @@ export interface RawSourceMap extends StartOfSourceMap {
1214
}
1315

1416
export interface VueTemplateCompiler {
17+
parseComponent(
18+
source: string,
19+
options?: any
20+
): SFCDescriptor
21+
1522
compile(
1623
template: string,
1724
options: VueTemplateCompilerOptions
@@ -30,6 +37,10 @@ export interface VueTemplateCompilerOptions {
3037
modules?: Object[]
3138
}
3239

40+
export interface VueTemplateCompilerParseOptions {
41+
pad?: 'line' | 'space'
42+
}
43+
3344
export interface VueTemplateCompilerResults {
3445
ast: Object | void
3546
render: string

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vue/component-compiler-utils",
3-
"version": "1.3.1",
3+
"version": "1.4.0",
44
"description": "Lower level utilities for compiling Vue single file components",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",
@@ -48,8 +48,5 @@
4848
"prettier": "^1.13.0",
4949
"source-map": "^0.5.6",
5050
"vue-template-es2015-compiler": "^1.6.0"
51-
},
52-
"peerDependencies": {
53-
"vue-template-compiler": "^2.0.0"
5451
}
5552
}

test/compileStyle.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { parse } from '../lib/parse'
22
import { compileStyle, compileStyleAsync } from '../lib/compileStyle'
3+
import * as compiler from 'vue-template-compiler'
34

45
test('preprocess less', () => {
56
const style = parse({
@@ -8,6 +9,7 @@ test('preprocess less', () => {
89
'@red: rgb(255, 0, 0);\n' +
910
'.color { color: @red; }\n' +
1011
'</style>\n',
12+
compiler,
1113
filename: 'example.vue',
1214
needMap: true
1315
}).styles[0]
@@ -32,6 +34,7 @@ test('preprocess scss', () => {
3234
'$red: rgb(255, 0, 0);\n' +
3335
'.color { color: $red; }\n' +
3436
'</style>\n',
37+
compiler,
3538
filename: 'example.vue',
3639
needMap: true
3740
}).styles[0]
@@ -57,6 +60,7 @@ test('preprocess sass', () => {
5760
'.color\n' +
5861
' color: $red\n' +
5962
'</style>\n',
63+
compiler,
6064
filename: 'example.vue',
6165
needMap: true
6266
}).styles[0]
@@ -82,6 +86,7 @@ test('preprocess stylus', () => {
8286
'.color\n' +
8387
' color: red-color\n' +
8488
'</style>\n',
89+
compiler,
8590
filename: 'example.vue',
8691
needMap: true
8792
}).styles[0]
@@ -101,7 +106,7 @@ test('preprocess stylus', () => {
101106

102107
test('custom postcss plugin', () => {
103108
const spy = jest.fn()
104-
109+
105110
compileStyle({
106111
id: 'v-scope-xxx',
107112
filename: 'example.vue',
@@ -148,7 +153,7 @@ test('async postcss plugin', async () => {
148153
})
149154

150155
expect(promise instanceof Promise).toBe(true)
151-
156+
152157
const result = await promise
153158
expect(result.errors).toHaveLength(0)
154159
expect(result.code).toEqual(expect.stringContaining('color: red'))

test/compileTemplate.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ test('preprocess pug', () => {
1111
' div.container\n' +
1212
' p Cool Pug example!\n' +
1313
'</template>\n',
14+
compiler,
1415
filename: 'example.vue',
1516
needMap: true
1617
}).template
@@ -30,6 +31,7 @@ test('warn missing preprocessor', () => {
3031
source:
3132
'<template lang="unknownLang">\n' +
3233
'</template>\n',
34+
compiler,
3335
filename: 'example.vue',
3436
needMap: true
3537
}).template
@@ -42,4 +44,4 @@ test('warn missing preprocessor', () => {
4244
})
4345

4446
expect(result.errors.length).toBe(1)
45-
})
47+
})

0 commit comments

Comments
 (0)