Skip to content

Commit da253e8

Browse files
authored
fix: enable verbatimModuleSyntax for script blocks (#10)
1 parent 494eba8 commit da253e8

File tree

3 files changed

+52
-33
lines changed

3 files changed

+52
-33
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"dist"
2828
],
2929
"engines": {
30-
"node": ">=6.9.0"
30+
"node": ">=18.0.0"
3131
},
3232
"scripts": {
3333
"build": "unbuild",
@@ -44,6 +44,7 @@
4444
"test:types": "tsc --noEmit"
4545
},
4646
"peerDependencies": {
47+
"esbuild": "*",
4748
"vue": "^3.5.13"
4849
},
4950
"dependencies": {

src/mkdist.ts

+26-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { SFCBlock, SFCTemplateBlock } from 'vue/compiler-sfc'
22
import type { InputFile, Loader, LoaderContext, LoaderResult, OutputFile } from './types/mkdist'
3+
import { transform } from 'esbuild'
34
import { preTranspileScriptSetup, transpileVueTemplate } from './index'
45

56
interface 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

245239
export const vueLoader = defineVueLoader({
246240
blockLoaders: {

test/mkdist.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,30 @@ describe('transform typescript script setup', () => {
192192
`)
193193
})
194194

195+
it('do not tree-shake', async () => {
196+
expect(
197+
await fixture(
198+
`<template>
199+
<div :data-test="toValue('hello')" />
200+
</template>
201+
<script setup lang="ts">
202+
import { toValue, type Ref } from 'vue'
203+
const msg = 1
204+
</script>`,
205+
),
206+
).toMatchInlineSnapshot(`
207+
"<template>
208+
<div :data-test="toValue('hello')" />
209+
</template>
210+
211+
<script setup>
212+
import { toValue } from "vue";
213+
const msg = 1;
214+
</script>
215+
"
216+
`)
217+
})
218+
195219
async function fixture(src: string): Promise<string> {
196220
await rm(tmpDir, { force: true, recursive: true })
197221
await mkdir(join(tmpDir, 'src'), { recursive: true })

0 commit comments

Comments
 (0)