refactor(compiler-core): move compile into separate file
This commit is contained in:
		
							parent
							
								
									8277d131c4
								
							
						
					
					
						commit
						9e757b5cc5
					
				@ -84,13 +84,7 @@ function createCodegenContext(
 | 
				
			|||||||
    line: 1,
 | 
					    line: 1,
 | 
				
			||||||
    offset: 0,
 | 
					    offset: 0,
 | 
				
			||||||
    indentLevel: 0,
 | 
					    indentLevel: 0,
 | 
				
			||||||
 | 
					    map: undefined,
 | 
				
			||||||
    // lazy require source-map implementation, only in non-browser builds!
 | 
					 | 
				
			||||||
    map:
 | 
					 | 
				
			||||||
      __BROWSER__ || !sourceMap
 | 
					 | 
				
			||||||
        ? undefined
 | 
					 | 
				
			||||||
        : new (loadDep('source-map')).SourceMapGenerator(),
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    helper(key) {
 | 
					    helper(key) {
 | 
				
			||||||
      const name = helperNameMap[key]
 | 
					      const name = helperNameMap[key]
 | 
				
			||||||
      return prefixIdentifiers ? name : `_${name}`
 | 
					      return prefixIdentifiers ? name : `_${name}`
 | 
				
			||||||
@ -148,9 +142,12 @@ function createCodegenContext(
 | 
				
			|||||||
    })
 | 
					    })
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (!__BROWSER__ && context.map) {
 | 
					  if (!__BROWSER__ && sourceMap) {
 | 
				
			||||||
    context.map.setSourceContent(filename, context.source)
 | 
					    // lazy require source-map implementation, only in non-browser builds
 | 
				
			||||||
 | 
					    context.map = new (loadDep('source-map')).SourceMapGenerator()
 | 
				
			||||||
 | 
					    context.map!.setSourceContent(filename, context.source)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return context
 | 
					  return context
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										74
									
								
								packages/compiler-core/src/compile.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								packages/compiler-core/src/compile.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,74 @@
 | 
				
			|||||||
 | 
					import { CompilerOptions } from './options'
 | 
				
			||||||
 | 
					import { parse } from './parse'
 | 
				
			||||||
 | 
					import { transform } from './transform'
 | 
				
			||||||
 | 
					import { generate, CodegenResult } from './codegen'
 | 
				
			||||||
 | 
					import { RootNode } from './ast'
 | 
				
			||||||
 | 
					import { isString } from '@vue/shared'
 | 
				
			||||||
 | 
					import { transformIf } from './transforms/vIf'
 | 
				
			||||||
 | 
					import { transformFor } from './transforms/vFor'
 | 
				
			||||||
 | 
					import { transformExpression } from './transforms/transformExpression'
 | 
				
			||||||
 | 
					import { transformSlotOutlet } from './transforms/transformSlotOutlet'
 | 
				
			||||||
 | 
					import { transformElement } from './transforms/transformElement'
 | 
				
			||||||
 | 
					import { transformOn } from './transforms/vOn'
 | 
				
			||||||
 | 
					import { transformBind } from './transforms/vBind'
 | 
				
			||||||
 | 
					import { trackSlotScopes, trackVForSlotScopes } from './transforms/vSlot'
 | 
				
			||||||
 | 
					import { transformText } from './transforms/transformText'
 | 
				
			||||||
 | 
					import { transformOnce } from './transforms/vOnce'
 | 
				
			||||||
 | 
					import { transformModel } from './transforms/vModel'
 | 
				
			||||||
 | 
					import { defaultOnError, createCompilerError, ErrorCodes } from './errors'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// we name it `baseCompile` so that higher order compilers like @vue/compiler-dom
 | 
				
			||||||
 | 
					// can export `compile` while re-exporting everything else.
 | 
				
			||||||
 | 
					export function baseCompile(
 | 
				
			||||||
 | 
					  template: string | RootNode,
 | 
				
			||||||
 | 
					  options: CompilerOptions = {}
 | 
				
			||||||
 | 
					): CodegenResult {
 | 
				
			||||||
 | 
					  /* istanbul ignore if */
 | 
				
			||||||
 | 
					  if (__BROWSER__) {
 | 
				
			||||||
 | 
					    const onError = options.onError || defaultOnError
 | 
				
			||||||
 | 
					    if (options.prefixIdentifiers === true) {
 | 
				
			||||||
 | 
					      onError(createCompilerError(ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED))
 | 
				
			||||||
 | 
					    } else if (options.mode === 'module') {
 | 
				
			||||||
 | 
					      onError(createCompilerError(ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED))
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const ast = isString(template) ? parse(template, options) : template
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const prefixIdentifiers =
 | 
				
			||||||
 | 
					    !__BROWSER__ &&
 | 
				
			||||||
 | 
					    (options.prefixIdentifiers === true || options.mode === 'module')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  transform(ast, {
 | 
				
			||||||
 | 
					    ...options,
 | 
				
			||||||
 | 
					    prefixIdentifiers,
 | 
				
			||||||
 | 
					    nodeTransforms: [
 | 
				
			||||||
 | 
					      transformOnce,
 | 
				
			||||||
 | 
					      transformIf,
 | 
				
			||||||
 | 
					      transformFor,
 | 
				
			||||||
 | 
					      ...(prefixIdentifiers
 | 
				
			||||||
 | 
					        ? [
 | 
				
			||||||
 | 
					            // order is important
 | 
				
			||||||
 | 
					            trackVForSlotScopes,
 | 
				
			||||||
 | 
					            transformExpression
 | 
				
			||||||
 | 
					          ]
 | 
				
			||||||
 | 
					        : []),
 | 
				
			||||||
 | 
					      transformSlotOutlet,
 | 
				
			||||||
 | 
					      transformElement,
 | 
				
			||||||
 | 
					      trackSlotScopes,
 | 
				
			||||||
 | 
					      transformText,
 | 
				
			||||||
 | 
					      ...(options.nodeTransforms || []) // user transforms
 | 
				
			||||||
 | 
					    ],
 | 
				
			||||||
 | 
					    directiveTransforms: {
 | 
				
			||||||
 | 
					      on: transformOn,
 | 
				
			||||||
 | 
					      bind: transformBind,
 | 
				
			||||||
 | 
					      model: transformModel,
 | 
				
			||||||
 | 
					      ...(options.directiveTransforms || {}) // user transforms
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return generate(ast, {
 | 
				
			||||||
 | 
					    ...options,
 | 
				
			||||||
 | 
					    prefixIdentifiers
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -1,77 +1,4 @@
 | 
				
			|||||||
import { CompilerOptions } from './options'
 | 
					export { baseCompile } from './compile'
 | 
				
			||||||
import { parse } from './parse'
 | 
					 | 
				
			||||||
import { transform } from './transform'
 | 
					 | 
				
			||||||
import { generate, CodegenResult } from './codegen'
 | 
					 | 
				
			||||||
import { RootNode } from './ast'
 | 
					 | 
				
			||||||
import { isString } from '@vue/shared'
 | 
					 | 
				
			||||||
import { transformIf } from './transforms/vIf'
 | 
					 | 
				
			||||||
import { transformFor } from './transforms/vFor'
 | 
					 | 
				
			||||||
import { transformExpression } from './transforms/transformExpression'
 | 
					 | 
				
			||||||
import { transformSlotOutlet } from './transforms/transformSlotOutlet'
 | 
					 | 
				
			||||||
import { transformElement } from './transforms/transformElement'
 | 
					 | 
				
			||||||
import { transformOn } from './transforms/vOn'
 | 
					 | 
				
			||||||
import { transformBind } from './transforms/vBind'
 | 
					 | 
				
			||||||
import { defaultOnError, createCompilerError, ErrorCodes } from './errors'
 | 
					 | 
				
			||||||
import { trackSlotScopes, trackVForSlotScopes } from './transforms/vSlot'
 | 
					 | 
				
			||||||
import { transformText } from './transforms/transformText'
 | 
					 | 
				
			||||||
import { transformOnce } from './transforms/vOnce'
 | 
					 | 
				
			||||||
import { transformModel } from './transforms/vModel'
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// we name it `baseCompile` so that higher order compilers like @vue/compiler-dom
 | 
					 | 
				
			||||||
// can export `compile` while re-exporting everything else.
 | 
					 | 
				
			||||||
export function baseCompile(
 | 
					 | 
				
			||||||
  template: string | RootNode,
 | 
					 | 
				
			||||||
  options: CompilerOptions = {}
 | 
					 | 
				
			||||||
): CodegenResult {
 | 
					 | 
				
			||||||
  /* istanbul ignore if */
 | 
					 | 
				
			||||||
  if (__BROWSER__) {
 | 
					 | 
				
			||||||
    const onError = options.onError || defaultOnError
 | 
					 | 
				
			||||||
    if (options.prefixIdentifiers === true) {
 | 
					 | 
				
			||||||
      onError(createCompilerError(ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED))
 | 
					 | 
				
			||||||
    } else if (options.mode === 'module') {
 | 
					 | 
				
			||||||
      onError(createCompilerError(ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED))
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  const ast = isString(template) ? parse(template, options) : template
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  const prefixIdentifiers =
 | 
					 | 
				
			||||||
    !__BROWSER__ &&
 | 
					 | 
				
			||||||
    (options.prefixIdentifiers === true || options.mode === 'module')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  transform(ast, {
 | 
					 | 
				
			||||||
    ...options,
 | 
					 | 
				
			||||||
    prefixIdentifiers,
 | 
					 | 
				
			||||||
    nodeTransforms: [
 | 
					 | 
				
			||||||
      transformOnce,
 | 
					 | 
				
			||||||
      transformIf,
 | 
					 | 
				
			||||||
      transformFor,
 | 
					 | 
				
			||||||
      ...(prefixIdentifiers
 | 
					 | 
				
			||||||
        ? [
 | 
					 | 
				
			||||||
            // order is important
 | 
					 | 
				
			||||||
            trackVForSlotScopes,
 | 
					 | 
				
			||||||
            transformExpression
 | 
					 | 
				
			||||||
          ]
 | 
					 | 
				
			||||||
        : []),
 | 
					 | 
				
			||||||
      transformSlotOutlet,
 | 
					 | 
				
			||||||
      transformElement,
 | 
					 | 
				
			||||||
      trackSlotScopes,
 | 
					 | 
				
			||||||
      transformText,
 | 
					 | 
				
			||||||
      ...(options.nodeTransforms || []) // user transforms
 | 
					 | 
				
			||||||
    ],
 | 
					 | 
				
			||||||
    directiveTransforms: {
 | 
					 | 
				
			||||||
      on: transformOn,
 | 
					 | 
				
			||||||
      bind: transformBind,
 | 
					 | 
				
			||||||
      model: transformModel,
 | 
					 | 
				
			||||||
      ...(options.directiveTransforms || {}) // user transforms
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  })
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  return generate(ast, {
 | 
					 | 
				
			||||||
    ...options,
 | 
					 | 
				
			||||||
    prefixIdentifiers
 | 
					 | 
				
			||||||
  })
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Also expose lower level APIs & types
 | 
					// Also expose lower level APIs & types
 | 
				
			||||||
export {
 | 
					export {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user