Skip to content

Commit df18743

Browse files
committed
refactor(compiler): remove compiler-dom dependency
1 parent 819ead3 commit df18743

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1250
-848
lines changed

packages/compiler/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"dependencies": {
4343
"@babel/parser": "catalog:",
4444
"@babel/types": "catalog:",
45-
"@vue/compiler-dom": "catalog:",
4645
"@vue/shared": "catalog:",
4746
"ast-kit": "catalog:",
4847
"source-map-js": "catalog:"

packages/compiler/src/compile.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import { transformVSlot } from './transforms/vSlot'
2424
import { transformVSlots } from './transforms/vSlots'
2525
import { transformVText } from './transforms/vText'
2626
import type { ExpressionStatement, JSXElement, JSXFragment } from '@babel/types'
27-
import type { CompilerOptions as BaseCompilerOptions } from '@vue/compiler-dom'
2827

2928
// code/AST -> IR (transform) -> JS (generate)
3029
export function compile(
@@ -85,8 +84,7 @@ export function compile(
8584
return generate(ir, resolvedOptions)
8685
}
8786

88-
export type CompilerOptions = HackOptions<BaseCompilerOptions> &
89-
TransformOptions
87+
export type CompilerOptions = HackOptions<TransformOptions>
9088
export type TransformPreset = [
9189
NodeTransform[],
9290
Record<string, DirectiveTransform>,

packages/compiler/src/generate.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,34 @@ import {
88
INDENT_END,
99
INDENT_START,
1010
NEWLINE,
11-
} from './generators/utils'
11+
} from './utils/generate'
1212
import type { BlockIRNode, RootIRNode } from './ir'
13-
import type {
14-
CodegenOptions as BaseCodegenOptions,
15-
BaseCodegenResult,
16-
SimpleExpressionNode,
17-
} from '@vue/compiler-dom'
18-
19-
export type CodegenOptions = Omit<
20-
BaseCodegenOptions,
21-
'optimizeImports' | 'inline' | 'bindingMetadata' | 'prefixIdentifiers'
22-
> & {
13+
import type { SimpleExpressionNode } from './utils'
14+
import type { ParserPlugin } from '@babel/parser'
15+
import type { RawSourceMap } from 'source-map-js'
16+
17+
export type CodegenOptions = {
18+
/**
19+
* Generate source map?
20+
* @default false
21+
*/
22+
sourceMap?: boolean
23+
/**
24+
* Filename for source map generation.
25+
* Also used for self-recursive reference in templates
26+
* @default 'index.jsx'
27+
*/
28+
filename?: string
29+
/**
30+
* Indicates that transforms and codegen should try to output valid TS code
31+
*/
32+
isTS?: boolean
33+
/**
34+
* A list of parser plugins to enable for `@babel/parser`, which is used to
35+
* parse expressions in bindings and interpolations.
36+
* https://babeljs.io/docs/en/next/babel-parser#plugins
37+
*/
38+
expressionPlugins?: ParserPlugin[]
2339
templates?: string[]
2440
}
2541

@@ -73,30 +89,24 @@ export class CodegenContext {
7389
options: CodegenOptions,
7490
) {
7591
const defaultOptions: Required<CodegenOptions> = {
76-
mode: 'module',
7792
sourceMap: false,
78-
filename: `template.vue.html`,
79-
scopeId: null,
80-
runtimeGlobalName: `Vue`,
81-
runtimeModuleName: `vue`,
82-
ssrRuntimeModuleName: 'vue/server-renderer',
83-
ssr: false,
93+
filename: `index.jsx`,
8494
isTS: false,
85-
inSSR: false,
95+
expressionPlugins: ['jsx'],
8696
templates: [],
87-
expressionPlugins: [],
8897
}
8998
this.options = extend(defaultOptions, options)
9099
this.block = ir.block
91100
}
92101
}
93102

94-
export interface VaporCodegenResult
95-
extends Omit<BaseCodegenResult, 'preamble'> {
103+
export interface VaporCodegenResult {
96104
ast: RootIRNode
97105
helpers: Set<string>
98106
templates: string[]
99107
delegates: Set<string>
108+
code: string
109+
map?: RawSourceMap
100110
}
101111

102112
// IR -> JS codegen

packages/compiler/src/generators/block.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import { toValidAssetId } from '@vue/compiler-dom'
2-
import type { CodegenContext } from '../generate'
3-
import type { BlockIRNode } from '../ir'
4-
import { genEffects, genOperations } from './operation'
5-
import { genChildren, genSelf } from './template'
61
import {
72
buildCodeFragment,
83
DELIMITERS_ARRAY,
@@ -11,8 +6,13 @@ import {
116
INDENT_END,
127
INDENT_START,
138
NEWLINE,
9+
toValidAssetId,
1410
type CodeFragment,
15-
} from './utils'
11+
} from '../utils'
12+
import type { CodegenContext } from '../generate'
13+
import type { BlockIRNode } from '../ir'
14+
import { genEffects, genOperations } from './operation'
15+
import { genChildren, genSelf } from './template'
1616

1717
export function genBlock(
1818
oper: BlockIRNode,

packages/compiler/src/generators/component.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { createSimpleExpression, toValidAssetId } from '@vue/compiler-dom'
21
import { camelize, extend, isArray } from '@vue/shared'
32
import { walkIdentifiers } from 'ast-kit'
43
import {
@@ -16,13 +15,8 @@ import {
1615
type IRSlotsStatic,
1716
type SlotBlockIRNode,
1817
} from '../ir'
19-
import type { CodegenContext } from '../generate'
20-
import { genBlock } from './block'
21-
import { genDirectiveModifiers, genDirectivesForElement } from './directive'
22-
import { genEventHandler } from './event'
23-
import { genExpression } from './expression'
24-
import { genPropKey, genPropValue } from './prop'
2518
import {
19+
createSimpleExpression,
2620
DELIMITERS_ARRAY_NEWLINE,
2721
DELIMITERS_OBJECT,
2822
DELIMITERS_OBJECT_NEWLINE,
@@ -31,8 +25,15 @@ import {
3125
INDENT_END,
3226
INDENT_START,
3327
NEWLINE,
28+
toValidAssetId,
3429
type CodeFragment,
35-
} from './utils'
30+
} from '../utils'
31+
import type { CodegenContext } from '../generate'
32+
import { genBlock } from './block'
33+
import { genDirectiveModifiers, genDirectivesForElement } from './directive'
34+
import { genEventHandler } from './event'
35+
import { genExpression } from './expression'
36+
import { genPropKey, genPropValue } from './prop'
3637
import { genModelHandler } from './vModel'
3738

3839
export function genCreateComponent(

packages/compiler/src/generators/directive.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import {
2-
createSimpleExpression,
3-
isSimpleIdentifier,
4-
toValidAssetId,
5-
} from '@vue/compiler-dom'
61
import { extend } from '@vue/shared'
72
import { IRNodeTypes, type DirectiveIRNode, type OperationNode } from '../ir'
8-
import type { CodegenContext } from '../generate'
9-
import { genExpression } from './expression'
103
import {
4+
createSimpleExpression,
115
DELIMITERS_ARRAY,
126
genCall,
137
genMulti,
8+
isSimpleIdentifier,
149
NEWLINE,
10+
toValidAssetId,
1511
type CodeFragment,
1612
type CodeFragmentDelimiters,
17-
} from './utils'
13+
} from '../utils'
14+
import type { CodegenContext } from '../generate'
15+
import { genExpression } from './expression'
1816
import { genVModel } from './vModel'
1917
import { genVShow } from './vShow'
2018

packages/compiler/src/generators/dom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { genCall, NEWLINE, type CodeFragment } from '../utils'
12
import type { CodegenContext } from '../generate'
23
import type { InsertNodeIRNode, PrependNodeIRNode } from '../ir'
3-
import { genCall, NEWLINE, type CodeFragment } from './utils'
44

55
export function genInsertNode(
66
{ parent, elements, anchor }: InsertNodeIRNode,

packages/compiler/src/generators/event.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
import {
2-
isFnExpression,
3-
isMemberExpression,
4-
type SimpleExpressionNode,
5-
} from '@vue/compiler-dom'
61
import {
72
IRNodeTypes,
83
type OperationNode,
94
type SetDynamicEventsIRNode,
105
type SetEventIRNode,
116
} from '../ir'
12-
import type { CodegenContext } from '../generate'
13-
import { genExpression } from './expression'
147
import {
158
DELIMITERS_OBJECT_NEWLINE,
169
genCall,
1710
genMulti,
11+
isFnExpression,
12+
isMemberExpression,
1813
NEWLINE,
1914
type CodeFragment,
20-
} from './utils'
15+
type SimpleExpressionNode,
16+
} from '../utils'
17+
import type { CodegenContext } from '../generate'
18+
import { genExpression } from './expression'
2119

2220
export function genSetEvent(
2321
oper: SetEventIRNode,
@@ -118,7 +116,7 @@ export function genEventHandler(
118116
if (value && value.content.trim()) {
119117
// Determine how the handler should be wrapped so it always reference the
120118
// latest value when invoked.
121-
if (isMemberExpression(value, context.options)) {
119+
if (isMemberExpression(value)) {
122120
// e.g. @click="foo.bar"
123121
handlerExp = genExpression(value, context)
124122
if (!extraWrap) {
@@ -127,7 +125,7 @@ export function genEventHandler(
127125
// can skip this
128126
handlerExp = [`e => `, ...handlerExp, `(e)`]
129127
}
130-
} else if (isFnExpression(value, context.options)) {
128+
} else if (isFnExpression(value)) {
131129
// Fn expression: @click="e => foo(e)"
132130
// no need to wrap in this case
133131
handlerExp = genExpression(value, context)

packages/compiler/src/generators/expression.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1+
import { isString } from '@vue/shared'
2+
import { walkIdentifiers } from 'ast-kit'
13
import {
24
advancePositionWithClone,
5+
buildCodeFragment,
6+
isConstantExpression,
37
isStaticProperty,
48
NewlineType,
59
TS_NODE_TYPES,
10+
type CodeFragment,
611
type SimpleExpressionNode,
7-
type SourceLocation,
8-
} from '@vue/compiler-dom'
9-
import { isString } from '@vue/shared'
10-
import { walkIdentifiers } from 'ast-kit'
11-
import { isConstantExpression } from '../utils'
12+
} from '../utils'
1213
import type { CodegenContext } from '../generate'
13-
import { buildCodeFragment, type CodeFragment } from './utils'
14-
import type { Identifier, Node } from '@babel/types'
14+
import type { Identifier, Node, SourceLocation } from '@babel/types'
1515

1616
export function genExpression(
1717
node: SimpleExpressionNode,
1818
context: CodegenContext,
1919
assignment?: string,
20+
needWrap = false,
2021
): CodeFragment[] {
21-
const { content, ast, isStatic, loc } = node
22+
let { content, ast, isStatic, loc } = node
23+
if (needWrap) {
24+
content = `() => (${content})`
25+
}
2226

2327
if (isStatic) {
2428
return [[JSON.stringify(content), NewlineType.None, loc]]
@@ -55,8 +59,7 @@ export function genExpression(
5559
if (ids.length) {
5660
const [frag, push] = buildCodeFragment()
5761
const isTSNode = ast && TS_NODE_TYPES.includes(ast.type)
58-
const offset =
59-
(ast?.start ? ast.start - 1 : 0) - ((ast as any)._offset || 0)
62+
const offset = (ast?.start ? ast.start - 1 : 0) - (needWrap ? 7 : 0)
6063
ids
6164
.sort((a, b) => a.start! - b.start!)
6265
.forEach((id, i) => {
@@ -87,9 +90,10 @@ export function genExpression(
8790
source,
8891
context,
8992
{
90-
start: advancePositionWithClone(node.loc.start, source, start),
91-
end: advancePositionWithClone(node.loc.start, source, end),
92-
source,
93+
start: advancePositionWithClone(node.loc!.start, source, start),
94+
end: advancePositionWithClone(node.loc!.start, source, end),
95+
filename: '',
96+
identifierName: undefined,
9397
},
9498
hasMemberExpression ? undefined : assignment,
9599
parent,
@@ -113,7 +117,7 @@ export function genExpression(
113117
function genIdentifier(
114118
raw: string,
115119
context: CodegenContext,
116-
loc?: SourceLocation,
120+
loc?: SourceLocation | null,
117121
assignment?: string,
118122
parent?: Node,
119123
): CodeFragment[] {

0 commit comments

Comments
 (0)