1
1
import type { SFCBlock , SFCTemplateBlock } from 'vue/compiler-sfc'
2
2
import type { InputFile , Loader , LoaderContext , LoaderResult , OutputFile } from './types/mkdist'
3
+ import { transform } from 'esbuild'
3
4
import { preTranspileScriptSetup , transpileVueTemplate } from './index'
4
5
5
6
interface DefineVueLoaderOptions {
@@ -40,7 +41,6 @@ function defineVueLoader(options?: DefineVueLoaderOptions): Loader {
40
41
const { parse } = await import ( 'vue/compiler-sfc' )
41
42
42
43
let modified = false
43
- let fakeScriptBlock = false
44
44
45
45
const raw = await input . getContents ( )
46
46
const sfc = parse ( raw , {
@@ -81,28 +81,14 @@ function defineVueLoader(options?: DefineVueLoaderOptions): Loader {
81
81
: sfc . descriptor . scriptSetup ,
82
82
)
83
83
}
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
+ } )
106
92
107
93
const results = await Promise . all (
108
94
blocks . map ( async ( data ) => {
@@ -127,10 +113,6 @@ function defineVueLoader(options?: DefineVueLoaderOptions): Loader {
127
113
const contents = results
128
114
. sort ( ( a , b ) => a . offset - b . offset )
129
115
. map ( ( { block } ) => {
130
- if ( block . type === 'script' && fakeScriptBlock ) {
131
- return undefined
132
- }
133
-
134
116
const attrs = Object . entries ( block . attrs )
135
117
. map ( ( [ key , value ] ) => {
136
118
if ( ! value ) {
@@ -236,11 +218,23 @@ const styleLoader = defineDefaultBlockLoader({
236
218
type : 'style' ,
237
219
} )
238
220
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
+ }
244
238
245
239
export const vueLoader = defineVueLoader ( {
246
240
blockLoaders : {
0 commit comments