Skip to content

Commit

Permalink
fix: support jsx by default
Browse files Browse the repository at this point in the history
closes #198
  • Loading branch information
sxzz committed Jan 6, 2023
1 parent 230c80e commit af9978b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .changeset/loud-crews-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'unplugin-vue-define-options': patch
'@vue-macros/common': patch
---

support jsx by default
10 changes: 5 additions & 5 deletions packages/common/src/ast.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { babelParse as _babelParse, walkIdentifiers } from '@vue/compiler-sfc'
import { walk } from 'estree-walker'
import { REGEX_JSX_FILE } from './constants'
import { REGEX_LANG_JSX } from './constants'
import { isTs } from './lang'
import type {
CallExpression,
Expand All @@ -21,10 +21,10 @@ export function babelParse(
options: ParserOptions = {}
): Program {
const plugins: ParserPlugin[] = []
if (lang) {
if (isTs(lang)) plugins.push('typescript')
if (REGEX_JSX_FILE.test(lang)) plugins.push('jsx')
}
if (isTs(lang)) {
plugins.push('typescript')
if (REGEX_LANG_JSX.test(lang!)) plugins.push('jsx')
} else plugins.push('jsx')
const { program } = _babelParse(code, {
sourceType: 'module',
plugins,
Expand Down
5 changes: 3 additions & 2 deletions packages/common/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ export const REPO_ISSUE_URL =
'https://github.com/sxzz/unplugin-vue-macros/issues'

export const REGEX_SRC_FILE = /\.[cm]?[jt]sx?$/
export const REGEX_TS_FILE = /^[cm]?tsx?$/
export const REGEX_JSX_FILE = /^[cm]?[jt]sx$/
export const REGEX_SETUP_SFC = /\.setup\.[cm]?[jt]sx?$/
export const REGEX_VUE_SFC = /\.vue$/
export const REGEX_VUE_SUB = /\.vue\?vue&/

export const REGEX_LANG_TS = /^[cm]?tsx?$/
export const REGEX_LANG_JSX = /^[cm]?[jt]sx$/
4 changes: 2 additions & 2 deletions packages/common/src/lang.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from 'node:path'
import { REGEX_TS_FILE } from './constants'
import { REGEX_LANG_TS } from './constants'

export function getLang(filename: string) {
return path.extname(filename).replace(/^\./, '')
}

export function isTs(lang?: string) {
return lang && REGEX_TS_FILE.test(lang)
return lang && REGEX_LANG_TS.test(lang)
}
29 changes: 23 additions & 6 deletions packages/define-options/tests/__snapshots__/rollup.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,17 @@ export { empty as default };
"
`;
exports[`Rollup > fixtures > tests/fixtures/error1.vue 1`] = `[RollupError: unplugin-vue-define-options SyntaxError: \`defineOptions()\` in <script setup> cannot reference locally declared variables (name) because it will be hoisted outside of the setup() function.]`;
exports[`Rollup > fixtures > tests/fixtures/error1.vue 1`] = `"unplugin-vue-define-options SyntaxError: \`defineOptions()\` in <script setup> cannot reference locally declared variables (name) because it will be hoisted outside of the setup() function."`;
exports[`Rollup > fixtures > tests/fixtures/error2.vue 1`] = `[RollupError: unplugin-vue-define-options SyntaxError: \`defineOptions()\` in <script setup> cannot reference locally declared variables (foo) because it will be hoisted outside of the setup() function.]`;
exports[`Rollup > fixtures > tests/fixtures/error2.vue 1`] = `"unplugin-vue-define-options SyntaxError: \`defineOptions()\` in <script setup> cannot reference locally declared variables (foo) because it will be hoisted outside of the setup() function."`;
exports[`Rollup > fixtures > tests/fixtures/error3.vue 1`] = `[RollupError: unplugin-vue-define-options SyntaxError: defineOptions cannot be used with default export within <script>.]`;
exports[`Rollup > fixtures > tests/fixtures/error3.vue 1`] = `"unplugin-vue-define-options SyntaxError: defineOptions cannot be used with default export within <script>."`;
exports[`Rollup > fixtures > tests/fixtures/error4.vue 1`] = `[RollupError: unplugin-vue-define-options SyntaxError: defineOptions() please use defineProps or defineEmits instead.]`;
exports[`Rollup > fixtures > tests/fixtures/error4.vue 1`] = `"unplugin-vue-define-options SyntaxError: defineOptions() please use defineProps or defineEmits instead."`;
exports[`Rollup > fixtures > tests/fixtures/error5.vue 1`] = `[RollupError: unplugin-vue-define-options SyntaxError: defineOptions() please use defineProps or defineEmits instead.]`;
exports[`Rollup > fixtures > tests/fixtures/error5.vue 1`] = `"unplugin-vue-define-options SyntaxError: defineOptions() please use defineProps or defineEmits instead."`;
exports[`Rollup > fixtures > tests/fixtures/error6.vue 1`] = `[RollupError: unplugin-vue-define-options SyntaxError: duplicate defineOptions() call]`;
exports[`Rollup > fixtures > tests/fixtures/error6.vue 1`] = `"unplugin-vue-define-options SyntaxError: duplicate defineOptions() call"`;
exports[`Rollup > fixtures > tests/fixtures/function.vue 1`] = `
"var _function = \`<script lang=\\"ts\\">
Expand Down Expand Up @@ -266,6 +266,23 @@ export { js as default };
"
`;
exports[`Rollup > fixtures > tests/fixtures/jsx.vue 1`] = `
"var jsx = \`<script>
import { defineComponent as DO_defineComponent } from 'vue';
export default /*#__PURE__*/ DO_defineComponent({
name: 'Jsx',
});
</script>
<script setup>
console.log(<h1>jsx</h1>)
</script>
\`;
export { jsx as default };
"
`;
exports[`Rollup > fixtures > tests/fixtures/no-transform.vue 1`] = `
"var noTransform = \`<script setup>
console.log('setup')
Expand Down
6 changes: 6 additions & 0 deletions packages/define-options/tests/fixtures/jsx.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script setup>
defineOptions({
name: 'Jsx',
})
console.log(<h1>jsx</h1>)
</script>
13 changes: 7 additions & 6 deletions packages/define-options/tests/rollup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ describe('Rollup', () => {
for (const file of files) {
it(file.replace(/\\/g, '/'), async () => {
const filepath = resolve(root, file)

const code = await rollupBuild(filepath, [
VueDefineOptions({}),
RollupToStringPlugin(),
]).catch((err) => err)
expect(code).toMatchSnapshot()
const exec = () =>
rollupBuild(filepath, [VueDefineOptions({}), RollupToStringPlugin()])
if (file.includes('error')) {
await expect(exec()).rejects.toThrowErrorMatchingSnapshot()
} else {
await expect(exec()).resolves.toMatchSnapshot()
}
})
}
})
Expand Down

0 comments on commit af9978b

Please sign in to comment.