1
1
import hash from 'hash-sum'
2
2
import path from 'path'
3
3
import qs from 'querystring'
4
- import {
5
- compileScript ,
6
- parse ,
7
- SFCBlock ,
8
- SFCDescriptor ,
9
- SFCTemplateCompileOptions ,
10
- } from '@vue/compiler-sfc'
4
+ import { parse , SFCBlock , SFCDescriptor } from '@vue/compiler-sfc'
11
5
import { Options } from '.'
12
- import { getTemplateCompilerOptions } from './template'
13
6
import { setDescriptor } from './utils/descriptorCache'
14
7
import { TransformPluginContext } from 'rollup'
15
8
import { createRollupError } from './utils/error'
9
+ import { resolveScript } from './script'
16
10
17
11
export function transformSFCEntry (
18
12
code : string ,
19
- resourcePath : string ,
13
+ filename : string ,
20
14
options : Options ,
21
15
sourceRoot : string ,
22
16
isProduction : boolean ,
@@ -26,20 +20,20 @@ export function transformSFCEntry(
26
20
) {
27
21
const { descriptor, errors } = parse ( code , {
28
22
sourceMap : true ,
29
- filename : resourcePath ,
23
+ filename,
30
24
sourceRoot,
31
25
} )
32
- setDescriptor ( resourcePath , descriptor )
26
+ setDescriptor ( filename , descriptor )
33
27
34
28
if ( errors . length ) {
35
29
errors . forEach ( ( error ) =>
36
- pluginContext . error ( createRollupError ( resourcePath , error ) )
30
+ pluginContext . error ( createRollupError ( filename , error ) )
37
31
)
38
32
return null
39
33
}
40
34
41
35
const shortFilePath = path
42
- . relative ( sourceRoot , resourcePath )
36
+ . relative ( sourceRoot , filename )
43
37
. replace ( / ^ ( \. \. [ \/ \\ ] ) + / , '' )
44
38
. replace ( / \\ / g, '/' )
45
39
const scopeId = hash (
@@ -54,7 +48,7 @@ export function transformSFCEntry(
54
48
( isServer || ! descriptor . scriptSetup )
55
49
56
50
const templateImport = hasTemplateImport
57
- ? genTemplateCode ( descriptor , resourcePath , scopeId , isServer )
51
+ ? genTemplateCode ( descriptor , scopeId , isServer )
58
52
: ''
59
53
60
54
const renderReplace = hasTemplateImport
@@ -65,23 +59,13 @@ export function transformSFCEntry(
65
59
66
60
const scriptImport = genScriptCode (
67
61
descriptor ,
68
- resourcePath ,
69
62
scopeId ,
70
63
isProduction ,
71
64
isServer ,
72
- getTemplateCompilerOptions ( options , descriptor , scopeId )
73
- )
74
- const stylesCode = genStyleCode (
75
- descriptor ,
76
- resourcePath ,
77
- scopeId ,
78
- options . preprocessStyles
79
- )
80
- const customBlocksCode = getCustomBlock (
81
- descriptor ,
82
- resourcePath ,
83
- filterCustomBlock
65
+ options
84
66
)
67
+ const stylesCode = genStyleCode ( descriptor , scopeId , options . preprocessStyles )
68
+ const customBlocksCode = getCustomBlock ( descriptor , filterCustomBlock )
85
69
const output = [
86
70
scriptImport ,
87
71
templateImport ,
@@ -110,15 +94,14 @@ export function transformSFCEntry(
110
94
111
95
function genTemplateCode (
112
96
descriptor : SFCDescriptor ,
113
- resourcePath : string ,
114
97
id : string ,
115
98
isServer : boolean
116
99
) {
117
100
const renderFnName = isServer ? 'ssrRender' : 'render'
118
101
let templateImport = `const ${ renderFnName } = () => {}`
119
102
let templateRequest
120
103
if ( descriptor . template ) {
121
- const src = descriptor . template . src || resourcePath
104
+ const src = descriptor . template . src || descriptor . filename
122
105
const idQuery = `&id=${ id } `
123
106
const srcQuery = descriptor . template . src ? `&src` : ``
124
107
const attrsQuery = attrsToQuery ( descriptor . template . attrs , 'js' , true )
@@ -132,48 +115,35 @@ function genTemplateCode(
132
115
133
116
function genScriptCode (
134
117
descriptor : SFCDescriptor ,
135
- resourcePath : string ,
136
- id : string ,
118
+ scopeId : string ,
137
119
isProd : boolean ,
138
120
isServer : boolean ,
139
- templateOptions ?: Partial < SFCTemplateCompileOptions >
121
+ options : Options
140
122
) {
141
123
let scriptImport = `const script = {}`
142
- if ( descriptor . script || descriptor . scriptSetup ) {
143
- if ( compileScript ) {
144
- descriptor . scriptCompiled = compileScript ( descriptor , {
145
- id,
146
- isProd,
147
- inlineTemplate : ! isServer ,
148
- templateOptions,
149
- } )
150
- }
151
- const script = descriptor . scriptCompiled || descriptor . script
152
- if ( script ) {
153
- const src = script . src || resourcePath
154
- const attrsQuery = attrsToQuery ( script . attrs , 'js' )
155
- const srcQuery = script . src ? `&src` : ``
156
- const query = `?vue&type=script${ srcQuery } ${ attrsQuery } `
157
- const scriptRequest = JSON . stringify ( src + query )
158
- scriptImport =
159
- `import script from ${ scriptRequest } \n` +
160
- `export * from ${ scriptRequest } ` // support named exports
161
- }
124
+ const script = resolveScript ( descriptor , scopeId , isProd , isServer , options )
125
+ if ( script ) {
126
+ const src = script . src || descriptor . filename
127
+ const attrsQuery = attrsToQuery ( script . attrs , 'js' )
128
+ const srcQuery = script . src ? `&src` : ``
129
+ const query = `?vue&type=script${ srcQuery } ${ attrsQuery } `
130
+ const scriptRequest = JSON . stringify ( src + query )
131
+ scriptImport =
132
+ `import script from ${ scriptRequest } \n` + `export * from ${ scriptRequest } ` // support named exports
162
133
}
163
134
return scriptImport
164
135
}
165
136
166
137
function genStyleCode (
167
138
descriptor : SFCDescriptor ,
168
- resourcePath : string ,
169
- id : string ,
139
+ scopeId : string ,
170
140
preprocessStyles ?: boolean
171
141
) {
172
142
let stylesCode = ``
173
143
let hasCSSModules = false
174
144
if ( descriptor . styles . length ) {
175
145
descriptor . styles . forEach ( ( style , i ) => {
176
- const src = style . src || resourcePath
146
+ const src = style . src || descriptor . filename
177
147
// do not include module in default query, since we use it to indicate
178
148
// that the module needs to export the modules json
179
149
const attrsQuery = attrsToQuery ( style . attrs , 'css' , preprocessStyles )
@@ -183,7 +153,7 @@ function genStyleCode(
183
153
)
184
154
// make sure to only pass id when necessary so that we don't inject
185
155
// duplicate tags when multiple components import the same css file
186
- const idQuery = `&id=${ id } `
156
+ const idQuery = `&id=${ scopeId } `
187
157
const srcQuery = style . src ? `&src` : ``
188
158
const query = `?vue&type=style&index=${ i } ${ srcQuery } ${ idQuery } `
189
159
const styleRequest = src + query + attrsQuery
@@ -194,7 +164,7 @@ function genStyleCode(
194
164
hasCSSModules = true
195
165
}
196
166
stylesCode += genCSSModulesCode (
197
- id ,
167
+ scopeId ,
198
168
i ,
199
169
styleRequest ,
200
170
styleRequestWithoutModule ,
@@ -211,14 +181,13 @@ function genStyleCode(
211
181
212
182
function getCustomBlock (
213
183
descriptor : SFCDescriptor ,
214
- resourcePath : string ,
215
184
filter : ( type : string ) => boolean
216
185
) {
217
186
let code = ''
218
187
219
188
descriptor . customBlocks . forEach ( ( block , index ) => {
220
189
if ( filter ( block . type ) ) {
221
- const src = block . src || resourcePath
190
+ const src = block . src || descriptor . filename
222
191
const attrsQuery = attrsToQuery ( block . attrs , block . type )
223
192
const srcQuery = block . src ? `&src` : ``
224
193
const query = `?vue&type=${ block . type } &index=${ index } ${ srcQuery } ${ attrsQuery } `
0 commit comments