Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use both typescript and ecmascript prasers #687

Merged
merged 2 commits into from
May 2, 2024
Merged
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
6 changes: 2 additions & 4 deletions packages/waku/src/lib/plugins/vite-plugin-rsc-analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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' &&
Expand Down
11 changes: 3 additions & 8 deletions packages/waku/src/lib/plugins/vite-plugin-rsc-delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' &&
Expand Down Expand Up @@ -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:')) {
Expand Down
14 changes: 14 additions & 0 deletions packages/waku/src/lib/utils/swc.ts
Original file line number Diff line number Diff line change
@@ -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;
};
Loading