1- import { parseComponent } from ' vue-template-compiler'
2- import walkes from ' walkes'
3- import transformOneImport from ' ./transformOneImport'
4- import getAst from ' ./getAst'
1+ import { parseComponent } from " vue-template-compiler" ;
2+ import walkes from " walkes" ;
3+ import transformOneImport from " ./transformOneImport" ;
4+ import getAst from " ./getAst" ;
55
66const buildStyles = function ( styles ) {
7- let _styles = ''
7+ let _styles = "" ;
88 if ( styles ) {
99 styles . forEach ( it => {
1010 if ( it . content ) {
11- _styles += it . content
11+ _styles += it . content ;
1212 }
13- } )
13+ } ) ;
1414 }
15- if ( _styles !== '' ) {
16- return `<style scoped> ${ _styles . trim ( ) } </style>`
15+ if ( _styles !== "" ) {
16+ return _styles . trim ( ) ;
1717 }
18- return undefined
19- }
18+ return undefined ;
19+ } ;
2020
2121function getSingleFileComponentParts ( code ) {
22- const parts = parseComponent ( code )
22+ const parts = parseComponent ( code ) ;
2323 if ( parts . script )
2424 parts . script . content = parts . script . content . replace (
2525 / \/ \* [ \s \S ] * ?\* \/ | ( [ ^ : ] | ^ ) \/ \/ .* $ / gm,
26- '$1'
27- )
28- return parts
26+ "$1"
27+ ) ;
28+ return parts ;
2929}
3030
3131function injectTemplateAndParseExport ( parts ) {
32- const templateString = parts . template . content . replace ( / ` / g, ' \\`' )
32+ const templateString = parts . template . content . replace ( / ` / g, " \\`" ) ;
3333
34- if ( ! parts . script ) return `{\ntemplate: \`${ templateString } \` }`
34+ if ( ! parts . script ) return `{\ntemplate: \`${ templateString } \` }` ;
3535
36- let code = parts . script . content
37- let preprocessing = ''
38- let startIndex = - 1
39- let endIndex = - 1
40- let offset = 0
36+ let code = parts . script . content ;
37+ let preprocessing = "" ;
38+ let startIndex = - 1 ;
39+ let endIndex = - 1 ;
40+ let offset = 0 ;
4141 walkes ( getAst ( code ) , {
4242 // export const MyComponent = {}
4343 ExportNamedDeclaration ( node ) {
44- preprocessing = code . slice ( 0 , node . start + offset )
45- startIndex = node . declaration . declarations [ 0 ] . init . start + offset
46- endIndex = node . declaration . declarations [ 0 ] . init . end + offset
44+ preprocessing = code . slice ( 0 , node . start + offset ) ;
45+ startIndex = node . declaration . declarations [ 0 ] . init . start + offset ;
46+ endIndex = node . declaration . declarations [ 0 ] . init . end + offset ;
4747 } ,
4848 // export default {}
4949 ExportDefaultDeclaration ( node ) {
50- preprocessing = code . slice ( 0 , node . start + offset )
51- startIndex = node . declaration . start + offset
52- endIndex = node . declaration . end + offset
50+ preprocessing = code . slice ( 0 , node . start + offset ) ;
51+ startIndex = node . declaration . start + offset ;
52+ endIndex = node . declaration . end + offset ;
5353 } ,
5454 // module.exports = {}
5555 AssignmentExpression ( node ) {
@@ -59,26 +59,26 @@ function injectTemplateAndParseExport(parts) {
5959 / m o d u l e / . test ( node . left . object . name ) &&
6060 / e x p o r t s / . test ( node . left . property . name ) )
6161 ) {
62- preprocessing = code . slice ( 0 , node . start + offset )
63- startIndex = node . right . start + offset
64- endIndex = node . right . end + offset
62+ preprocessing = code . slice ( 0 , node . start + offset ) ;
63+ startIndex = node . right . start + offset ;
64+ endIndex = node . right . end + offset ;
6565 }
6666 } ,
6767 ImportDeclaration ( node ) {
68- const ret = transformOneImport ( node , code , offset )
69- offset = ret . offset
70- code = ret . code
68+ const ret = transformOneImport ( node , code , offset ) ;
69+ offset = ret . offset ;
70+ code = ret . code ;
7171 }
72- } )
72+ } ) ;
7373 if ( startIndex === - 1 ) {
74- throw new Error ( ' Failed to parse single file component: ' + code )
74+ throw new Error ( " Failed to parse single file component: " + code ) ;
7575 }
76- let right = code . slice ( startIndex + 1 , endIndex - 1 )
76+ let right = code . slice ( startIndex + 1 , endIndex - 1 ) ;
7777 return {
7878 preprocessing,
7979 component : `{\n template: \`${ templateString } \`,\n ${ right } }` ,
8080 postprocessing : code . slice ( endIndex )
81- }
81+ } ;
8282}
8383
8484/**
@@ -87,14 +87,14 @@ function injectTemplateAndParseExport(parts) {
8787 * transformed into requires
8888 */
8989export default function normalizeSfcComponent ( code ) {
90- const parts = getSingleFileComponentParts ( code )
91- const extractedComponent = injectTemplateAndParseExport ( parts )
90+ const parts = getSingleFileComponentParts ( code ) ;
91+ const extractedComponent = injectTemplateAndParseExport ( parts ) ;
9292 return {
9393 component : [
9494 extractedComponent . preprocessing ,
9595 `new Vue(${ extractedComponent . component } );` ,
9696 extractedComponent . postprocessing
97- ] . join ( '\n' ) ,
97+ ] . join ( "\n" ) ,
9898 style : buildStyles ( parts . styles )
99- }
99+ } ;
100100}
0 commit comments