11import type { SFCBlock , SFCTemplateBlock } from 'vue/compiler-sfc'
22import type { InputFile , Loader , LoaderContext , LoaderResult , OutputFile } from './types/mkdist'
3+ import { transform } from 'esbuild'
34import { preTranspileScriptSetup , transpileVueTemplate } from './index'
45
56interface DefineVueLoaderOptions {
@@ -40,7 +41,6 @@ function defineVueLoader(options?: DefineVueLoaderOptions): Loader {
4041 const { parse } = await import ( 'vue/compiler-sfc' )
4142
4243 let modified = false
43- let fakeScriptBlock = false
4444
4545 const raw = await input . getContents ( )
4646 const sfc = parse ( raw , {
@@ -81,28 +81,14 @@ function defineVueLoader(options?: DefineVueLoaderOptions): Loader {
8181 : sfc . descriptor . scriptSetup ,
8282 )
8383 }
84- if ( ! sfc . descriptor . script && ! sfc . descriptor . scriptSetup ) {
85- // push a fake script block to generate dts
86- blocks . unshift ( {
87- type : 'script' ,
88- content : 'default {}' ,
89- attrs : { } ,
90- loc : {
91- start : {
92- offset : 0 ,
93- line : 1 ,
94- column : 1 ,
95- } ,
96- end : {
97- offset : 0 ,
98- line : 1 ,
99- column : 1 ,
100- } ,
101- source : '' ,
102- } ,
103- } )
104- fakeScriptBlock = true
105- }
84+
85+ // generate dts
86+ await context . loadFile ( {
87+ path : `${ input . path } .js` ,
88+ srcPath : `${ input . srcPath } .js` ,
89+ extension : '.js' ,
90+ getContents : ( ) => 'export default {}' ,
91+ } )
10692
10793 const results = await Promise . all (
10894 blocks . map ( async ( data ) => {
@@ -127,10 +113,6 @@ function defineVueLoader(options?: DefineVueLoaderOptions): Loader {
127113 const contents = results
128114 . sort ( ( a , b ) => a . offset - b . offset )
129115 . map ( ( { block } ) => {
130- if ( block . type === 'script' && fakeScriptBlock ) {
131- return undefined
132- }
133-
134116 const attrs = Object . entries ( block . attrs )
135117 . map ( ( [ key , value ] ) => {
136118 if ( ! value ) {
@@ -236,11 +218,23 @@ const styleLoader = defineDefaultBlockLoader({
236218 type : 'style' ,
237219} )
238220
239- const scriptLoader = defineDefaultBlockLoader ( {
240- defaultLang : 'js' ,
241- type : 'script' ,
242- validExtensions : [ '.js' , '.mjs' ] ,
243- } )
221+ const scriptLoader : VueBlockLoader = async ( block , { options } ) => {
222+ if ( block . type !== 'script' ) {
223+ return
224+ }
225+
226+ const { code : result } = await transform ( block . content , {
227+ ...options . esbuild ,
228+ loader : 'ts' ,
229+ tsconfigRaw : { compilerOptions : { target : 'ESNext' , verbatimModuleSyntax : true } } ,
230+ } )
231+
232+ return {
233+ type : block . type ,
234+ attrs : toOmit ( block . attrs , [ 'lang' , 'generic' ] ) ,
235+ content : result ,
236+ }
237+ }
244238
245239export const vueLoader = defineVueLoader ( {
246240 blockLoaders : {
0 commit comments