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

JSX in TypeScript namespace causes error #7

Closed
7 tasks done
AmazingShine opened this issue Oct 24, 2022 · 2 comments
Closed
7 tasks done

JSX in TypeScript namespace causes error #7

AmazingShine opened this issue Oct 24, 2022 · 2 comments
Labels
p2-has-workaround 🍰 Bug, but has workaround (priority)

Comments

@AmazingShine
Copy link

AmazingShine commented Oct 24, 2022

Describe the bug

Jsx in namespace is not supported, similar to this bug: #22413

Reproduction

pnpm create vite demo --template react-ts

Steps to reproduce

https://stackblitz.com/edit/vitejs-vite-o1zmeh?file=src%2FApp.tsx&terminal=dev

Create a official demo: pnpm create vite demo --template react-ts.
And then write the following code:

import { useState } from 'react'
import reactLogo from './assets/react.svg'
import './App.css'

namespace AAA {
    export const Hello = () => <div>Hello</div>
}

function App() {
  const [count, setCount] = useState(0)

  return (
    <div className="App">
        <AAA.Hello></AAA.Hello>
    </div>
  )
}

export default App

System Info

System:
    OS: Linux 5.19 Arch Linux
    CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11700K @ 3.60GHz
    Memory: 20.67 GB / 31.17 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 16.16.0 - ~/.nvm/versions/node/v16.16.0/bin/node
    Yarn: 1.22.10 - /usr/bin/yarn
    npm: 8.11.0 - ~/.nvm/versions/node/v16.16.0/bin/npm

Used Package Manager

pnpm

Logs

This error occured:

Internal server error: Transform failed with 1 error:
.../demo/src/App.tsx:14:5: ERROR: Cannot use "this" here:

Validations

@sapphi-red sapphi-red changed the title JSX in namespace causes error JSX in TypeScript namespace causes error Oct 25, 2022
@sapphi-red
Copy link
Member

sapphi-red commented Oct 25, 2022

For automatic runtime, Vite uses babel to transform JSX and esbuild to transform TS.

// input
namespace AAA {
  export const Hello = () => <div>Hello</div>
}

// output from babel
namespace AAA {
  export const Hello = () => /*#__PURE__*/_jsxDEV("div", {
    children: "Hello"
  }, void 0, false, {
    fileName: _jsxFileName,
    lineNumber: 5,
    columnNumber: 30
  }, this);
  _c = Hello;
}

This output uses this in namespace and esbuild throws an error for that. Vite needs to inject a custom babel plugin to babel to transform this this to void 0. vitejs/vite#9590 might fix this indirectly.


For classic runtime, Vite uses babel to add debug information and esbuild to transform JSX+TS.

// input
namespace AAA {
  export const Hello = () => <div>Hello</div>
}

// output from babel
namespace AAA {
  export const Hello = () => <div __self={this} __source={{
    fileName: _jsxFileName,
    lineNumber: 5,
    columnNumber: 30
  }}>Hello</div>;
  _c = Hello;
}

We need to use a similar babel plugin for this case too.

@patak-dev patak-dev transferred this issue from vitejs/vite Dec 3, 2022
@sapphi-red sapphi-red added the p2-has-workaround 🍰 Bug, but has workaround (priority) label Dec 19, 2022
@sapphi-red
Copy link
Member

Closing as I confirmed plugin-react 3.0.0 works with this. plugin-react now uses esbuild to transform JSX and I think that fixed this.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-has-workaround 🍰 Bug, but has workaround (priority)
Projects
None yet
Development

No branches or pull requests

2 participants