├── .editorconfig ├── .github ├── ISSUE_TEMPLATE │ └── ---bug-report.yml └── workflows │ ├── ci.yml │ ├── release-nightly.yml │ └── release.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENCE ├── README.md ├── build.config.ts ├── eslint.config.js ├── package.json ├── playground ├── index.js └── package.json ├── pnpm-lock.yaml ├── pnpm-workspace.yaml ├── renovate.json ├── src ├── block-loader │ ├── default.ts │ ├── script.ts │ ├── style.ts │ ├── template.ts │ └── types.ts ├── index.ts ├── mkdist.ts ├── plugins │ └── mkdist.ts ├── sfc-transformer.ts ├── types │ └── mkdist.ts └── utils │ ├── object.ts │ ├── script-setup.ts │ ├── string.ts │ └── template.ts ├── test ├── mkdist.test.ts ├── setup.test.ts └── template.test.ts ├── tsconfig.json └── vitest.config.ts /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | trim_trailing_whitespace = true 7 | charset = utf-8 8 | 9 | [*.js] 10 | indent_style = space 11 | indent_size = 2 12 | 13 | [{package.json,*.yml,*.cjson}] 14 | indent_style = space 15 | indent_size = 2 16 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/---bug-report.yml: -------------------------------------------------------------------------------- 1 | name: 🐛 Bug report 2 | description: Something's not working 3 | labels: [bug] 4 | body: 5 | - type: textarea 6 | validations: 7 | required: true 8 | attributes: 9 | label: 🐛 The bug 10 | description: What isn't working? Describe what the bug is. 11 | - type: input 12 | validations: 13 | required: true 14 | attributes: 15 | label: 🛠️ To reproduce 16 | description: A reproduction of the bug via https://stackblitz.com/github/nuxt-contrib/vue-sfc-transformer/tree/main/playground 17 | placeholder: https://stackblitz.com/[...] 18 | - type: textarea 19 | validations: 20 | required: true 21 | attributes: 22 | label: 🌈 Expected behaviour 23 | description: What did you expect to happen? Is there a section in the docs about this? 24 | - type: textarea 25 | attributes: 26 | label: ℹ️ Additional context 27 | description: Add any other context about the problem here. 28 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | push: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | lint: 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - uses: actions/checkout@v4 17 | - run: npm i -g --force corepack && corepack enable 18 | - uses: actions/setup-node@v4 19 | with: 20 | node-version: lts/* 21 | cache: pnpm 22 | 23 | - name: 📦 Install dependencies 24 | run: pnpm install 25 | 26 | - name: 🔠 Lint project 27 | run: pnpm lint 28 | 29 | - name: ✂️ Knip project 30 | run: pnpm test:knip 31 | 32 | - name: ⚙️ Check package engines 33 | run: pnpm test:versions 34 | 35 | test: 36 | runs-on: ubuntu-latest 37 | 38 | steps: 39 | - uses: actions/checkout@v4 40 | - run: npm i -g --force corepack && corepack enable 41 | - uses: actions/setup-node@v4 42 | with: 43 | node-version: lts/* 44 | cache: pnpm 45 | 46 | - name: 📦 Install dependencies 47 | run: pnpm install 48 | 49 | - name: 🛠 Build project 50 | run: pnpm build 51 | 52 | - name: 💪 Test types 53 | run: pnpm test:types 54 | 55 | - name: 🧪 Test project 56 | run: pnpm test:unit -- --coverage 57 | 58 | - name: 🟩 Coverage 59 | uses: codecov/codecov-action@v5 60 | -------------------------------------------------------------------------------- /.github/workflows/release-nightly.yml: -------------------------------------------------------------------------------- 1 | name: release 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | 11 | permissions: {} 12 | 13 | jobs: 14 | release-nightly: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 18 | - run: npm i -g --force corepack && corepack enable 19 | - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 20 | with: 21 | node-version: lts/* 22 | cache: pnpm 23 | 24 | - name: 📦 Install dependencies 25 | run: pnpm install 26 | 27 | - name: 🛠 Build project 28 | run: pnpm build 29 | 30 | - name: publish nightly release 31 | run: pnpm pkg-pr-new publish --compact 32 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: release 2 | 3 | permissions: 4 | contents: write 5 | 6 | on: 7 | push: 8 | tags: 9 | - 'v*' 10 | 11 | jobs: 12 | release: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v4 16 | with: 17 | fetch-depth: 0 18 | 19 | - run: npm i -g --force corepack && corepack enable 20 | - uses: actions/setup-node@v4 21 | with: 22 | node-version: lts/* 23 | cache: pnpm 24 | 25 | - name: 📦 Install dependencies 26 | run: pnpm install 27 | 28 | - run: pnpm changelogithub 29 | env: 30 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules 3 | coverage 4 | .vscode 5 | .DS_Store 6 | .eslintcache 7 | *.log* 8 | *.env* 9 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | - Using welcoming and inclusive language 12 | - Being respectful of differing viewpoints and experiences 13 | - Gracefully accepting constructive criticism 14 | - Focusing on what is best for the community 15 | - Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | - The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | - Trolling, insulting/derogatory comments, and personal or political attacks 21 | - Public or private harassment 22 | - Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | - Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies within all project spaces, and it also applies when an individual is representing the project or its community in public spaces. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [daniel@roe.dev](mailto:daniel@roe.dev). All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 44 | 45 | [homepage]: https://www.contributor-covenant.org 46 | 47 | For answers to common questions about this code of conduct, see https://www.contributor-covenant.org/faq 48 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Daniel Roe 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vue-sfc-transformer 2 | 3 | [![npm version][npm-version-src]][npm-version-href] 4 | [![npm downloads][npm-downloads-src]][npm-downloads-href] 5 | [![Github Actions][github-actions-src]][github-actions-href] 6 | [![Codecov][codecov-src]][codecov-href] 7 | 8 | > Tools for minimal TypeScript transpilation of Vue SFCs 9 | 10 | ## Usage 11 | 12 | Install package: 13 | 14 | ```sh 15 | # npm 16 | npm install vue-sfc-transformer vue @vue/compiler-core esbuild 17 | 18 | # pnpm 19 | pnpm install vue-sfc-transformer vue @vue/compiler-core esbuild 20 | ``` 21 | 22 | ```js 23 | import { parse as parseSFC } from '@vue/compiler-sfc' 24 | import { transform } from 'esbuild' 25 | 26 | import { preTranspileScriptSetup, transpileVueTemplate } from 'vue-sfc-transformer' 27 | 28 | const src = ` 29 | 30 | 31 | 32 | 33 | 38 | ` 39 | 40 | const sfc = parseSFC(src, { 41 | filename: 'test.vue', 42 | ignoreEmpty: true, 43 | }) 44 | 45 | // transpile template block 46 | const templateBlockContents = await transpileVueTemplate( 47 | sfc.descriptor.template.content, 48 | sfc.descriptor.template.ast, 49 | sfc.descriptor.template.loc.start.offset, 50 | async (code) => { 51 | const res = await transform(code, { loader: 'ts', target: 'esnext' }) 52 | return res.code 53 | }, 54 | ) 55 | console.log(templateBlockContents) 56 | //
57 | 58 | const { content: scriptBlockContents } = await preTranspileScriptSetup(sfc.descriptor, 'test.vue') 59 | console.log(scriptBlockContents) 60 | // defineProps({ 61 | // test: { type: String, required: false } 62 | // }) 63 | ``` 64 | 65 | If you are using `mkdist`, `vue-sfc-transformer` exports a loader you can use: 66 | 67 | ```ts 68 | import { vueLoader } from 'vue-sfc-transformer/mkdist' 69 | ``` 70 | 71 | > `mkdist` will automatically use the loader from `vue-sfc-transformer` when you pass `vue` to the `loaders` option and have this package installed. 72 | 73 | ## 💻 Development 74 | 75 | - Clone this repository 76 | - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` 77 | - Install dependencies using `pnpm install` 78 | - Run interactive tests using `pnpm dev` 79 | 80 | ## Credits 81 | 82 | This package was based on the work of contributors to [`mkdist`](https://github.com/unjs/mkdist), and in particular this PR by [**@Teages**](https://github.com/teages): [unjs/mkdist#300](https://github.com/unjs/mkdist/pull/300). 83 | 84 | ## License 85 | 86 | Made with ❤️ 87 | 88 | Published under [MIT License](./LICENCE). 89 | 90 | 91 | 92 | [npm-version-src]: https://img.shields.io/npm/v/vue-sfc-transformer?style=flat-square 93 | [npm-version-href]: https://npmjs.com/package/vue-sfc-transformer 94 | [npm-downloads-src]: https://img.shields.io/npm/dm/vue-sfc-transformer?style=flat-square 95 | [npm-downloads-href]: https://npm.chart.dev/vue-sfc-transformer 96 | [github-actions-src]: https://img.shields.io/github/actions/workflow/status/nuxt-contrvue-sfc-transformerransformer/ci.yml?branch=main&style=flat-square 97 | [github-actions-href]: https://github.com/nuxt-contrvue-sfc-transformerransformer/actions?query=workflow%3Aci 98 | [codecov-src]: https://img.shields.io/codecov/c/gh/nuxt-contrvue-sfc-transformerransformer/main?style=flat-square 99 | [codecov-href]: https://codecov.io/gh/nuxt-contrvue-sfc-transformerransformer 100 | -------------------------------------------------------------------------------- /build.config.ts: -------------------------------------------------------------------------------- 1 | import { defineBuildConfig } from 'unbuild' 2 | 3 | export default defineBuildConfig({ 4 | declaration: 'node16', 5 | externals: [ 6 | '@vue/compiler-dom', 7 | 'mkdist', 8 | ], 9 | rollup: { 10 | dts: { 11 | respectExternal: false, 12 | }, 13 | }, 14 | }) 15 | -------------------------------------------------------------------------------- /eslint.config.js: -------------------------------------------------------------------------------- 1 | import antfu from '@antfu/eslint-config' 2 | 3 | export default antfu().append({ 4 | files: ['playground/**'], 5 | rules: { 6 | 'antfu/no-top-level-await': 'off', 7 | 'no-console': 'off', 8 | }, 9 | }) 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vue-sfc-transformer", 3 | "type": "module", 4 | "version": "0.1.16", 5 | "packageManager": "pnpm@10.11.1", 6 | "description": "Tools for minimal TypeScript transpilation of Vue SFCs", 7 | "license": "MIT", 8 | "repository": "nuxt-contrib/vue-sfc-transformer", 9 | "sideEffects": false, 10 | "exports": { 11 | ".": "./dist/index.mjs", 12 | "./mkdist": "./dist/mkdist.mjs" 13 | }, 14 | "main": "./dist/index.mjs", 15 | "module": "./dist/index.mjs", 16 | "typesVersions": { 17 | "*": { 18 | ".": [ 19 | "./dist/index.d.mts" 20 | ], 21 | "mkdist": [ 22 | "./dist/mkdist.d.mts" 23 | ] 24 | } 25 | }, 26 | "files": [ 27 | "dist" 28 | ], 29 | "engines": { 30 | "node": ">=18.0.0" 31 | }, 32 | "scripts": { 33 | "build": "unbuild", 34 | "dev": "vitest dev", 35 | "lint": "eslint .", 36 | "prepare": "simple-git-hooks", 37 | "prepack": "pnpm build", 38 | "prepublishOnly": "pnpm lint && pnpm test", 39 | "release": "bumpp && pnpm publish", 40 | "test": "pnpm test:unit && pnpm test:types", 41 | "test:unit": "vitest", 42 | "test:knip": "knip", 43 | "test:versions": "installed-check -d --no-workspaces", 44 | "test:types": "tsc --noEmit" 45 | }, 46 | "peerDependencies": { 47 | "@vue/compiler-core": "^3.5.13", 48 | "esbuild": "*", 49 | "vue": "^3.5.13" 50 | }, 51 | "dependencies": { 52 | "@babel/parser": "^7.27.0" 53 | }, 54 | "devDependencies": { 55 | "@antfu/eslint-config": "4.13.2", 56 | "@babel/types": "7.27.3", 57 | "@types/node": "22.15.29", 58 | "@vitest/coverage-v8": "3.2.0", 59 | "@vue/compiler-core": "3.5.16", 60 | "@vue/compiler-dom": "3.5.16", 61 | "bumpp": "10.1.1", 62 | "changelogithub": "13.15.0", 63 | "esbuild": "0.25.5", 64 | "eslint": "9.28.0", 65 | "exsolve": "1.0.5", 66 | "installed-check": "9.3.0", 67 | "knip": "5.59.1", 68 | "lint-staged": "16.1.0", 69 | "mkdist": "2.3.0", 70 | "pkg-pr-new": "0.0.51", 71 | "simple-git-hooks": "2.13.0", 72 | "typescript": "5.8.3", 73 | "unbuild": "3.5.0", 74 | "vitest": "3.2.0", 75 | "vue": "3.5.16", 76 | "vue-tsc": "2.2.10" 77 | }, 78 | "resolutions": { 79 | "@vue/compiler-core": "3.5.16", 80 | "vue-sfc-transformer": "link:." 81 | }, 82 | "simple-git-hooks": { 83 | "pre-commit": "npx lint-staged" 84 | }, 85 | "lint-staged": { 86 | "*.{js,ts,mjs,cjs,json,.*rc}": [ 87 | "npx eslint --fix" 88 | ] 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /playground/index.js: -------------------------------------------------------------------------------- 1 | import { parse as parseSFC } from '@vue/compiler-sfc' 2 | import { transform } from 'esbuild' 3 | 4 | import { preTranspileScriptSetup, transpileVueTemplate } from 'vue-sfc-transformer' 5 | 6 | const src = ` 7 | 8 | 9 | 10 | 11 | 16 | ` 17 | 18 | const sfc = parseSFC(src, { 19 | filename: 'test.vue', 20 | ignoreEmpty: true, 21 | }) 22 | 23 | // transpile template block 24 | const templateBlockContents = await transpileVueTemplate( 25 | sfc.descriptor.template.content, 26 | sfc.descriptor.template.ast, 27 | sfc.descriptor.template.loc.start.offset, 28 | async (code) => { 29 | const res = await transform(code, { loader: 'ts', target: 'esnext' }) 30 | return res.code 31 | }, 32 | ) 33 | console.log(`transpiled block:`) 34 | console.log(`\`\`\`\n${templateBlockContents}\n\`\`\`\n`) 35 | 36 | // transpile script block 37 | // notice: it is still in typescript, you need to transpile it to javascript later 38 | const { content: scriptBlockContents } = await preTranspileScriptSetup(sfc.descriptor, 'test.vue') 39 | console.log(`transpiled \n\`\`\`\n`) 41 | -------------------------------------------------------------------------------- /playground/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "module", 3 | "private": true, 4 | "scripts": { 5 | "dev": "node index.js" 6 | }, 7 | "dependencies": { 8 | "vue-sfc-transformer": "latest" 9 | }, 10 | "devDependencies": { 11 | "@vue/compiler-sfc": "3.5.16", 12 | "esbuild": "0.25.5" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /pnpm-workspace.yaml: -------------------------------------------------------------------------------- 1 | packages: 2 | - playground 3 | ignoredBuiltDependencies: 4 | - '@parcel/watcher' 5 | - esbuild 6 | onlyBuiltDependencies: 7 | - simple-git-hooks 8 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "github>danielroe/renovate" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /src/block-loader/default.ts: -------------------------------------------------------------------------------- 1 | import type { BlockLoader, LoaderFile, LoadFileContext } from './types' 2 | import { toOmit } from '../utils/object' 3 | 4 | interface DefaultBlockLoaderOptions { 5 | type: 'script' | 'style' | 'template' | (string & {}) 6 | defaultLang: string 7 | validExtensions?: string[] 8 | } 9 | 10 | export function defineDefaultBlockLoader( 11 | options: DefaultBlockLoaderOptions, 12 | ): BlockLoader { 13 | return async (block, { isTs, loadFile }) => { 14 | if (options.type !== block.type) { 15 | return 16 | } 17 | 18 | const lang = typeof block.attrs.lang === 'string' 19 | ? block.attrs.lang 20 | : options.defaultLang 21 | const extension = `.${lang}` 22 | 23 | const input: LoaderFile = { extension, content: block.content } 24 | const context: LoadFileContext = { isTs, block } 25 | const files = await loadFile(input, context) || [] 26 | 27 | const output = files.find( 28 | file => file.extension === `.${options.defaultLang}` || options.validExtensions?.includes(file.extension), 29 | ) 30 | if (!output) { 31 | return 32 | } 33 | 34 | return { 35 | type: block.type, 36 | attrs: toOmit(block.attrs, ['lang', 'generic']), 37 | content: output.content, 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/block-loader/script.ts: -------------------------------------------------------------------------------- 1 | import type { BlockLoader, LoaderFile, LoadFileContext } from './types' 2 | import { toOmit } from '../utils/object' 3 | 4 | const scriptValidExtensions = new Set(['.js', '.cjs', '.mjs']) 5 | 6 | export const scriptLoader: BlockLoader = async (block, { isTs, loadFile }) => { 7 | if (block.type !== 'script') { 8 | return 9 | } 10 | 11 | const extension = isTs ? '.ts' : '.js' 12 | 13 | const input: LoaderFile = { extension, content: block.content } 14 | const context: LoadFileContext = { isTs, block } 15 | 16 | const files = await loadFile(input, context) || [] 17 | 18 | const output = files.find(file => scriptValidExtensions.has(file.extension)) 19 | if (!output) { 20 | return 21 | } 22 | 23 | return { 24 | type: block.type, 25 | attrs: toOmit(block.attrs, ['lang', 'generic']), 26 | content: output.content, 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/block-loader/style.ts: -------------------------------------------------------------------------------- 1 | import { defineDefaultBlockLoader } from './default' 2 | 3 | export const styleLoader = defineDefaultBlockLoader({ 4 | defaultLang: 'css', 5 | type: 'style', 6 | }) 7 | -------------------------------------------------------------------------------- /src/block-loader/template.ts: -------------------------------------------------------------------------------- 1 | import type { SFCTemplateBlock } from 'vue/compiler-sfc' 2 | import type { BlockLoader, LoadFileContext } from './types' 3 | import { transpileVueTemplate } from '../utils/template' 4 | 5 | const templateJsSnippetValidExtensions = new Set(['.js', '.cjs', '.mjs']) 6 | 7 | export const templateLoader: BlockLoader = async (block, { isTs, loadFile }) => { 8 | if (block.type !== 'template') { 9 | return 10 | } 11 | if (!isTs) { 12 | return 13 | } 14 | 15 | const typedBlock = block as SFCTemplateBlock 16 | 17 | const snippetExtension = isTs ? '.ts' : '.js' 18 | const context: LoadFileContext = { isTs, block } 19 | 20 | const transformed = await transpileVueTemplate( 21 | // for lower version of @vue/compiler-sfc, `ast.source` is the whole .vue file 22 | typedBlock.content, 23 | typedBlock.ast!, 24 | typedBlock.loc.start.offset, 25 | async (code) => { 26 | const res = await loadFile( 27 | { extension: snippetExtension, content: code }, 28 | context, 29 | ) 30 | 31 | return res?.find(f => templateJsSnippetValidExtensions.has(f.extension))?.content || code 32 | }, 33 | ) 34 | 35 | return { 36 | type: 'template', 37 | attrs: typedBlock.attrs, 38 | content: transformed, 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/block-loader/types.ts: -------------------------------------------------------------------------------- 1 | import type { SFCBlock, SFCParseResult } from 'vue/compiler-sfc' 2 | 3 | export interface LoaderFile { 4 | extension: string 5 | content: string 6 | } 7 | 8 | export interface LoadFileContext { 9 | isTs: boolean 10 | block: SFCBlock 11 | } 12 | 13 | export interface BlockLoaderContext { 14 | /** 15 | * Whether the SFC is using TypeScript 16 | */ 17 | isTs: boolean 18 | 19 | /** 20 | * Relative path to the SFC 21 | */ 22 | path: string 23 | 24 | /** 25 | * Absolute path to the SFC 26 | */ 27 | srcPath: string 28 | 29 | /** 30 | * Raw content of the SFC 31 | */ 32 | raw: string 33 | 34 | /** 35 | * Parsed SFC 36 | */ 37 | sfc: SFCParseResult 38 | 39 | loadFile: (input: LoaderFile, context: LoadFileContext) => Promise{{ (data as any).value }}
{{ data.value }}