Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ npm install vue-sfc-transformer vue @vue/compiler-core
pnpm install vue-sfc-transformer vue @vue/compiler-core
```

A TypeScript transpiler is needed to strip types from script blocks and template expressions. `rolldown` and `esbuild` are both optional peer dependencies; install whichever suits your setup. The `mkdist` loader uses `rolldown/utils` when it is available, and `esbuild` otherwise (or when mkdist's `esbuild` options are set).
TypeScript types in template expressions are stripped by `petrea` transpiler, which blanks them in place to preserve line and column positions.

```js
import { parse as parseSFC } from '@vue/compiler-sfc'
import { transform } from 'rolldown/utils'

import { preTranspileScriptSetup, transpileVueTemplate } from 'vue-sfc-transformer'

const src = `
Expand All @@ -49,17 +47,9 @@ const templateBlockContents = await transpileVueTemplate(
sfc.descriptor.template.content,
sfc.descriptor.template.ast,
sfc.descriptor.template.loc.start.offset,
async (code) => {
const res = await transform('__sfc.ts', code, {
lang: 'ts',
sourcemap: false,
typescript: { onlyRemoveTypeImports: true },
})
return res.code
},
)
console.log(templateBlockContents)
// <div v-if="test" />
// <div v-if="test " />

const { content: scriptBlockContents } = await preTranspileScriptSetup(sfc.descriptor, 'test.vue')
console.log(scriptBlockContents)
Expand All @@ -80,7 +70,7 @@ import { vueLoader } from 'vue-sfc-transformer/mkdist'

`vue-sfc-transformer/rolldown` ships a [rolldown](https://github.com/rolldown/rolldown) plugin that transpiles `<script lang="ts">` and template expressions, then emits a `<name>.d.vue.ts` declaration for each SFC under `srcDir`. That's the form `vue-tsc` / `@vue/language-core` / `@volar/typescript` (since 2.4.19) resolve for `import './Foo.vue'`. Pass `emitLegacyDeclarationAlias: true` to also emit the older `<name>.vue.d.ts` form, which plain `tsc` resolves but vue-tsc does not.

Script blocks are transpiled with `verbatimModuleSyntax` semantics: only explicit `import type` statements and inline `type` specifiers are removed. A type-only import written without the `type` keyword (`import { Props } from './types'`) survives into the emitted SFC as a runtime import and will fail to resolve at runtime; make sure to write `import type` for types.
Script blocks are transpiled by the `petrea` transpiler with `verbatimModuleSyntax` semantics: only explicit `import type` statements and inline `type` specifiers are removed. A type-only import written without the `type` keyword (`import { Props } from './types'`) survives into the emitted SFC as a runtime import and will fail to resolve at runtime; make sure to write `import type` for types.

Works with anything that runs rolldown plugins: [tsdown](https://github.com/rolldown/tsdown), [obuild](https://github.com/unjs/obuild) or rolldown directly.

Expand Down
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dist"
],
"engines": {
"node": ">=18.0.0"
"node": ">=20.0.0"
},
"scripts": {
"build": "tsdown",
Expand All @@ -50,7 +50,6 @@
"@volar/typescript": "^2.4.0",
"@vue/compiler-core": "^3.5.13",
"@vue/language-core": "^3.0.0",
"esbuild": "*",
"rolldown": ">=1.0.0-beta.0",
"typescript": ">=5.0.0 <7.0.0-0",
"vue": "^3.5.13"
Expand All @@ -62,9 +61,6 @@
"@vue/language-core": {
"optional": true
},
"esbuild": {
"optional": true
},
"rolldown": {
"optional": true
},
Expand All @@ -73,6 +69,7 @@
}
},
"dependencies": {
"petrea": "catalog:prod",
"tinyglobby": "catalog:prod"
},
"devDependencies": {
Expand All @@ -84,7 +81,6 @@
"@vue/compiler-core": "catalog:peer",
"@vue/compiler-dom": "catalog:dev",
"@vue/language-core": "catalog:dev",
"esbuild": "catalog:peer",
"eslint": "catalog:dev",
"exsolve": "catalog:dev",
"installed-check": "catalog:dev",
Expand Down
5 changes: 0 additions & 5 deletions playground/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { parse as parseSFC } from '@vue/compiler-sfc'
import { transform } from 'esbuild'

import { preTranspileScriptSetup, transpileVueTemplate } from 'vue-sfc-transformer'

Expand All @@ -25,10 +24,6 @@ const templateBlockContents = await transpileVueTemplate(
sfc.descriptor.template.content,
sfc.descriptor.template.ast,
sfc.descriptor.template.loc.start.offset,
async (code) => {
const res = await transform(code, { loader: 'ts', target: 'esnext' })
return res.code
},
)
console.log(`transpiled <template> block:`)
console.log(`\`\`\`\n<template>${templateBlockContents}</template>\n\`\`\`\n`)
Expand Down
3 changes: 1 addition & 2 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"vue-sfc-transformer": "workspace:*"
},
"devDependencies": {
"@vue/compiler-sfc": "catalog:dev",
"esbuild": "catalog:peer"
"@vue/compiler-sfc": "catalog:dev"
}
}
112 changes: 103 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ trustPolicy: no-downgrade
trustPolicyExclude:
- '@octokit/action@6.1.0'
- '@octokit/endpoint@9.0.6'
- '@octokit/plugin-paginate-rest@9.2.2'
- '@octokit/plugin-paginate-rest@9.2.0'
- chokidar@4.0.3
- undici@5.29.0 || 6.22.0

Expand Down Expand Up @@ -48,7 +48,7 @@ catalogs:
vue-tsc: 3.3.14
peer:
'@vue/compiler-core': 3.5.45
esbuild: 0.28.2
vue: 3.5.45
prod:
petrea: ^0.5.1
tinyglobby: ^0.2.16
17 changes: 2 additions & 15 deletions src/block-loader/template.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { SFCTemplateBlock } from 'vue/compiler-sfc'
import type { BlockLoader, LoadFileContext } from './types'
import type { BlockLoader } from './types'
import { transpileVueTemplate } from '../utils/template'

const templateJsSnippetValidExtensions = new Set(['.js', '.cjs', '.mjs'])

export const templateLoader: BlockLoader = async (block, { isTs, loadFile }) => {
export const templateLoader: BlockLoader = async (block, { isTs }) => {
if (block.type !== 'template') {
return
}
Expand All @@ -14,22 +12,11 @@ export const templateLoader: BlockLoader = async (block, { isTs, loadFile }) =>

const typedBlock = block as SFCTemplateBlock

const snippetExtension = isTs ? '.ts' : '.js'
const context: LoadFileContext = { isTs, block }

const transformed = await transpileVueTemplate(
// for lower version of @vue/compiler-sfc, `ast.source` is the whole .vue file
typedBlock.content,
typedBlock.ast!,
typedBlock.loc.start.offset,
async (code) => {
const res = await loadFile(
{ extension: snippetExtension, content: code },
context,
)

return res?.find(f => templateJsSnippetValidExtensions.has(f.extension))?.content || code
},
)

return {
Expand Down
Loading
Loading