-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@vue-macros/setup-block': patch | ||
--- | ||
|
||
support `<setup>` block for shortcut of `<script setup lang="ts">` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @vue-macros/setup-block [![npm](https://img.shields.io/npm/v/@vue-macros/setup-block.svg)](https://npmjs.com/package/@vue-macros/setup-block) | ||
|
||
Please refer to [README.md](https://github.com/sxzz/unplugin-vue-macros#readme) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
{ | ||
"name": "@vue-macros/setup-block", | ||
"version": "0.0.0", | ||
"packageManager": "pnpm@7.17.0", | ||
"description": "", | ||
"keywords": [ | ||
"unplugin", | ||
"vue", | ||
"sfc", | ||
"setup", | ||
"macros", | ||
"script-setup", | ||
"setup-block" | ||
], | ||
"license": "MIT", | ||
"homepage": "https://github.com/sxzz/unplugin-vue-macros#readme", | ||
"bugs": { | ||
"url": "https://github.com/sxzz/unplugin-vue-macros/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/sxzz/unplugin-vue-macros.git" | ||
}, | ||
"author": "三咲智子 <sxzz@sxzz.moe>", | ||
"files": [ | ||
"dist" | ||
], | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "index.d.ts", | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.js", | ||
"import": "./dist/index.mjs" | ||
}, | ||
"./vite": { | ||
"require": "./dist/vite.js", | ||
"import": "./dist/vite.mjs" | ||
}, | ||
"./webpack": { | ||
"require": "./dist/webpack.js", | ||
"import": "./dist/webpack.mjs" | ||
}, | ||
"./rollup": { | ||
"require": "./dist/rollup.js", | ||
"import": "./dist/rollup.mjs" | ||
}, | ||
"./esbuild": { | ||
"require": "./dist/esbuild.js", | ||
"import": "./dist/esbuild.mjs" | ||
}, | ||
"./*": "./*" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"./dist/*", | ||
"./*" | ||
] | ||
} | ||
}, | ||
"scripts": { | ||
"build": "tsup && tsx ../../scripts/postbuild.mts", | ||
"dev": "DEV=1 tsup" | ||
}, | ||
"dependencies": { | ||
"@rollup/pluginutils": "^4.2.1", | ||
"@vue-macros/common": "workspace:~", | ||
"@vue/compiler-dom": "^3.2.45", | ||
"unplugin": "^1.0.0" | ||
}, | ||
"engines": { | ||
"node": ">=14.19.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { MagicString, getTransformResult } from '@vue-macros/common' | ||
import { parse } from '@vue/compiler-dom' | ||
|
||
export const transformSetupBlock = ( | ||
code: string, | ||
id: string, | ||
lang?: string | ||
) => { | ||
const s = new MagicString(code) | ||
|
||
const node = parse(code, { | ||
// there are no components at SFC parsing level | ||
isNativeTag: () => true, | ||
// preserve all whitespaces | ||
isPreTag: () => true, | ||
getTextMode: ({ tag, props }, parent) => { | ||
// all top level elements except <template> are parsed as raw text | ||
// containers | ||
if ( | ||
(!parent && tag !== 'template') || | ||
// <template lang="xxx"> should also be treated as raw text | ||
(tag === 'template' && | ||
props.some( | ||
(p) => | ||
p.type === 6 /* NodeTypes.ATTRIBUTE */ && | ||
p.name === 'lang' && | ||
p.value && | ||
p.value.content && | ||
p.value.content !== 'html' | ||
)) | ||
) { | ||
return 2 /* TextModes.RAWTEXT */ | ||
} else { | ||
return 0 /* TextModes.DATA */ | ||
} | ||
}, | ||
}) | ||
for (const child of node.children) { | ||
if (child.type === 1 && child.tag === 'setup') { | ||
const hasLang = child.props.some((p) => p.name === 'lang') | ||
let codegen = 'script setup' | ||
if (!hasLang && lang) { | ||
codegen += ` lang="${lang}"` | ||
} | ||
s.overwrite( | ||
child.loc.start.offset + 1 /* '<'.length */, | ||
child.loc.start.offset + 6 /* '<setup'.length */, | ||
codegen | ||
) | ||
s.overwrite( | ||
child.loc.end.offset - 6 /* `setup>`.length */, | ||
child.loc.end.offset - 1 /* '>'.length */, | ||
'script' | ||
) | ||
} | ||
} | ||
|
||
return getTransformResult(s, id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import unplugin from '.' | ||
|
||
export default unplugin.esbuild |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { createUnplugin } from 'unplugin' | ||
import { createFilter } from '@rollup/pluginutils' | ||
import { REGEX_SETUP_SFC, REGEX_VUE_SFC } from '@vue-macros/common' | ||
import { transformSetupBlock } from './core' | ||
import type { FilterPattern } from '@rollup/pluginutils' | ||
|
||
export interface Options { | ||
include?: FilterPattern | ||
exclude?: FilterPattern | ||
defaultLang?: string | ||
} | ||
|
||
export type OptionsResolved = Omit<Required<Options>, 'exclude'> & { | ||
exclude?: FilterPattern | ||
} | ||
|
||
function resolveOption(options: Options): OptionsResolved { | ||
return { | ||
include: [REGEX_VUE_SFC, REGEX_SETUP_SFC], | ||
defaultLang: 'ts', | ||
...options, | ||
} | ||
} | ||
|
||
const name = 'unplugin-vue-setup-block' | ||
|
||
export default createUnplugin((userOptions: Options = {}) => { | ||
const options = resolveOption(userOptions) | ||
const filter = createFilter(options.include, options.exclude) | ||
|
||
return { | ||
name, | ||
enforce: 'pre', | ||
|
||
transformInclude(id) { | ||
return filter(id) | ||
}, | ||
|
||
transform(code, id) { | ||
try { | ||
return transformSetupBlock(code, id, options.defaultLang) | ||
} catch (err: unknown) { | ||
this.error(`${name} ${err}`) | ||
} | ||
}, | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import unplugin from '.' | ||
|
||
export default unplugin.rollup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import unplugin from '.' | ||
import type {} from 'vite' | ||
|
||
export default unplugin.vite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import unplugin from '.' | ||
|
||
export default unplugin.webpack |
23 changes: 23 additions & 0 deletions
23
packages/setup-block/tests/__snapshots__/fixtures.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Vitest Snapshot v1 | ||
|
||
exports[`fixtures > ./fixtures/basic.vue 1`] = ` | ||
"<script setup lang=\\"magic\\"> | ||
import { ref } from 'vue' | ||
console.log('Hello World') | ||
const msg = ref('Hello World') | ||
</script> | ||
" | ||
`; | ||
exports[`fixtures > ./fixtures/with-lang.vue 1`] = ` | ||
"<script setup lang=\\"ts\\"> | ||
import { ref } from 'vue' | ||
console.log('Hello World') | ||
const msg = ref('Hello World') | ||
</script> | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { transformSetupBlock } from '../src/core' | ||
|
||
describe('fixtures', () => { | ||
const files = import.meta.glob('./fixtures/*.vue', { | ||
eager: true, | ||
as: 'raw', | ||
}) | ||
|
||
for (const [id, code] of Object.entries(files)) { | ||
test(id.replace(/\\/g, '/'), () => { | ||
const exec = () => transformSetupBlock(code, id, 'magic')?.code | ||
|
||
if (id.includes('error')) { | ||
expect(exec).toThrowErrorMatchingSnapshot() | ||
} else { | ||
expect(exec()).toMatchSnapshot() | ||
} | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<setup> | ||
import { ref } from 'vue' | ||
|
||
console.log('Hello World') | ||
|
||
const msg = ref('Hello World') | ||
</setup> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<setup lang="ts"> | ||
import { ref } from 'vue' | ||
|
||
console.log('Hello World') | ||
|
||
const msg = ref('Hello World') | ||
</setup> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import cfg from '../common/tsup.config.js' | ||
|
||
export default cfg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<setup> | ||
import { ref } from 'vue' | ||
const msg = ref('Hello World') | ||
</setup> | ||
|
||
<template> | ||
<div>setupBlock</div> | ||
{{ msg }} | ||
</template> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.