From 04dd67f0f85288567cb3af61e328fae146130234 Mon Sep 17 00:00:00 2001 From: daishi Date: Tue, 30 Apr 2024 09:56:31 +0900 Subject: [PATCH] fix: use both typescript and ecmascript prasers --- .../src/lib/plugins/vite-plugin-rsc-analyze.ts | 6 ++---- .../src/lib/plugins/vite-plugin-rsc-delegate.ts | 11 +++-------- packages/waku/src/lib/utils/swc.ts | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 packages/waku/src/lib/utils/swc.ts diff --git a/packages/waku/src/lib/plugins/vite-plugin-rsc-analyze.ts b/packages/waku/src/lib/plugins/vite-plugin-rsc-analyze.ts index 7ca7b3932..ee197f6ea 100644 --- a/packages/waku/src/lib/plugins/vite-plugin-rsc-analyze.ts +++ b/packages/waku/src/lib/plugins/vite-plugin-rsc-analyze.ts @@ -3,6 +3,7 @@ import * as swc from '@swc/core'; import { EXTENSIONS } from '../config.js'; import { extname } from '../utils/path.js'; +import { parseOpts } from '../utils/swc.js'; // HACK: Is it common to depend on another plugin like this? import { rscTransformPlugin } from './vite-plugin-rsc-transform.js'; @@ -30,10 +31,7 @@ export function rscAnalyzePlugin( async transform(code, id, options) { const ext = extname(id); if (EXTENSIONS.includes(ext)) { - const mod = swc.parseSync(code, { - syntax: 'typescript', - tsx: ext.endsWith('x'), - }); + const mod = swc.parseSync(code, parseOpts(ext)); for (const item of mod.body) { if ( item.type === 'ExpressionStatement' && diff --git a/packages/waku/src/lib/plugins/vite-plugin-rsc-delegate.ts b/packages/waku/src/lib/plugins/vite-plugin-rsc-delegate.ts index d2469d962..df227685a 100644 --- a/packages/waku/src/lib/plugins/vite-plugin-rsc-delegate.ts +++ b/packages/waku/src/lib/plugins/vite-plugin-rsc-delegate.ts @@ -3,15 +3,13 @@ import * as swc from '@swc/core'; import { EXTENSIONS } from '../config.js'; import { extname } from '../utils/path.js'; +import { parseOpts } from '../utils/swc.js'; import type { HotUpdatePayload } from './vite-plugin-rsc-hmr.js'; const isClientEntry = (id: string, code: string) => { const ext = extname(id); if (EXTENSIONS.includes(ext)) { - const mod = swc.parseSync(code, { - syntax: 'typescript', - tsx: ext.endsWith('x'), - }); + const mod = swc.parseSync(code, parseOpts(ext)); for (const item of mod.body) { if ( item.type === 'ExpressionStatement' && @@ -102,10 +100,7 @@ export function rscDelegatePlugin( async transform(code, id) { const ext = extname(id); if (mode === 'development' && EXTENSIONS.includes(ext)) { - const mod = swc.parseSync(code, { - syntax: 'typescript', - tsx: ext.endsWith('x'), - }); + const mod = swc.parseSync(code, parseOpts(ext)); for (const item of mod.body) { if (item.type === 'ImportDeclaration') { if (item.source.value.startsWith('virtual:')) { diff --git a/packages/waku/src/lib/utils/swc.ts b/packages/waku/src/lib/utils/swc.ts new file mode 100644 index 000000000..b6b149ef7 --- /dev/null +++ b/packages/waku/src/lib/utils/swc.ts @@ -0,0 +1,14 @@ +export const parseOpts = (ext: string) => { + if (ext === '.ts' || ext === '.tsx') { + return { + syntax: 'typescript', + tsx: ext.endsWith('x'), + } as const; + } + // We hoped to use 'typescript' for everything, but it fails in some cases. + // https://github.com/dai-shi/waku/issues/677 + return { + syntax: 'ecmascript', + jsx: ext.endsWith('x'), + } as const; +};