11import hash from 'hash-sum'
22import path from 'path'
33import 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'
115import { Options } from '.'
12- import { getTemplateCompilerOptions } from './template'
136import { setDescriptor } from './utils/descriptorCache'
147import { TransformPluginContext } from 'rollup'
158import { createRollupError } from './utils/error'
9+ import { resolveScript } from './script'
1610
1711export function transformSFCEntry (
1812 code : string ,
19- resourcePath : string ,
13+ filename : string ,
2014 options : Options ,
2115 sourceRoot : string ,
2216 isProduction : boolean ,
@@ -26,20 +20,20 @@ export function transformSFCEntry(
2620) {
2721 const { descriptor, errors } = parse ( code , {
2822 sourceMap : true ,
29- filename : resourcePath ,
23+ filename,
3024 sourceRoot,
3125 } )
32- setDescriptor ( resourcePath , descriptor )
26+ setDescriptor ( filename , descriptor )
3327
3428 if ( errors . length ) {
3529 errors . forEach ( ( error ) =>
36- pluginContext . error ( createRollupError ( resourcePath , error ) )
30+ pluginContext . error ( createRollupError ( filename , error ) )
3731 )
3832 return null
3933 }
4034
4135 const shortFilePath = path
42- . relative ( sourceRoot , resourcePath )
36+ . relative ( sourceRoot , filename )
4337 . replace ( / ^ ( \. \. [ \/ \\ ] ) + / , '' )
4438 . replace ( / \\ / g, '/' )
4539 const scopeId = hash (
@@ -54,7 +48,7 @@ export function transformSFCEntry(
5448 ( isServer || ! descriptor . scriptSetup )
5549
5650 const templateImport = hasTemplateImport
57- ? genTemplateCode ( descriptor , resourcePath , scopeId , isServer )
51+ ? genTemplateCode ( descriptor , scopeId , isServer )
5852 : ''
5953
6054 const renderReplace = hasTemplateImport
@@ -65,23 +59,13 @@ export function transformSFCEntry(
6559
6660 const scriptImport = genScriptCode (
6761 descriptor ,
68- resourcePath ,
6962 scopeId ,
7063 isProduction ,
7164 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
8466 )
67+ const stylesCode = genStyleCode ( descriptor , scopeId , options . preprocessStyles )
68+ const customBlocksCode = getCustomBlock ( descriptor , filterCustomBlock )
8569 const output = [
8670 scriptImport ,
8771 templateImport ,
@@ -110,15 +94,14 @@ export function transformSFCEntry(
11094
11195function genTemplateCode (
11296 descriptor : SFCDescriptor ,
113- resourcePath : string ,
11497 id : string ,
11598 isServer : boolean
11699) {
117100 const renderFnName = isServer ? 'ssrRender' : 'render'
118101 let templateImport = `const ${ renderFnName } = () => {}`
119102 let templateRequest
120103 if ( descriptor . template ) {
121- const src = descriptor . template . src || resourcePath
104+ const src = descriptor . template . src || descriptor . filename
122105 const idQuery = `&id=${ id } `
123106 const srcQuery = descriptor . template . src ? `&src` : ``
124107 const attrsQuery = attrsToQuery ( descriptor . template . attrs , 'js' , true )
@@ -132,48 +115,35 @@ function genTemplateCode(
132115
133116function genScriptCode (
134117 descriptor : SFCDescriptor ,
135- resourcePath : string ,
136- id : string ,
118+ scopeId : string ,
137119 isProd : boolean ,
138120 isServer : boolean ,
139- templateOptions ?: Partial < SFCTemplateCompileOptions >
121+ options : Options
140122) {
141123 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
162133 }
163134 return scriptImport
164135}
165136
166137function genStyleCode (
167138 descriptor : SFCDescriptor ,
168- resourcePath : string ,
169- id : string ,
139+ scopeId : string ,
170140 preprocessStyles ?: boolean
171141) {
172142 let stylesCode = ``
173143 let hasCSSModules = false
174144 if ( descriptor . styles . length ) {
175145 descriptor . styles . forEach ( ( style , i ) => {
176- const src = style . src || resourcePath
146+ const src = style . src || descriptor . filename
177147 // do not include module in default query, since we use it to indicate
178148 // that the module needs to export the modules json
179149 const attrsQuery = attrsToQuery ( style . attrs , 'css' , preprocessStyles )
@@ -183,7 +153,7 @@ function genStyleCode(
183153 )
184154 // make sure to only pass id when necessary so that we don't inject
185155 // duplicate tags when multiple components import the same css file
186- const idQuery = `&id=${ id } `
156+ const idQuery = `&id=${ scopeId } `
187157 const srcQuery = style . src ? `&src` : ``
188158 const query = `?vue&type=style&index=${ i } ${ srcQuery } ${ idQuery } `
189159 const styleRequest = src + query + attrsQuery
@@ -194,7 +164,7 @@ function genStyleCode(
194164 hasCSSModules = true
195165 }
196166 stylesCode += genCSSModulesCode (
197- id ,
167+ scopeId ,
198168 i ,
199169 styleRequest ,
200170 styleRequestWithoutModule ,
@@ -211,14 +181,13 @@ function genStyleCode(
211181
212182function getCustomBlock (
213183 descriptor : SFCDescriptor ,
214- resourcePath : string ,
215184 filter : ( type : string ) => boolean
216185) {
217186 let code = ''
218187
219188 descriptor . customBlocks . forEach ( ( block , index ) => {
220189 if ( filter ( block . type ) ) {
221- const src = block . src || resourcePath
190+ const src = block . src || descriptor . filename
222191 const attrsQuery = attrsToQuery ( block . attrs , block . type )
223192 const srcQuery = block . src ? `&src` : ``
224193 const query = `?vue&type=${ block . type } &index=${ index } ${ srcQuery } ${ attrsQuery } `
0 commit comments