Skip to content

Commit 39e6c28

Browse files
zhiyuanzmjsxzz
andauthored
feat: introduce script-lang (#731)
Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
1 parent 9ee43b3 commit 39e6c28

File tree

33 files changed

+520
-4
lines changed

33 files changed

+520
-4
lines changed

.changeset/orange-readers-fetch.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"unplugin-vue-macros": minor
3+
"@vue-macros/volar": minor
4+
"@vue-macros/script-lang": minor
5+
---
6+
7+
introduce script-lang
8+

docs/.vitepress/locale.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ export function getLocaleConfig(lang: string) {
233233
text: 'booleanProp',
234234
link: `${urlPrefix}/features/boolean-prop`,
235235
},
236+
{
237+
text: 'scriptLang',
238+
link: `${urlPrefix}/features/script-lang`,
239+
},
236240
],
237241
},
238242
],

docs/features/script-lang.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# scriptLang <PackageVersion name="@vue-macros/setup-lang" />
2+
3+
<StabilityLevel level="experimental" />
4+
5+
Set the default language for `<script>` block.
6+
7+
::: tip
8+
Convert `<script setup>` to `<script setup lang="ts">`.
9+
:::
10+
11+
| Features | Supported |
12+
| :----------: | :----------------: |
13+
| Vue 3 | :white_check_mark: |
14+
| Nuxt 3 | :white_check_mark: |
15+
| Vue 2 | :white_check_mark: |
16+
| Volar Plugin | :white_check_mark: |
17+
18+
## Options
19+
20+
```ts
21+
interface Options {
22+
/**
23+
* @default 'ts'
24+
*/
25+
defaultLang?: 'ts' | 'tsx' | 'jsx' | string
26+
}
27+
```
28+
29+
## Usage
30+
31+
```vue twoslash
32+
<script setup>
33+
defineProps<{
34+
foo: string
35+
}>()
36+
</script>
37+
```
38+
39+
## Volar Configuration
40+
41+
```jsonc {6}
42+
// tsconfig.json
43+
{
44+
"vueCompilerOptions": {
45+
"target": 3,
46+
"plugins": [
47+
"@vue-macros/volar/script-lang",
48+
// ...more feature
49+
],
50+
},
51+
}
52+
```

docs/zh-CN/features/script-lang.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# scriptLang <PackageVersion name="@vue-macros/setup-lang" />
2+
3+
<StabilityLevel level="experimental" />
4+
5+
`<script>` 块设置默认语言。
6+
7+
::: tip
8+
`<script setup>` 转换为 `<script setup lang="ts">`.
9+
:::
10+
11+
| Features | Supported |
12+
| :----------: | :----------------: |
13+
| Vue 3 | :white_check_mark: |
14+
| Nuxt 3 | :white_check_mark: |
15+
| Vue 2 | :white_check_mark: |
16+
| Volar Plugin | :white_check_mark: |
17+
18+
## Options
19+
20+
```ts
21+
interface Options {
22+
/**
23+
* @default 'ts'
24+
*/
25+
defaultLang?: 'ts' | 'tsx' | 'jsx' | string
26+
}
27+
```
28+
29+
## Usage
30+
31+
```vue twoslash
32+
<script setup>
33+
defineProps<{
34+
foo: string
35+
}>()
36+
</script>
37+
```
38+
39+
## Volar Configuration
40+
41+
```jsonc {6}
42+
// tsconfig.json
43+
{
44+
"vueCompilerOptions": {
45+
"target": 3,
46+
"plugins": [
47+
"@vue-macros/volar/script-lang",
48+
// ...more feature
49+
],
50+
},
51+
}
52+
```

packages/macros/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"@vue-macros/jsx-directive": "workspace:*",
104104
"@vue-macros/named-template": "workspace:*",
105105
"@vue-macros/reactivity-transform": "workspace:*",
106+
"@vue-macros/script-lang": "workspace:*",
106107
"@vue-macros/setup-block": "workspace:*",
107108
"@vue-macros/setup-component": "workspace:*",
108109
"@vue-macros/setup-sfc": "workspace:*",

