Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit33eb148

Browse files
authored
fix: dynamic import module to avoid breaking in browser (#129)
1 parent55f5753 commit33eb148

File tree

10 files changed

+39
-50
lines changed

10 files changed

+39
-50
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
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=awaittransform(`
253253
<template>
254254
<!-- ... -->
255255
</template>

‎jest.js

Lines changed: 2 additions & 2 deletions
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-
consttransformed=transform(source,filename)
18+
asyncprocess(source,filename, ...args){
19+
consttransformed=awaittransform(source,filename)
2020
constcode=transformed ?transformed.code :source
2121
returnrequireVueJest().process.call(this,code,filename, ...args)
2222
},

‎package.json

Lines changed: 3 additions & 7 deletions
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

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎rollup.config.js

Lines changed: 1 addition & 2 deletions
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
constdtsEntries=[

‎src/core/parseSFC.ts

Lines changed: 3 additions & 12 deletions
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
importtype{Program}from'@babel/types'
65
importtype{ParserPlugin}from'@babel/parser'
@@ -83,14 +82,6 @@ const BUILD_IN_DIRECTIVES = new Set([
8382
// 'ref',
8483
])
8584

86-
functiongetRequire(){
87-
return(
88-
(typeofrequire==='function')
89-
?require
90-
:createRequire(import.meta.url)
91-
)
92-
}
93-
9485
functiongetComponents(node:TemplateChildNode):string[]{
9586
constcurrent
9687
=node.type===NodeTypes.ELEMENT&&node.tagType===ElementTypes.COMPONENT
@@ -282,11 +273,11 @@ function getBabelParserOptions(lang: string | null | undefined) {
282273
plugins,
283274
}
284275
}
285-
exportfunctionparseSFC(
276+
exportasyncfunctionparseSFC(
286277
code:string,
287278
id?:string,
288279
options?:ScriptSetupTransformOptions,
289-
):ParsedSFC{
280+
):Promise<ParsedSFC>{
290281
constelementChildren=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+
(awaitimport('pug')).compile(
380371
templateNode.children.map(x=>x.loc.source).join(''),
381372
{
382373
filename:id,

‎src/core/transform.ts

Lines changed: 4 additions & 4 deletions
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-
exportfunctiontransform(input:string,id:string,options?:ScriptSetupTransformOptions):TransformResult{
18+
exportasyncfunctiontransform(input:string,id:string,options?:ScriptSetupTransformOptions):Promise<TransformResult>{
1919
if(!shouldTransform(input,id,options))
2020
returnnull
2121
constresolved=resolveOptions(options)
2222
if(id.endsWith('.vue')||id.includes('.vue?vue'))
23-
returntransformVue(input,id,resolved)
23+
returnawaittransformVue(input,id,resolved)
2424
else
2525
returntransformNonVue(input,id,resolved)
2626
}
@@ -36,10 +36,10 @@ function transformNonVue(input: string, id: string, options: ResolvedOptions): T
3636
returnnull
3737
}
3838

39-
functiontransformVue(input:string,id:string,options:ResolvedOptions):TransformResult{
39+
asyncfunctiontransformVue(input:string,id:string,options:ResolvedOptions):Promise<TransformResult>{
4040
consts=newMagicString(input)
4141

42-
constsfc=parseSFC(input,id)
42+
constsfc=awaitparseSFC(input,id)
4343

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

‎src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}) => {
1717
transformInclude(id){
1818
returnfilter(id)
1919
},
20-
transform(code,id){
20+
asynctransform(code,id){
2121
try{
22-
returntransform(code,id,options)
22+
returnawaittransform(code,id,options)
2323
}
2424
catch(e:any){
2525
this.error(e)

‎test/errors.test.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { describe, expect, it, vi } from 'vitest'
22
import{transformast}from'../src'
33

44
describe('errors',()=>{
5-
it('langs',()=>{
6-
expect(()=>
5+
it('langs',async()=>{
6+
awaitexpect(()=>
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+
awaitexpect(()=>
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+
awaitexpect(()=>
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+
awaitexpect(()=>
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
constconsoleWarnMock=vi.spyOn(console,'warn')
5251

53-
expect(()=>
54-
t(`
52+
awaitt(`
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ describe('transform', () => {
1818
for(constfileoffiles){
1919
it(file.replace(/\\/g,'/'),async()=>{
2020
constfixture=awaitfs.readFile(resolve(root,file),'utf-8')
21-
constresult=transform(fixture,file,{reactivityTransform:true})?.code||fixture
21+
constresult=(awaittransform(fixture,file,{reactivityTransform:true}))?.code||fixture
2222
expect(result).toMatchSnapshot()
2323

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp