@@ -10,7 +10,9 @@ import {
1010 ParserPlugin
1111} from '@babel/parser'
1212import { generateCodeFrame } from 'compiler/codeframe'
13- import { camelize , capitalize , makeMap } from 'shared/util'
13+ import { camelize , capitalize , isBuiltInTag , makeMap } from 'shared/util'
14+ import { parseHTML } from 'compiler/parser/html-parser'
15+ import { baseOptions as webCompilerOptions } from 'web/compiler/options'
1416import {
1517 Node ,
1618 Declaration ,
@@ -37,6 +39,10 @@ import {
3739import { walk } from 'estree-walker'
3840import { RawSourceMap } from 'source-map'
3941import { warnOnce } from './warn'
42+ import { isReservedTag } from 'web/util'
43+ import { dirRE } from 'compiler/parser'
44+ import { parseText } from 'compiler/parser/text-parser'
45+ import { DEFAULT_FILENAME } from './parse'
4046
4147// Special compiler macros
4248const DEFINE_PROPS = 'defineProps'
@@ -52,7 +58,6 @@ const isBuiltInDir = makeMap(
5258)
5359
5460export interface SFCScriptCompileOptions {
55- filename : string
5661 /**
5762 * Production mode. Used to determine whether to generate hashed CSS variables
5863 */
@@ -82,10 +87,9 @@ export interface ImportBinding {
8287 */
8388export function compileScript (
8489 sfc : SFCDescriptor ,
85- options : SFCScriptCompileOptions
90+ options : SFCScriptCompileOptions = { }
8691) : SFCScriptBlock {
87- let { script, scriptSetup, source } = sfc
88- const { filename } = options
92+ let { filename, script, scriptSetup, source } = sfc
8993 const isProd = ! ! options . isProd
9094 const genSourceMap = options . sourceMap !== false
9195 let refBindings : string [ ] | undefined
@@ -201,10 +205,10 @@ export function compileScript(
201205
202206 // magic-string state
203207 const s = new MagicString ( source )
204- const startOffset = scriptSetup . loc . start . offset
205- const endOffset = scriptSetup . loc . end . offset
206- const scriptStartOffset = script && script . loc . start . offset
207- const scriptEndOffset = script && script . loc . end . offset
208+ const startOffset = scriptSetup . start
209+ const endOffset = scriptSetup . end
210+ const scriptStartOffset = script && script . start
211+ const scriptEndOffset = script && script . end
208212
209213 function helper ( key : string ) : string {
210214 helperImports . add ( key )
@@ -1211,7 +1215,7 @@ export function compileScript(
12111215
12121216 // 11. finalize default export
12131217 let runtimeOptions = ``
1214- if ( ! hasDefaultExportName && filename ) {
1218+ if ( ! hasDefaultExportName && filename && filename !== DEFAULT_FILENAME ) {
12151219 const match = filename . match ( / ( [ ^ / \\ ] + ) \. \w + $ / )
12161220 if ( match ) {
12171221 runtimeOptions += `\n __name: '${ match [ 1 ] } ',`
@@ -1828,44 +1832,39 @@ function getObjectOrArrayExpressionKeys(value: Node): string[] {
18281832const templateUsageCheckCache = new LRU < string , string > ( 512 )
18291833
18301834function resolveTemplateUsageCheckString ( sfc : SFCDescriptor ) {
1831- const { content, ast } = sfc . template !
1835+ const { content } = sfc . template !
18321836 const cached = templateUsageCheckCache . get ( content )
18331837 if ( cached ) {
18341838 return cached
18351839 }
18361840
18371841 let code = ''
1838- transform ( createRoot ( [ ast ] ) , {
1839- nodeTransforms : [
1840- node => {
1841- if ( node . type === NodeTypes . ELEMENT ) {
1842- if (
1843- ! parserOptions . isNativeTag ! ( node . tag ) &&
1844- ! parserOptions . isBuiltInComponent ! ( node . tag )
1845- ) {
1846- code += `,${ camelize ( node . tag ) } ,${ capitalize ( camelize ( node . tag ) ) } `
1842+
1843+ parseHTML ( content , {
1844+ ...webCompilerOptions ,
1845+ start ( tag , attrs ) {
1846+ if ( ! isBuiltInTag ( tag ) && ! isReservedTag ( tag ) ) {
1847+ code += `,${ camelize ( tag ) } ,${ capitalize ( camelize ( tag ) ) } `
1848+ }
1849+ for ( let i = 0 ; i < attrs . length ; i ++ ) {
1850+ const { name, value } = attrs [ i ]
1851+ if ( dirRE . test ( name ) ) {
1852+ const baseName = name . replace ( dirRE , '' )
1853+ if ( ! isBuiltInDir ( baseName ) ) {
1854+ code += `,v${ capitalize ( camelize ( baseName ) ) } `
18471855 }
1848- for ( let i = 0 ; i < node . props . length ; i ++ ) {
1849- const prop = node . props [ i ]
1850- if ( prop . type === NodeTypes . DIRECTIVE ) {
1851- if ( ! isBuiltInDir ( prop . name ) ) {
1852- code += `,v${ capitalize ( camelize ( prop . name ) ) } `
1853- }
1854- if ( prop . exp ) {
1855- code += `,${ processExp (
1856- ( prop . exp as SimpleExpressionNode ) . content ,
1857- prop . name
1858- ) } `
1859- }
1860- }
1856+ if ( value ) {
1857+ code += `,${ processExp ( value , baseName ) } `
18611858 }
1862- } else if ( node . type === NodeTypes . INTERPOLATION ) {
1863- code += `,${ processExp (
1864- ( node . content as SimpleExpressionNode ) . content
1865- ) } `
18661859 }
18671860 }
1868- ]
1861+ } ,
1862+ chars ( text ) {
1863+ const res = parseText ( text )
1864+ if ( res ) {
1865+ code += `,${ processExp ( res . expression ) } `
1866+ }
1867+ }
18691868 } )
18701869
18711870 code += ';'
0 commit comments