packages/macros/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ import VueNamedTemplate, {
7676
import VueReactivityTransform, {
7777
type Options as OptionsReactivityTransform,
7878
} from '@vue-macros/reactivity-transform'
79+
import VueScriptLang, {
80+
type Options as OptionsScriptLang,
81+
} from '@vue-macros/script-lang'
7982
import VueSetupBlock, {
8083
type Options as OptionsSetupBlock,
8184
} from '@vue-macros/setup-block'
@@ -117,6 +120,7 @@ export interface FeatureOptionsMap {
117120
jsxDirective: OptionsJsxDirective
118121
namedTemplate: OptionsNamedTemplate
119122
reactivityTransform: OptionsReactivityTransform
123+
scriptLang: OptionsScriptLang
120124
setupBlock: OptionsSetupBlock
121125
setupComponent: OptionsSetupComponent
122126
setupSFC: OptionsSetupSFC
@@ -176,6 +180,7 @@ export function resolveOptions({
176180
jsxDirective,
177181
namedTemplate,
178182
reactivityTransform,
183+
scriptLang,
179184
setupBlock,
180185
setupComponent,
181186
setupSFC,
@@ -266,6 +271,7 @@ export function resolveOptions({
266271
reactivityTransform,
267272
{ version },
268273
),
274+
scriptLang: resolveSubOptions<'scriptLang'>(scriptLang, { version }, false),
269275
setupBlock: resolveSubOptions<'setupBlock'>(setupBlock, { version }, false),
270276
setupComponent: resolveSubOptions<'setupComponent'>(setupComponent, {
271277
root,
@@ -324,6 +330,7 @@ const plugin: UnpluginCombineInstance<Options | undefined> =
324330
resolvePlugin(VueSetupSFC, framework, options.setupSFC),
325331
setupComponentPlugins?.[0],
326332
resolvePlugin(VueSetupBlock, framework, options.setupBlock),
333+
resolvePlugin(VueScriptLang, framework, options.scriptLang),
327334
namedTemplatePlugins?.[0],
328335

329336
// props

packages/script-lang/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @vue-macros/script-lang [![npm](https://img.shields.io/npm/v/@vue-macros/script-lang.svg)](https://npmjs.com/package/@vue-macros/script-lang)
2+
3+
Please refer to [README.md](https://github.com/vue-macros/vue-macros#readme)

packages/script-lang/package.json

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"name": "@vue-macros/script-lang",
3+
"version": "0.0.0",
4+
"packageManager": "pnpm@9.6.0",
5+
"description": "script-lang feature from Vue Macros.",
6+
"type": "module",
7+
"keywords": [
8+
"vue-macros",
9+
"macros",
10+
"vue",
11+
"sfc",
12+
"setup",
13+
"script-setup",
14+
"script-lang",
15+
"unplugin"
16+
],
17+
"license": "MIT",
18+
"homepage": "https://vue-macros.dev",
19+
"bugs": {
20+
"url": "https://github.com/vue-macros/vue-macros/issues"
21+
},
22+
"repository": {
23+
"type": "git",
24+
"url": "git+https://github.com/vue-macros/vue-macros.git",
25+
"directory": "packages/script-lang"
26+
},
27+
"author": "zhiyuanzmj",
28+
"contributors": [
29+
"三咲智子 <sxzz@sxzz.moe>"
30+
],
31+
"files": [
32+
"dist"
33+
],
34+
"main": "dist/index.cjs",
35+
"module": "dist/index.js",
36+
"types": "dist/index.d.ts",
37+
"exports": {
38+
".": {
39+
"dev": "./src/index.ts",
40+
"require": "./dist/index.cjs",
41+
"import": "./dist/index.js"
42+
},
43+
"./api": {
44+
"dev": "./src/api.ts",
45+
"require": "./dist/api.cjs",
46+
"import": "./dist/api.js"
47+
},
48+
"./esbuild": {
49+
"dev": "./src/esbuild.ts",
50+
"require": "./dist/esbuild.cjs",
51+
"import": "./dist/esbuild.js"
52+
},
53+
"./rollup": {
54+
"dev": "./src/rollup.ts",
55+
"require": "./dist/rollup.cjs",
56+
"import": "./dist/rollup.js"
57+
},
58+
"./vite": {
59+
"dev": "./src/vite.ts",
60+
"require": "./dist/vite.cjs",
61+
"import": "./dist/vite.js"
62+
},
63+
"./webpack": {
64+
"dev": "./src/webpack.ts",
65+
"require": "./dist/webpack.cjs",
66+
"import": "./dist/webpack.js"
67+
},
68+
"./*": "./*"
69+
},
70+
"typesVersions": {
71+
"*": {
72+
"*": [
73+
"./dist/*",
74+
"./*"
75+
]
76+
}
77+
},
78+
"scripts": {
79+
"build": "tsup",
80+
"dev": "DEV=true tsup"
81+
},
82+
"peerDependencies": {
83+
"vue": "^2.7.0 || ^3.0.0"
84+
},
85+
"dependencies": {
86+
"@vue-macros/common": "workspace:*",
87+
"unplugin": "catalog:"
88+
},
89+
"devDependencies": {
90+
"vue": "catalog:"
91+
},
92+
"engines": {
93+
"node": ">=16.14.0"
94+
}
95+
}

packages/script-lang/src/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './core'
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {
2+
generateTransform,
3+
MagicString,
4+
parseSFC,
5+
type CodeTransform,
6+
} from '@vue-macros/common'
7+
import type { OptionsResolved } from '..'
8+
9+
export function transformScriptLang(
10+
code: string,
11+
id: string,
12+
options: OptionsResolved,
13+
): CodeTransform | undefined {
14+
const s = new MagicString(code)
15+
const lang = ` lang="${options?.defaultLang || 'ts'}"`
16+
17+
const {
18+
sfc: {
19+
descriptor: { script, scriptSetup },
20+
},
21+
} = parseSFC(code, id)
22+
23+
if (script && !script.attrs.lang) {
24+
const start = script.loc.start.offset
25+
s.appendLeft(start - 1, lang)
26+
}
27+
if (scriptSetup && !scriptSetup.attrs.lang) {
28+
const start = scriptSetup.loc.start.offset
29+
s.appendLeft(start - 1, lang)
30+
}
31+
32+
return generateTransform(s, id)
33+
}

0 commit comments

Comments
 (0)