Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 33eb148

Browse files
authored
fix: dynamic import module to avoid breaking in browser (#129)
1 parent 55f5753 commit 33eb148

10 files changed

+39
-50
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ module.exports = {
249249
```ts
250250
import { transform } from 'unplugin-vue2-script-setup'
251251

252-
const Vue2SFC = transform(`
252+
const Vue2SFC = await transform(`
253253
<template>
254254
<!-- ... -->
255255
</template>

jest.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function requireVueJest() {
1515
}
1616

1717
module.exports = {
18-
process(source, filename, ...args) {
19-
const transformed = transform(source, filename)
18+
async process(source, filename, ...args) {
19+
const transformed = await transform(source, filename)
2020
const code = transformed ? transformed.code : source
2121
return requireVueJest().process.call(this, code, filename, ...args)
2222
},

package.json

+3-7
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@
4949
"require": "./dist/webpack.js",
5050
"import": "./dist/webpack.mjs",
5151
"types": "./webpack.d.ts"
52-
},
53-
"./lib": {
54-
"require": "./dist/lib.js",
55-
"import": "./dist/lib.mjs",
56-
"types": "./lib.d.ts"
5752
}
5853
},
5954
"main": "dist/index.js",
@@ -101,6 +96,7 @@
10196
"@types/babel__core": "^7.1.18",
10297
"@types/estree": "^0.0.51",
10398
"@types/node": "^17.0.21",
99+
"@types/pug": "^2.0.6",
104100
"@types/ws": "^8.5.0",
105101
"@vue/composition-api": "^1.4.6",
106102
"@vue/runtime-dom": "^3.2.31",
@@ -117,9 +113,9 @@
117113
"vitest": "0.5.4"
118114
},
119115
"peerDependencies": {
120-
"pug": "^3.0.2",
121116
"@vue/composition-api": "^1.4.3",
122-
"@vue/runtime-dom": "^3.2.31"
117+
"@vue/runtime-dom": "^3.2.31",
118+
"pug": "^3.0.2"
123119
},
124120
"peerDependenciesMeta": {
125121
"pug": {

pnpm-lock.yaml

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rollup.config.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ const entries = [
1313
'src/vite.ts',
1414
'src/rollup.ts',
1515
'src/esbuild.ts',
16-
'src/nuxt.ts',
17-
'src/lib.ts',
16+
'src/nuxt.ts'
1817
]
1918

2019
const dtsEntries = [

src/core/parseSFC.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-disable one-var */
22
/* eslint-disable @typescript-eslint/no-namespace */
3-
import { createRequire } from 'module'
43
import { notNullish, partition } from '@antfu/utils'
54
import type { Program } from '@babel/types'
65
import type { ParserPlugin } from '@babel/parser'
@@ -83,14 +82,6 @@ const BUILD_IN_DIRECTIVES = new Set([
8382
// 'ref',
8483
])
8584

86-
function getRequire() {
87-
return (
88-
(typeof require === 'function')
89-
? require
90-
: createRequire(import.meta.url)
91-
)
92-
}
93-
9485
function getComponents(node: TemplateChildNode): string[] {
9586
const current
9687
= node.type === NodeTypes.ELEMENT && node.tagType === ElementTypes.COMPONENT
@@ -282,11 +273,11 @@ function getBabelParserOptions(lang: string | null | undefined) {
282273
plugins,
283274
}
284275
}
285-
export function parseSFC(
276+
export async function parseSFC(
286277
code: string,
287278
id?: string,
288279
options?: ScriptSetupTransformOptions,
289-
): ParsedSFC {
280+
): Promise<ParsedSFC> {
290281
const elementChildren = baseParse(code, parserOptions).children.flatMap(x =>
291282
x.type === NodeTypes.ELEMENT && x.tagType === ElementTypes.ELEMENT
292283
? [x]
@@ -376,7 +367,7 @@ export function parseSFC(
376367
&& p.value.content === 'pug',
377368
)
378369
? baseParse(
379-
getRequire()('pug').compile(
370+
(await import('pug')).compile(
380371
templateNode.children.map(x => x.loc.source).join(''),
381372
{
382373
filename: id,

src/core/transform.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ export function shouldTransform(code: string, id: string, options?: ScriptSetupT
1515
return (options?.reactivityTransform && shouldTransformRefSugar(code)) || scriptSetupRE.test(code)
1616
}
1717

18-
export function transform(input: string, id: string, options?: ScriptSetupTransformOptions): TransformResult {
18+
export async function transform(input: string, id: string, options?: ScriptSetupTransformOptions): Promise<TransformResult> {
1919
if (!shouldTransform(input, id, options))
2020
return null
2121
const resolved = resolveOptions(options)
2222
if (id.endsWith('.vue') || id.includes('.vue?vue'))
23-
return transformVue(input, id, resolved)
23+
return await transformVue(input, id, resolved)
2424
else
2525
return transformNonVue(input, id, resolved)
2626
}
@@ -36,10 +36,10 @@ function transformNonVue(input: string, id: string, options: ResolvedOptions): T
3636
return null
3737
}
3838

39-
function transformVue(input: string, id: string, options: ResolvedOptions): TransformResult {
39+
async function transformVue(input: string, id: string, options: ResolvedOptions): Promise<TransformResult> {
4040
const s = new MagicString(input)
4141

42-
const sfc = parseSFC(input, id)
42+
const sfc = await parseSFC(input, id)
4343

4444
if (options.reactivityTransform)
4545
transformSfcRefSugar(sfc, options)

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}) => {
1717
transformInclude(id) {
1818
return filter(id)
1919
},
20-
transform(code, id) {
20+
async transform(code, id) {
2121
try {
22-
return transform(code, id, options)
22+
return await transform(code, id, options)
2323
}
2424
catch (e: any) {
2525
this.error(e)

test/errors.test.ts

+14-17
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { describe, expect, it, vi } from 'vitest'
22
import { transform as t } from '../src'
33

44
describe('errors', () => {
5-
it('langs', () => {
6-
expect(() =>
5+
it('langs', async() => {
6+
await expect(() =>
77
t(`
88
<script setup>
99
const a = 1
@@ -12,54 +12,51 @@ const a = 1
1212
<script lang="ts">
1313
export default {}
1414
</script>
15-
`, 'Lang.vue'))
16-
.toThrowError('<script setup> language must be the same as <script>')
15+
`, 'Lang.vue')).rejects.toThrowError('<script setup> language must be the same as <script>')
1716
})
1817

19-
it('defineProps', () => {
20-
expect(() =>
18+
it('defineProps', async() => {
19+
await expect(() =>
2120
t(`
2221
<script setup>
2322
defineProps()
2423
const a = defineProps()
2524
</script>
2625
`, 'DefineProps.vue'))
27-
.toThrowError('duplicate defineProps() call')
26+
.rejects.toThrowError('duplicate defineProps() call')
2827
})
2928

30-
it('top-level await', () => {
31-
expect(() =>
29+
it('top-level await', async() => {
30+
await expect(() =>
3231
t(`
3332
<script setup>
3433
defineProps()
3534
await something()
3635
</script>
3736
`, 'TopLevel.vue'))
38-
.toThrowError('top-level await is not supported in Vue 2')
37+
.rejects.toThrowError('top-level await is not supported in Vue 2')
3938

40-
expect(() =>
39+
await expect(() =>
4140
t(`
4241
<script setup>
4342
defineProps()
4443
const {data} = await something()
4544
</script>
4645
`, 'TopLevel.vue'))
47-
.toThrowError('top-level await is not supported in Vue 2')
46+
.rejects.toThrowError('top-level await is not supported in Vue 2')
4847
})
4948

50-
it('ref sugar', () => {
49+
it('ref sugar', async() => {
5150
const consoleWarnMock = vi.spyOn(console, 'warn')
5251

53-
expect(() =>
54-
t(`
52+
await t(`
5553
<script setup>
5654
defineProps()
5755
const a = async () => {
5856
await something()
5957
}
6058
</script>
61-
`, 'App.vue'))
62-
.not.toThrow()
59+
`, 'App.vue')
6360

6461
consoleWarnMock.mockRestore()
6562
})

test/transform.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ describe('transform', () => {
1818
for (const file of files) {
1919
it(file.replace(/\\/g, '/'), async() => {
2020
const fixture = await fs.readFile(resolve(root, file), 'utf-8')
21-
const result = transform(fixture, file, { reactivityTransform: true })?.code || fixture
21+
const result = (await transform(fixture, file, { reactivityTransform: true }))?.code || fixture
2222
expect(result).toMatchSnapshot()
2323

24-
const result2 = transform(result, file, { reactivityTransform: true })?.code || result
24+
const result2 = (await transform(result, file, { reactivityTransform: true }))?.code || result
2525
expect(result).toEqual(result2)
2626
})
2727
}

0 commit comments

Comments
 (0)