[英]Setup issue on Webpack and 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
配置文件
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。
一般來說,我只使用帶有 TypeScript 預設的 Babel。 我知道這不會為您提供實時類型檢查,而只是去除類型。 Create React App 使用 fork-ts-checker-webpack-plugin 來解決該問題,並與 Babel 轉換並行處理類型檢查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.