我正在使用 node、react、electron 和 typescript 创建一个项目
我使用以下教程启动项目:https://flaviocopes.com/react-electron/#add-electron
更改了一些设置 TS 的内容:
npx create-react-app files --template typescript
npm install --save-dev ts-node
"electron-start": "ts-node src/start-react.ts"
问题来了
"type": "module"
才能导入/导出工作Unknown file extension ".ts"
"type": "module"
使 TS 正常工作"module" : "CommonJS"
"module" : "esnext"
那么我该怎么做才能让 node 运行我的 typescript start-react.ts
文件
./package.json
{
"name": "files",
"version": "0.1.0",
"private": true,
"homepage": "./",
"main": "src/start.ts",
"dependencies": {
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.3",
"@testing-library/user-event": "^12.6.0",
"@types/jest": "^26.0.20",
"@types/node": "^12.19.14",
"@types/react": "^16.14.2",
"@types/react-dom": "^16.9.10",
"electron": "^11.2.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.1",
"typescript": "^4.1.3",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "nf start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"electron": "electron .",
"electron-start": "ts-node src/start-react.ts",
"react-start": "react-scripts start",
"pack": "build --dir",
"dist": "npm run build && build",
"postinstall": "install-app-deps"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"electron-builder": "^22.9.1",
"ts-node": "^9.1.1"
},
"build": {
"appId": "com.electron.electron-with-create-react-app",
"win": {
"iconUrl": "https://cdn2.iconfinder.com/data/icons/designer-skills/128/react-256.png"
},
"directories": {
"buildResources": "public"
}
}
}
./tsconfig.json
{
"compilerOptions": {
"target": "ESNext",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}
./src/start-react.ts
import * as net from "net"
import * as childProcess from "child_process"
const port:any = process.env.PORT
process.env.ELECTRON_START_URL = `http://localhost:${port}`
const client = new net.Socket()
let startedElectron = false
const tryConnection = () =>
{
client.connect({ port }, () =>
{
client.end()
if (startedElectron) return
startedElectron = true
childProcess.exec("npm run electron")
})
}
tryConnection()
client.on("error", () => setTimeout(tryConnection, 1000))
./src/start.ts
import { app, BrowserWindow } from "electron"
let mainWindow: BrowserWindow | null
function createWindow()
{
mainWindow = new BrowserWindow(
{
width: 800,
height: 800,
webPreferences:
{
nodeIntegration: true
}
})
mainWindow.loadURL(<string>process.env.ELECTRON_START_URL)
mainWindow.on("closed", () => mainWindow = null)
}
app.on("ready", createWindow)
app.on("window-all-closed", () => process.platform !== "darwin" ? app.quit : null)
app.on("activate", () => !mainWindow ? createWindow() : null)
附言如果您需要更多信息,请告诉我
答案 0 :(得分:0)
尝试将 "esModuleInterop": true,
添加到 tsconfig.json
答案 1 :(得分:-1)
我建议您在 Electron JS 的 React Boilerplate 中设置带有 official documentation 的应用程序。样板提供TypeScript 支持,并且在16.7 万 颗星上表现强劲。
此类样板可帮助您节省手动设置的时间和精力,并且高度可靠,因为它们采用了最佳做法。