Parcel 无法解析“solid-js/jsx-runtime”对于 TSX 文件

发布于 2025-01-14 01:30:14 字数 1548 浏览 2 评论 0原文

我正在将 React Webpack 项目迁移到 SolidJS Parcel 项目中。然而,SolidJS 和 Parcel 的组合对于嵌套的 Typescript 项目似乎存在问题,而 React 和 Webpack 的组合则没有。

在我的最小示例项目中,我收到错误:@parcel/core:无法从“./foo/src/index.tsx”解析“solid-js/jsx-runtime”。如果我尝试将 index.tsx 重命名为 index.jsx,Parcel 可以构建相同的文件而不会出现任何错误。但是,我想使用 Typescript 而不是 JavaScript。

/.babelrc:

{
  "presets": [ "solid" ]
}

/package.json:

{
  "private": true,
  "dependencies": {
    "solid-js": "1.3.12"
  },
  "devDependencies": {
    "babel-preset-solid": "1.3.12",
    "parcel": "2.3.2",
    "typescript": "4.6.2"
  }
}

/tsconfig.json:

{
    "compilerOptions": {
        "isolatedModules": true,
        "jsx": "preserve",
        "jsxImportSource": "solid-js",
        "lib": [
            "dom",
            "es2021"
        ],
        "module": "commonjs",
        "noEmit": true,
        "noImplicitAny": true,
        "noImplicitReturns": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "sourceMap": true,
        "strict": true,
        "target": "es5"
    }
}

/foo/src/index.tsx:

export function HelloWorld() {
  return <div>Hello World!</div>;
}

/foo/package.json:

{
  "private": true,
  "source": "src/index.tsx",
  "scripts": {
    "parcel:build": "npx parcel build"
  }
}

如何修复错误 @parcel/core: 无法解析 'solid- js/jsx-runtime 在我的嵌套 Typescript 项目中?

I'm migrating a React Webpack project into a SolidJS Parcel project. However, the combination of SolidJS and Parcel seems to have problems with nested Typescript projects, which combinations of React and Webpack do not have.

In my minimal example project, I get the error: @parcel/core: Failed to resolve 'solid-js/jsx-runtime' from './foo/src/index.tsx'. If I try to rename, index.tsx into index.jsx, Parcel can build the same file without any error. However, I want to use Typescript instead of JavaScript.

/.babelrc:

{
  "presets": [ "solid" ]
}

/package.json:

{
  "private": true,
  "dependencies": {
    "solid-js": "1.3.12"
  },
  "devDependencies": {
    "babel-preset-solid": "1.3.12",
    "parcel": "2.3.2",
    "typescript": "4.6.2"
  }
}

/tsconfig.json:

{
    "compilerOptions": {
        "isolatedModules": true,
        "jsx": "preserve",
        "jsxImportSource": "solid-js",
        "lib": [
            "dom",
            "es2021"
        ],
        "module": "commonjs",
        "noEmit": true,
        "noImplicitAny": true,
        "noImplicitReturns": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "sourceMap": true,
        "strict": true,
        "target": "es5"
    }
}

/foo/src/index.tsx:

export function HelloWorld() {
  return <div>Hello World!</div>;
}

/foo/package.json:

{
  "private": true,
  "source": "src/index.tsx",
  "scripts": {
    "parcel:build": "npx parcel build"
  }
}

How can I fix the error @parcel/core: Failed to resolve 'solid-js/jsx-runtime in my nested Typescript project?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

檐上三寸雪 2025-01-21 01:30:14

要在parcel中运行solid,您需要按照以下步骤操作:

  1. 安装babel-preset-solid@babel/core

  2. 创建一个包含以下内容的 .babelrc 文件:

{
  "presets": ["solid"]
}

Parcel 自动选择 babel 配置,但最好坚持基于 json 的配置:

Parcel 支持项目范围的配置文件(例如 babel.config.json)以及文件相关配置(例如 .babelrc)。

注意:应避免使用 JavaScript Babel 配置(例如 babel.config.js)。这些会导致 Parcel 的缓存效率降低,这意味着每次重新启动 Parcel 时,所有 JS 文件都将被重新编译。

附带说明一下,我们不需要提供 babel-transform-runtime 来使 Parcel 工作:

@babel/preset-env 和 @babel/plugin-transform-runtime 不是必需的,因为浏览器目标的转译是由 Parcel 自动处理的。
https://parceljs.org/languages/javascript/#babel

  1. 添加热模块替换 ( HMR)支持(如果您愿意):
import { render } from 'solid-js/web';
import { App } from './App';
const dispose = render(() => <App />, document.body);

if (module.hot) {
  module.hot.accept();
  module.hot.dispose(dispose);
}

如果您遇到任何问题,请确保您已正确设置工作区:

package.json 文件

{
  "name": "parcel-solid-demo",
  "source": "src/index.html",
  "browserslist": "> 0.5%, last 2 versions, not dead",
  "scripts": {
    "start": "parcel",
    "build": "parcel build",
    "serve": "parcel serve"
  },
  "devDependencies": {
    "babel-preset-solid": "^1.6.10",
    "@babel/core": "^7.21.0",
    "parcel": "^2.8.3"
  },
  "dependencies": {
    "solid-js": "^1.6.11"
  }
}

包中“source”目录中的 index.html 文件.json:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8"/>
    <title>My First Parcel App</title>
    <link rel="stylesheet" href="style.css" />
    <script type="module" src="index.tsx"></script>
  </head>
  <body>
  </body>
</html>

tsconfig.json 内容:

{
  "compilerOptions": {
    "jsx": "preserve",
    "jsxImportSource": "solid-js",
    "noEmit": true
  }
}

To run solid in parcel, you need to follow these steps:

  1. Install babel-preset-solid and @babel/core.

  2. Create a .babelrc file with the following content:

{
  "presets": ["solid"]
}

Parcel automatically picks up babel configuration but it is best to stick to the json based configuration:

Parcel supports both project wide config files such as babel.config.json, as well as file relative configs such as .babelrc.

Note: JavaScript Babel configs (e.g. babel.config.js) should be avoided. These cause Parcel’s caching to be less effective, which means all of your JS files will be recompiled each time you restart Parcel.

As a side note, we don't need to provide babel-transform-runtime for parcel to work:

@babel/preset-env and @babel/plugin-transform-runtime are not necessary, since transpilation for your browser targets is handled automatically by Parcel.
https://parceljs.org/languages/javascript/#babel

  1. Add hot module replacement (HMR) support if you like:
import { render } from 'solid-js/web';
import { App } from './App';
const dispose = render(() => <App />, document.body);

if (module.hot) {
  module.hot.accept();
  module.hot.dispose(dispose);
}

If you run into any problem, make sure you have properly set your workspace:

package.json file

{
  "name": "parcel-solid-demo",
  "source": "src/index.html",
  "browserslist": "> 0.5%, last 2 versions, not dead",
  "scripts": {
    "start": "parcel",
    "build": "parcel build",
    "serve": "parcel serve"
  },
  "devDependencies": {
    "babel-preset-solid": "^1.6.10",
    "@babel/core": "^7.21.0",
    "parcel": "^2.8.3"
  },
  "dependencies": {
    "solid-js": "^1.6.11"
  }
}

index.html file in the "source" directory from the package.json:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8"/>
    <title>My First Parcel App</title>
    <link rel="stylesheet" href="style.css" />
    <script type="module" src="index.tsx"></script>
  </head>
  <body>
  </body>
</html>

tsconfig.json content:

{
  "compilerOptions": {
    "jsx": "preserve",
    "jsxImportSource": "solid-js",
    "noEmit": true
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文