javascript - Webpack 和 Typescript(ts-loader) 上的设置问题
问题描述
尝试使用 Webpack 和 ts-loader 设置 SolidJS。
不断遇到有关return
功能的问题。
我在加载程序中究竟缺少什么来解决问题?
import { render } from "solid-js/web";
const App = () => {
// return "Hello World" // this works!
return (<div> Hello World </div>) // this doesnt work!
};
render(App, document.getElementById("app"));
错误日志输出:
> webpack --watch --progress --config webpack.config.js
asset index.js 1.56 KiB [emitted] (name: main)
./src/test.tsx 290 bytes [built] [code generated]
ERROR in ./src/test.tsx 6:12
Module parse failed: Unexpected token (6:12)
File was processed with these loaders:
* ./node_modules/ts-loader/index.js
You may need an additional loader to handle the result of these loaders.
| const App = () => {
| // return "Hello World" // this works!
> return (<div> Hello World </div>); // this doesnt work!
| };
| render(App, document.getElementById("app"));
webpack 5.47.0 compiled with 1 error in 2081 ms
webpack.config
const path = require('path');
module.exports = {
mode: 'development',
entry: './src/test.tsx',
module: {
rules: [
{
use: 'ts-loader',
test: /\.(ts|tsx)?$/,
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist')
}
}
更新:
# tsconfig.json
{
"compilerOptions": {
"jsx": "preserve",
"jsxImportSource": "solid-js",
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"esModuleInterop": false
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": ["node_modules"]
}
解决方案
您需要添加 Babel。从 TypeScript 中保留 JSX 很好,但在最终捆绑之前仍然需要对其进行转换。babel-preset-solid 负责这个。否则 Webpack 不知道如何处理该 JSX。
一般来说,我只是将 Babel 与 TypeScript 预设一起使用。我知道这不会给你实时类型检查,只是剥离类型。Create React App 使用 fork-ts-checker-webpack-plugin 来解决该问题,并与 Babel 转换并行处理类型检查。
推荐阅读
- camunda - Camunda“接收任务”打开“异步之后”会导致“无法关联消息”错误
- django - django ModelAdmin autocomplete_fields 中的 ID 保存在哪里?
- git - 如何恢复从一个分支合并的所有提交
- python - Python中的更多阈值算法?
- c# - 在 ItemsSourceUpdated 之后关注 DataGridRow
- java - 如何理解“应用程序的状态应存储在文件中”?
- firebase - Firebase 实时数据库 - 如何使用 REST API 管理数据库规则
- r - R中混合数据的DBSCAN
- javascript - 如何从子组件调用 React Hook 以刷新结果
- asp.net-core - 为多租户运行多个数据库的 ABP IO 代码示例