1
1
import qs from 'querystring'
2
+ import path from 'path'
2
3
import { rewriteDefault , SFCBlock , SFCDescriptor } from '@vue/compiler-sfc'
3
4
import { ResolvedOptions } from '.'
4
- import { createDescriptor , getPrevDescriptor } from './utils/descriptorCache'
5
+ import {
6
+ createDescriptor ,
7
+ getPrevDescriptor ,
8
+ setDescriptor
9
+ } from './utils/descriptorCache'
5
10
import { PluginContext , TransformPluginContext } from 'rollup'
6
11
import { resolveScript } from './script'
7
12
import { transformTemplateInMain } from './template'
@@ -162,14 +167,16 @@ function genTemplateCode(
162
167
pluginContext
163
168
)
164
169
} else {
170
+ if ( template . src ) {
171
+ linkSrcToDescriptor ( template . src , descriptor )
172
+ }
165
173
const src = template . src || descriptor . filename
166
174
const srcQuery = template . src ? `&src` : ``
167
175
const attrsQuery = attrsToQuery ( template . attrs , 'js' , true )
168
176
const query = `?vue&type=template${ srcQuery } ${ attrsQuery } `
177
+ const request = JSON . stringify ( src + query )
169
178
return {
170
- code : `import { ${ renderFnName } as _sfc_${ renderFnName } } from ${ JSON . stringify (
171
- src + query
172
- ) } `,
179
+ code : `import { ${ renderFnName } as _sfc_${ renderFnName } } from ${ request } ` ,
173
180
map : undefined
174
181
}
175
182
}
@@ -205,14 +212,17 @@ async function genScriptCode(
205
212
map = result . map
206
213
}
207
214
} else {
215
+ if ( script . src ) {
216
+ linkSrcToDescriptor ( script . src , descriptor )
217
+ }
208
218
const src = script . src || descriptor . filename
209
- const attrsQuery = attrsToQuery ( script . attrs , 'js' )
219
+ const langFallback = ( script . src && path . extname ( src ) . slice ( 1 ) ) || 'js'
220
+ const attrsQuery = attrsToQuery ( script . attrs , langFallback )
210
221
const srcQuery = script . src ? `&src` : ``
211
222
const query = `?vue&type=script${ srcQuery } ${ attrsQuery } `
212
- const scriptRequest = JSON . stringify ( src + query )
223
+ const request = JSON . stringify ( src + query )
213
224
scriptCode =
214
- `import _sfc_main from ${ scriptRequest } \n` +
215
- `export * from ${ scriptRequest } ` // support named exports
225
+ `import _sfc_main from ${ request } \n` + `export * from ${ request } ` // support named exports
216
226
}
217
227
}
218
228
return {
@@ -226,6 +236,9 @@ function genStyleCode(descriptor: SFCDescriptor) {
226
236
let hasCSSModules = false
227
237
if ( descriptor . styles . length ) {
228
238
descriptor . styles . forEach ( ( style , i ) => {
239
+ if ( style . src ) {
240
+ linkSrcToDescriptor ( style . src , descriptor )
241
+ }
229
242
const src = style . src || descriptor . filename
230
243
// do not include module in default query, since we use it to indicate
231
244
// that the module needs to export the modules json
@@ -251,6 +264,9 @@ function genStyleCode(descriptor: SFCDescriptor) {
251
264
function genCustomBlockCode ( descriptor : SFCDescriptor ) {
252
265
let code = ''
253
266
descriptor . customBlocks . forEach ( ( block , index ) => {
267
+ if ( block . src ) {
268
+ linkSrcToDescriptor ( block . src , descriptor )
269
+ }
254
270
const src = block . src || descriptor . filename
255
271
const attrsQuery = attrsToQuery ( block . attrs , block . type )
256
272
const srcQuery = block . src ? `&src` : ``
@@ -277,6 +293,19 @@ function genCSSModulesCode(
277
293
)
278
294
}
279
295
296
+ /**
297
+ * For blocks with src imports, it is important to link the imported file
298
+ * with its owner SFC descriptor so that we can get the information about
299
+ * the owner SFC when compiling that file in the transform phase.
300
+ */
301
+ function linkSrcToDescriptor ( src : string , descriptor : SFCDescriptor ) {
302
+ const srcFile = path . posix . resolve (
303
+ path . posix . dirname ( descriptor . filename ) ,
304
+ src
305
+ )
306
+ setDescriptor ( srcFile , descriptor )
307
+ }
308
+
280
309
// these are built-in query parameters so should be ignored
281
310
// if the user happen to add them as attrs
282
311
const ignoreList = [ 'id' , 'index' , 'src' , 'type' , 'lang' , 'module' ]
0 commit comments