feat(compiler): scaffold compiler-dom

This commit is contained in:
Evan You 2019-09-16 15:06:45 -04:00
parent c9a1b00c1e
commit 1c8f5b612a
10 changed files with 103 additions and 19 deletions

View File

@ -0,0 +1,5 @@
export function assert(condition: boolean, msg?: string) {
if (!condition) {
throw new Error(msg || `unexpected parser condition`)
}
}

View File

@ -1,4 +1,4 @@
import assert from 'assert' import { assert } from './assert'
import { ParserErrorTypes } from './errorTypes' import { ParserErrorTypes } from './errorTypes'
import { import {
Node, Node,
@ -69,7 +69,7 @@ function startsWith(source: string, searchString: string): boolean {
} }
function advanceBy(context: ParserContext, numberOfCharacters: number): void { function advanceBy(context: ParserContext, numberOfCharacters: number): void {
assert(numberOfCharacters <= context.source.length) __DEV__ && assert(numberOfCharacters <= context.source.length)
const { column, source } = context const { column, source } = context
const str = source.slice(0, numberOfCharacters) const str = source.slice(0, numberOfCharacters)
@ -175,7 +175,7 @@ function parseChildren(
const nodes: RootNode['children'] = [] const nodes: RootNode['children'] = []
while (!isEnd(context, mode, ancestors)) { while (!isEnd(context, mode, ancestors)) {
assert(context.source.length > 0) __DEV__ && assert(context.source.length > 0)
const s = context.source const s = context.source
let node: any = null let node: any = null
@ -332,15 +332,16 @@ function parseCDATA(
context: ParserContext, context: ParserContext,
ancestors: ElementNode[] ancestors: ElementNode[]
): RootNode['children'] { ): RootNode['children'] {
__DEV__ &&
assert(last(ancestors) == null || last(ancestors)!.ns !== Namespaces.HTML) assert(last(ancestors) == null || last(ancestors)!.ns !== Namespaces.HTML)
assert(startsWith(context.source, '<![CDATA[')) __DEV__ && assert(startsWith(context.source, '<![CDATA['))
advanceBy(context, 9) advanceBy(context, 9)
const nodes = parseChildren(context, TextModes.CDATA, ancestors) const nodes = parseChildren(context, TextModes.CDATA, ancestors)
if (context.source.length === 0) { if (context.source.length === 0) {
emitError(context, ParserErrorTypes.EOF_IN_CDATA) emitError(context, ParserErrorTypes.EOF_IN_CDATA)
} else { } else {
assert(startsWith(context.source, ']]>')) __DEV__ && assert(startsWith(context.source, ']]>'))
advanceBy(context, 3) advanceBy(context, 3)
} }
@ -348,7 +349,7 @@ function parseCDATA(
} }
function parseComment(context: ParserContext): CommentNode { function parseComment(context: ParserContext): CommentNode {
assert(startsWith(context.source, '<!--')) __DEV__ && assert(startsWith(context.source, '<!--'))
const start = getCursor(context) const start = getCursor(context)
let content: string let content: string
@ -390,7 +391,7 @@ function parseComment(context: ParserContext): CommentNode {
} }
function parseBogusComment(context: ParserContext): CommentNode | undefined { function parseBogusComment(context: ParserContext): CommentNode | undefined {
assert(/^<(?:[\!\?]|\/[^a-z>])/i.test(context.source)) __DEV__ && assert(/^<(?:[\!\?]|\/[^a-z>])/i.test(context.source))
const start = getCursor(context) const start = getCursor(context)
const contentStart = context.source[1] === '?' ? 1 : 2 const contentStart = context.source[1] === '?' ? 1 : 2
@ -416,7 +417,7 @@ function parseElement(
context: ParserContext, context: ParserContext,
ancestors: ElementNode[] ancestors: ElementNode[]
): ElementNode | undefined { ): ElementNode | undefined {
assert(/^<[a-z]/i.test(context.source)) __DEV__ && assert(/^<[a-z]/i.test(context.source))
// Start tag. // Start tag.
const parent = last(ancestors) const parent = last(ancestors)
@ -470,7 +471,8 @@ function parseTag(
type: TagType, type: TagType,
parent: ElementNode | undefined parent: ElementNode | undefined
): ElementNode { ): ElementNode {
assert(/^<\/?[a-z]/i.test(context.source)) __DEV__ && assert(/^<\/?[a-z]/i.test(context.source))
__DEV__ &&
assert( assert(
type === (startsWith(context.source, '</') ? TagType.End : TagType.Start) type === (startsWith(context.source, '</') ? TagType.End : TagType.Start)
) )
@ -547,7 +549,7 @@ function parseAttribute(
context: ParserContext, context: ParserContext,
nameSet: Set<string> nameSet: Set<string>
): AttributeNode | DirectiveNode { ): AttributeNode | DirectiveNode {
assert(/^[^\t\r\n\f />]/.test(context.source)) __DEV__ && assert(/^[^\t\r\n\f />]/.test(context.source))
// Name. // Name.
const start = getCursor(context) const start = getCursor(context)
@ -712,7 +714,7 @@ function parseInterpolation(
mode: TextModes mode: TextModes
): ExpressionNode | undefined { ): ExpressionNode | undefined {
const [open, close] = context.delimiters const [open, close] = context.delimiters
assert(startsWith(context.source, open)) __DEV__ && assert(startsWith(context.source, open))
const closeIndex = context.source.indexOf(close, open.length) const closeIndex = context.source.indexOf(close, open.length)
if (closeIndex === -1) { if (closeIndex === -1) {
@ -734,7 +736,7 @@ function parseInterpolation(
} }
function parseText(context: ParserContext, mode: TextModes): TextNode { function parseText(context: ParserContext, mode: TextModes): TextNode {
assert(context.source.length > 0) __DEV__ && assert(context.source.length > 0)
const [open] = context.delimiters const [open] = context.delimiters
const endIndex = Math.min( const endIndex = Math.min(
@ -745,7 +747,7 @@ function parseText(context: ParserContext, mode: TextModes): TextNode {
context.source.length context.source.length
].filter(n => n !== -1) ].filter(n => n !== -1)
) )
assert(endIndex > 0) __DEV__ && assert(endIndex > 0)
const start = getCursor(context) const start = getCursor(context)
const content = parseTextData(context, endIndex, mode) const content = parseTextData(context, endIndex, mode)

View File

@ -0,0 +1 @@
# @vue/compiler-dom

View File

@ -0,0 +1,7 @@
{
"extends": "../../api-extractor.json",
"mainEntryPointFilePath": "./dist/packages/<unscopedPackageName>/src/index.d.ts",
"dtsRollup": {
"untrimmedFilePath": "./dist/<unscopedPackageName>.d.ts"
}
}

View File

@ -0,0 +1,7 @@
'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./dist/compiler-dom.cjs.prod.js')
} else {
module.exports = require('./dist/compiler-dom.cjs.js')
}

View File

@ -0,0 +1,34 @@
{
"name": "@vue/compiler-dom",
"version": "3.0.0-alpha.1",
"description": "@vue/compiler-dom",
"main": "index.js",
"module": "dist/compiler-dom.esm-bundler.js",
"files": [
"index.js",
"dist"
],
"types": "dist/compiler-dom.d.ts",
"unpkg": "dist/compiler-dom/global.js",
"sideEffects": false,
"buildOptions": {
"name": "VueDOMCompiler",
"formats": ["esm", "cjs", "global", "esm-browser"]
},
"repository": {
"type": "git",
"url": "git+https://github.com/vuejs/vue.git"
},
"keywords": [
"vue"
],
"author": "Evan You",
"license": "MIT",
"bugs": {
"url": "https://github.com/vuejs/vue/issues"
},
"homepage": "https://github.com/vuejs/vue/tree/dev/packages/compiler-dom#readme",
"dependencies": {
"@vue/compiler-core": "3.0.0-alpha.1"
}
}

View File

@ -0,0 +1,2 @@
// TODO
export * from '@vue/compiler-core'

View File

@ -1,5 +1,10 @@
// TODO this package will be the "full-build" that includes both the runtime // This package is the "full-build" that includes both the runtime
// and the compiler // and the compiler. For now we are just exporting everything from the runtome
// AND the compiler.
// TODO hook up the runtime to compile templates on the fly
export * from '@vue/compiler-dom'
export * from '@vue/runtime-dom' export * from '@vue/runtime-dom'
if (__FEATURE_PRODUCTION_TIP__) { if (__FEATURE_PRODUCTION_TIP__) {

20
scripts/bootstrap.js vendored
View File

@ -47,6 +47,26 @@ files.forEach(shortName => {
fs.writeFileSync(readmePath, `# ${name}`) fs.writeFileSync(readmePath, `# ${name}`)
} }
const apiExtractorConfigPath = path.join(
packagesDir,
shortName,
`api-extractor.json`
)
if (args.force || !fs.existsSync(apiExtractorConfigPath)) {
fs.writeFileSync(
apiExtractorConfigPath,
`
{
"extends": "../../api-extractor.json",
"mainEntryPointFilePath": "./dist/packages/<unscopedPackageName>/src/index.d.ts",
"dtsRollup": {
"untrimmedFilePath": "./dist/<unscopedPackageName>.d.ts"
}
}
`.trim()
)
}
const srcDir = path.join(packagesDir, shortName, `src`) const srcDir = path.join(packagesDir, shortName, `src`)
const indexPath = path.join(packagesDir, shortName, `src/index.ts`) const indexPath = path.join(packagesDir, shortName, `src/index.ts`)
if (args.force || !fs.existsSync(indexPath)) { if (args.force || !fs.existsSync(indexPath)) {

View File

@ -24,6 +24,7 @@
"@vue/runtime-test": ["packages/runtime-test/src"], "@vue/runtime-test": ["packages/runtime-test/src"],
"@vue/reactivity": ["packages/reactivity/src"], "@vue/reactivity": ["packages/reactivity/src"],
"@vue/compiler-core": ["packages/compiler-core/src"], "@vue/compiler-core": ["packages/compiler-core/src"],
"@vue/compiler-dom": ["packages/compiler-dom/src"],
"@vue/server-renderer": ["packages/server-renderer/src"] "@vue/server-renderer": ["packages/server-renderer/src"]
} }
}, },