@@ -10,7 +10,9 @@ import {
10
10
ParserPlugin
11
11
} from '@babel/parser'
12
12
import { 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'
14
16
import {
15
17
Node ,
16
18
Declaration ,
@@ -37,6 +39,10 @@ import {
37
39
import { walk } from 'estree-walker'
38
40
import { RawSourceMap } from 'source-map'
39
41
import { 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'
40
46
41
47
// Special compiler macros
42
48
const DEFINE_PROPS = 'defineProps'
@@ -52,7 +58,6 @@ const isBuiltInDir = makeMap(
52
58
)
53
59
54
60
export interface SFCScriptCompileOptions {
55
- filename : string
56
61
/**
57
62
* Production mode. Used to determine whether to generate hashed CSS variables
58
63
*/
@@ -82,10 +87,9 @@ export interface ImportBinding {
82
87
*/
83
88
export function compileScript (
84
89
sfc : SFCDescriptor ,
85
- options : SFCScriptCompileOptions
90
+ options : SFCScriptCompileOptions = { }
86
91
) : SFCScriptBlock {
87
- let { script, scriptSetup, source } = sfc
88
- const { filename } = options
92
+ let { filename, script, scriptSetup, source } = sfc
89
93
const isProd = ! ! options . isProd
90
94
const genSourceMap = options . sourceMap !== false
91
95
let refBindings : string [ ] | undefined
@@ -201,10 +205,10 @@ export function compileScript(
201
205
202
206
// magic-string state
203
207
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
208
212
209
213
function helper ( key : string ) : string {
210
214
helperImports . add ( key )
@@ -1211,7 +1215,7 @@ export function compileScript(
1211
1215
1212
1216
// 11. finalize default export
1213
1217
let runtimeOptions = ``
1214
- if ( ! hasDefaultExportName && filename ) {
1218
+ if ( ! hasDefaultExportName && filename && filename !== DEFAULT_FILENAME ) {
1215
1219
const match = filename . match ( / ( [ ^ / \\ ] + ) \. \w + $ / )
1216
1220
if ( match ) {
1217
1221
runtimeOptions += `\n __name: '${ match [ 1 ] } ',`
@@ -1828,44 +1832,39 @@ function getObjectOrArrayExpressionKeys(value: Node): string[] {
1828
1832
const templateUsageCheckCache = new LRU < string , string > ( 512 )
1829
1833
1830
1834
function resolveTemplateUsageCheckString ( sfc : SFCDescriptor ) {
1831
- const { content, ast } = sfc . template !
1835
+ const { content } = sfc . template !
1832
1836
const cached = templateUsageCheckCache . get ( content )
1833
1837
if ( cached ) {
1834
1838
return cached
1835
1839
}
1836
1840
1837
1841
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 ) ) } `
1847
1855
}
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 ) } `
1861
1858
}
1862
- } else if ( node . type === NodeTypes . INTERPOLATION ) {
1863
- code += `,${ processExp (
1864
- ( node . content as SimpleExpressionNode ) . content
1865
- ) } `
1866
1859
}
1867
1860
}
1868
- ]
1861
+ } ,
1862
+ chars ( text ) {
1863
+ const res = parseText ( text )
1864
+ if ( res ) {
1865
+ code += `,${ processExp ( res . expression ) } `
1866
+ }
1867
+ }
1869
1868
} )
1870
1869
1871
1870
code += ';'
0 commit comments