refactor: useWith -> prefixIdentifiers
This commit is contained in:
parent
e57cb51066
commit
88e5e96a3e
@ -9,7 +9,7 @@ test(`should work`, async () => {
|
|||||||
<p>{{ i }}</p>
|
<p>{{ i }}</p>
|
||||||
`,
|
`,
|
||||||
{
|
{
|
||||||
useWith: false
|
prefixIdentifiers: true
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
console.log(code)
|
console.log(code)
|
||||||
|
@ -26,7 +26,7 @@ export interface CodegenOptions {
|
|||||||
// runtime helpers; otherwise will grab the helpers from global `Vue`.
|
// runtime helpers; otherwise will grab the helpers from global `Vue`.
|
||||||
// default: false
|
// default: false
|
||||||
mode?: 'module' | 'function'
|
mode?: 'module' | 'function'
|
||||||
useWith?: boolean
|
prefixIdentifiers?: boolean
|
||||||
// Filename for source map generation.
|
// Filename for source map generation.
|
||||||
filename?: string
|
filename?: string
|
||||||
}
|
}
|
||||||
@ -54,13 +54,13 @@ function createCodegenContext(
|
|||||||
ast: RootNode,
|
ast: RootNode,
|
||||||
{
|
{
|
||||||
mode = 'function',
|
mode = 'function',
|
||||||
useWith = true,
|
prefixIdentifiers = false,
|
||||||
filename = `template.vue.html`
|
filename = `template.vue.html`
|
||||||
}: CodegenOptions
|
}: CodegenOptions
|
||||||
): CodegenContext {
|
): CodegenContext {
|
||||||
const context: CodegenContext = {
|
const context: CodegenContext = {
|
||||||
mode,
|
mode,
|
||||||
useWith,
|
prefixIdentifiers,
|
||||||
filename,
|
filename,
|
||||||
source: ast.loc.source,
|
source: ast.loc.source,
|
||||||
code: ``,
|
code: ``,
|
||||||
@ -119,7 +119,7 @@ export function generate(
|
|||||||
options: CodegenOptions = {}
|
options: CodegenOptions = {}
|
||||||
): CodegenResult {
|
): CodegenResult {
|
||||||
const context = createCodegenContext(ast, options)
|
const context = createCodegenContext(ast, options)
|
||||||
const { mode, push, useWith, indent, deindent, newline } = context
|
const { mode, push, prefixIdentifiers, indent, deindent, newline } = context
|
||||||
const imports = ast.imports.join(', ')
|
const imports = ast.imports.join(', ')
|
||||||
if (mode === 'function') {
|
if (mode === 'function') {
|
||||||
// generate const declarations for helpers
|
// generate const declarations for helpers
|
||||||
@ -144,13 +144,13 @@ export function generate(
|
|||||||
})
|
})
|
||||||
newline()
|
newline()
|
||||||
}
|
}
|
||||||
if (useWith) {
|
if (!prefixIdentifiers) {
|
||||||
push(`with (this) {`)
|
push(`with (this) {`)
|
||||||
indent()
|
indent()
|
||||||
}
|
}
|
||||||
push(`return `)
|
push(`return `)
|
||||||
genChildren(ast.children, context)
|
genChildren(ast.children, context)
|
||||||
if (useWith) {
|
if (!prefixIdentifiers) {
|
||||||
deindent()
|
deindent()
|
||||||
push(`}`)
|
push(`}`)
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,7 @@ export const enum ErrorCodes {
|
|||||||
X_V_BIND_NO_EXPRESSION,
|
X_V_BIND_NO_EXPRESSION,
|
||||||
|
|
||||||
// generic errors
|
// generic errors
|
||||||
X_STRIP_WITH_NOT_SUPPORTED
|
X_PREFIX_ID_NOT_SUPPORTED
|
||||||
}
|
}
|
||||||
|
|
||||||
export const errorMessages: { [code: number]: string } = {
|
export const errorMessages: { [code: number]: string } = {
|
||||||
@ -135,5 +135,5 @@ export const errorMessages: { [code: number]: string } = {
|
|||||||
[ErrorCodes.X_FOR_MALFORMED_EXPRESSION]: `v-for has invalid expression`,
|
[ErrorCodes.X_FOR_MALFORMED_EXPRESSION]: `v-for has invalid expression`,
|
||||||
|
|
||||||
// generic errors
|
// generic errors
|
||||||
[ErrorCodes.X_STRIP_WITH_NOT_SUPPORTED]: `useWith: false is not supported in this build of compiler because it is optimized for payload size.`
|
[ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler because it is optimized for payload size.`
|
||||||
}
|
}
|
||||||
|
@ -18,21 +18,21 @@ export function compile(
|
|||||||
options: CompilerOptions = {}
|
options: CompilerOptions = {}
|
||||||
): CodegenResult {
|
): CodegenResult {
|
||||||
const ast = isString(template) ? parse(template, options) : template
|
const ast = isString(template) ? parse(template, options) : template
|
||||||
const useWith = __BROWSER__ || options.useWith !== false
|
const prefixIdentifiers = !__BROWSER__ && options.prefixIdentifiers === true
|
||||||
|
|
||||||
if (__BROWSER__ && options.useWith === false) {
|
if (__BROWSER__ && options.prefixIdentifiers === false) {
|
||||||
;(options.onError || defaultOnError)(
|
;(options.onError || defaultOnError)(
|
||||||
createCompilerError(ErrorCodes.X_STRIP_WITH_NOT_SUPPORTED)
|
createCompilerError(ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
transform(ast, {
|
transform(ast, {
|
||||||
...options,
|
...options,
|
||||||
useWith,
|
prefixIdentifiers,
|
||||||
nodeTransforms: [
|
nodeTransforms: [
|
||||||
transformIf,
|
transformIf,
|
||||||
transformFor,
|
transformFor,
|
||||||
...(useWith ? [] : [expressionTransform]),
|
...(prefixIdentifiers ? [expressionTransform] : []),
|
||||||
prepareElementForCodegen,
|
prepareElementForCodegen,
|
||||||
...(options.nodeTransforms || []) // user transforms
|
...(options.nodeTransforms || []) // user transforms
|
||||||
],
|
],
|
||||||
|
@ -43,7 +43,7 @@ export type StructuralDirectiveTransform = (
|
|||||||
export interface TransformOptions {
|
export interface TransformOptions {
|
||||||
nodeTransforms?: NodeTransform[]
|
nodeTransforms?: NodeTransform[]
|
||||||
directiveTransforms?: { [name: string]: DirectiveTransform }
|
directiveTransforms?: { [name: string]: DirectiveTransform }
|
||||||
useWith?: boolean
|
prefixIdentifiers?: boolean
|
||||||
onError?: (error: CompilerError) => void
|
onError?: (error: CompilerError) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ export interface TransformContext extends Required<TransformOptions> {
|
|||||||
function createTransformContext(
|
function createTransformContext(
|
||||||
root: RootNode,
|
root: RootNode,
|
||||||
{
|
{
|
||||||
useWith = true,
|
prefixIdentifiers = false,
|
||||||
nodeTransforms = [],
|
nodeTransforms = [],
|
||||||
directiveTransforms = {},
|
directiveTransforms = {},
|
||||||
onError = defaultOnError
|
onError = defaultOnError
|
||||||
@ -75,7 +75,7 @@ function createTransformContext(
|
|||||||
imports: new Set(),
|
imports: new Set(),
|
||||||
statements: [],
|
statements: [],
|
||||||
identifiers: {},
|
identifiers: {},
|
||||||
useWith,
|
prefixIdentifiers,
|
||||||
nodeTransforms,
|
nodeTransforms,
|
||||||
directiveTransforms,
|
directiveTransforms,
|
||||||
onError,
|
onError,
|
||||||
|
@ -87,7 +87,7 @@ function parseForExpression(
|
|||||||
RHS.trim(),
|
RHS.trim(),
|
||||||
source.indexOf(RHS, LHS.length),
|
source.indexOf(RHS, LHS.length),
|
||||||
context,
|
context,
|
||||||
!context.useWith
|
context.prefixIdentifiers
|
||||||
),
|
),
|
||||||
value: undefined,
|
value: undefined,
|
||||||
key: undefined,
|
key: undefined,
|
||||||
|
@ -15,7 +15,7 @@ import { processExpression } from './expression'
|
|||||||
export const transformIf = createStructuralDirectiveTransform(
|
export const transformIf = createStructuralDirectiveTransform(
|
||||||
/^(if|else|else-if)$/,
|
/^(if|else|else-if)$/,
|
||||||
(node, dir, context) => {
|
(node, dir, context) => {
|
||||||
if (!__BROWSER__ && !context.useWith && dir.exp) {
|
if (!__BROWSER__ && context.prefixIdentifiers && dir.exp) {
|
||||||
processExpression(dir.exp, context)
|
processExpression(dir.exp, context)
|
||||||
}
|
}
|
||||||
if (dir.name === 'if') {
|
if (dir.name === 'if') {
|
||||||
|
Loading…
Reference in New Issue
Block a user