electron 如何让TypeScript项目作为JavaScript项目中的子进程运行?

bq8i3lrv  于 2023-06-20  发布在  Electron
关注(0)|答案(1)|浏览(177)

我有一个电子应用程序,主要是所有的javascript,我也有一个express服务器项目,这是写在typescript,如果我试图使这个typescript项目的子进程在我的electron.js文件,我得到typescript错误,我的server.js不理解它试图导入的. ts文件类型(它们存在,它只是不知道如何处理它们)。TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
====电子应用程序===
下面是我在electron应用程序中的package.json:

"scripts": {
    "build": "react-app-rewired build",
    "start": "react-app-rewired start",
    "electron": "electron ."
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2020",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "noFallthroughCasesInSwitch": true
  },
  "include": ["src", "expressproject/src"]
}

下面是我在electron.js中运行express项目的方法:
//electron.js

const { spawn } = require("child_process");

// start express server
const expressServerProcess = spawn("node", [pathToServerJS]); // D:/expressproject/src/server.js

function createWindow() {
...
}

====快递项目====
下面是我的typescript express项目中的package.json

"scripts": {
    "build": "tsc -p tsconfig.json",
    "start": "npm run build && node --loader ts-node/esm --experimental-specifier-resolution=node server.js",
}

tsconfig.json

{
  "extends": "ts-node/node16/tsconfig.json",
  "ts-node": {
    "transpileOnly": true,
    "esm": true,
    "compilerOptions": {
      "outDir": "dist"
    }
  },
  "compilerOptions": {
    "outDir": "dist",
    "target": "ESNext",
    "module": "ES2020",
    "lib": ["ES2020", "ES2021"],
    "removeComments": true,
    "strict": true,
    "allowJs": true,
    "skipLibCheck": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "noEmit": true,
    "noFallthroughCasesInSwitch": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "sourceMap": true,
    "stripInternal": true,
    "declaration": true,
    "noImplicitAny": true,
    "allowUnusedLabels": false,
    "allowUnreachableCode": false,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictBindCallApply": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "useUnknownInCatchVariables": true,
    "alwaysStrict": true,
    "noImplicitReturns": true
  }
}

我认为正在发生的是TypeScript代码没有被编译成JavaScript?我该怎么做?

oprakyz7

oprakyz71#

在基思的帮助下回答:
基本上传递我在typescript项目上的npm start脚本中使用的所有值(除了构建脚本),作为我在electron应用程序中的child_process中运行的命令。
typescript express server package.json:
node --loader ts-node/esm --experimental-specifier-resolution=node server.js
electron.js

function startExpressServer() {
  const command = "node";
  const args = [
    "--loader",
    "ts-node/esm",
    "--experimental-specifier-resolution=node",
    pathToExpressServerJS, // expressproject/src/server.js
  ];

  const expressProcess = spawn(command, args, {
    shell: true,
    stdio: "ignore", 
  });

  expressProcess.on("close", (code) => {
    console.log(`Express server process exited with code ${code}`);
  });
}

startExpressServer()

function createWindow() {
...
}

相关问题