Skip to content

Commit

Permalink
導入TypeScript (#110)
Browse files Browse the repository at this point in the history
Co-authored-by: Enya <[email protected]>
  • Loading branch information
Irisloverain and Enya authored Sep 7, 2023
1 parent 9fb653d commit 1370138
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/frontend/js/components/App.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import getAPI from '../api/getAPI'
import Hello from './Hello'

const App = () => {
const { data } = getAPI()
return (
<>
<Hello></Hello>
<div>title:{data.msg}</div>
</>
)
Expand Down
17 changes: 17 additions & 0 deletions packages/frontend/js/components/Hello.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// src/components/Hello.tsx

import React from 'react'

interface Props {
name: string
}

const Hello: React.FC<Props> = ({ name }) => {
return (
<div>
<h1>Hello, {name}!</h1>
</div>
)
}

export default Hello
85 changes: 79 additions & 6 deletions packages/frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@types/prop-types": "^15.7.5",
"@types/react": "^18.2.21",
"@types/react-dom": "^18.2.7",
"clean-webpack-plugin": "^4.0.0",
"css-loader": "^6.8.1",
"cypress": "^12.14.0",
Expand Down Expand Up @@ -40,6 +43,8 @@
"sass": "^1.63.4",
"sass-loader": "^13.3.2",
"style-loader": "^3.3.3",
"ts-loader": "^9.4.4",
"typescript": "^5.2.2",
"webpack": "^5.87.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1"
Expand Down
19 changes: 19 additions & 0 deletions packages/frontend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "es2015", // 目標編譯到的ECMAScript版本(這裡是ES6)
"lib": ["dom", "dom.iterable", "esnext"], // 要包含的JavaScript標準庫
"jsx": "react", // 設定React JSX語法支援
"module": "esnext", // ES模組
"moduleResolution": "node", // 模組解析策略,使用Node.js解析
"strict": true, // 啟用嚴格模式,提供更多的類型檢查
"esModuleInterop": true, // 啟用導入/導出Interop支援
"skipLibCheck": true, // 跳過檢查宣告文件的型別檢查
"forceConsistentCasingInFileNames": true, // 強制文件名大小寫一致
"noEmit": false, // 不生成任何輸出文件,用於純類型檢查
"strictNullChecks": true, // 啟用嚴格的空值檢查
"noImplicitAny": true, // 不允許隱式的any型別
"allowJs": true // 允許編譯JavaScript文件(用於混合專案)
},
"include": ["js/**/*.ts", "js/**/*.tsx", "js/*.ts"], // TypeScript編譯包括的文件
"exclude": ["node_modules"] // TypeScript編譯排除的文件夾
}
7 changes: 6 additions & 1 deletion packages/frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ module.exports = function () {
},
module: {
rules: [
{
test: /\.(js|jsx|ts|tsx)$/,
loader: 'ts-loader',
exclude: /node_modules/
},
{
test: /\.(js|jsx)$/,
loader: 'esbuild-loader',
Expand Down Expand Up @@ -57,7 +62,7 @@ module.exports = function () {
]
},
resolve: {
extensions: ['.js', '.jsx']
extensions: ['.js', '.jsx', '.ts', '.tsx']
},
plugins: [
// 提取CSS文件, 和style-loader無法共存
Expand Down

0 comments on commit 1370138

Please sign in to comment.