简体   繁体   English

如果使用 babel 进行编译,是否需要 typescript 项目参考? (使用 Webpack 的电子项目)

[英]Are typescript project references necessary if transpiling with babel? (Electron project with Webpack)

I am still in the process of setting up my project configuration so I don't have any errors to work with right now, but if I am understanding the Typescript docs correctly...我仍在设置我的项目配置,所以我现在没有任何错误需要处理,但如果我正确理解 Typescript 文档...

It seems like Project references TypeScript Docs - Project references are not that necessary if transpiling with babel-loader in webpack.似乎项目引用TypeScript 文档 - 如果在 webpack 中使用 babel-loader 进行编译,则项目引用不是必需的。 (I'm working in VSCode) (我在 VSCode 工作)

I am trying to convert an Electron app to TypeScript and currently reorganizing the folder structure so I have minimal issues.我正在尝试将 Electron 应用程序转换为 TypeScript 并且目前正在重新组织文件夹结构,因此我遇到的问题很少。 I am trying to understand if I am on the right track and if I can avoid including "references" and instead just use "extends" to get the functionality I want.我试图了解我是否走在正确的轨道上,以及是否可以避免包含“引用”,而只是使用“扩展”来获得我想要的功能。

Here is my project structure ignoring all files that are not tsconfig files:这是我的项目结构,忽略了所有不是 tsconfig 文件的文件:

./tsconfig.json
./tsconfig-base.json
./main/tsconfig.json
./src/client/tsconfig.json
./__tests__
./__tests__/__client__/tsconfig.json
./__tests__/__main__/tsconfig.json

In this structure ./tsconfig.json would really just be for references like the example on Microsoft's Github在这个结构./tsconfig.json真的只是为了参考微软的 Github 上的例子

Electron Main Process and related files are in ./main . Electron 主进程和相关文件在./main The tsconfig here will set "module":"commonjs" for working in node.这里的 tsconfig 将设置"module":"commonjs"以便在节点中工作。 I think it will also extend from the ./tsconfig-base.json我认为它也将从./tsconfig-base.json扩展

Electron Renderer Process and my React-Redux app files are in ./src/client . Electron 渲染器进程和我的 React-Redux 应用程序文件位于./src/client中。 The tsconfig here sets "module":"es2015" or "module":"ESNEXT" for working with es modules.此处的 tsconfig 设置"module":"es2015""module":"ESNEXT"用于使用 es 模块。 I think it will also extend from the ./tsconfig-base.json我认为它也将从./tsconfig-base.json扩展

The ./__tests__/__client__/tsconfig.json and ./__tests__/__main__/tsconfig.json would just be duplicates of the non tests folder versions similarly extending from the base config in ./ ./__tests__/__client__/tsconfig.json./__tests__/__main__/tsconfig.json只是非测试文件夹版本的副本,类似地从./中的基本配置扩展而来

Webpack config is already set up to handle creating separate bundles for main and renderer processes so that the entire app can be in TypeScript. Webpack 配置已设置为处理为主进程和渲染器进程创建单独的包,以便整个应用程序可以在 TypeScript 中。 Is there any reason I should be using "references" in my files in the main or client folders?有什么理由我应该在主文件夹或客户端文件夹中的文件中使用“引用”?

Sample snippets of the Webpack config before I switch the tnry files to be.ts files (dev):在我将 tnry 文件切换为 be.ts 文件(dev)之前,Webpack 配置的示例片段:

const rendererInclude = path.resolve(__dirname, "src");
const mainInclude = path.resolve(__dirname, "main");

Main Process:主要流程:

module.exports = [
  {
    mode: "development",
    entry: path.join(__dirname, "main", "swell.js"),
    output: {
      path: path.join(__dirname, "dist"),
      filename: "main-bundle.js",
    },
    target: "electron-main",
    node: {
      __dirname: false,
      __filename: false,
    },
    resolve: {
      extensions: [".ts", ".tsx", ".js", ".json"],
    },
    module: {
      rules: [
        {
          test: /\.(ts|js)x?$/,
          loader: "babel-loader",
          include: mainInclude,
          exclude: /node_modules/,
        }
    ] } ... },

continued to Renderer Process:继续渲染进程:

{
    mode: "development",
    entry: path.join(__dirname, "src", "index.js"),
    output: {
      path: path.join(__dirname, "dist"),
      filename: "renderer-bundle.js",
    },
    target: "electron-renderer",
    resolve: {
      extensions: [".ts", ".tsx", ".js", ".json"],
    },
    module: {
      rules: [
        {
          test: /\.(ts|js)x?$/,
          loader: "babel-loader",
          include: rendererInclude,
          exclude: /node_modules/,
        ]} ...} ]

Project Reference will help you solve some problems in case where your test projects import src/main modules.如果您的测试项目导入 src/main 模块,项目参考将帮助您解决一些问题。 This is what Project Reference solves:这是项目参考解决的问题:

  • There's no built-in up-to-date checking, so you end up always running tsc twice没有内置的最新检查,所以你最终总是运行 tsc 两次
  • Invoking tsc twice incurs more startup time overhead调用 tsc 两次会导致更多的启动时间开销
  • tsc -w can't run on multiple config files at once tsc -w 不能同时在多个配置文件上运行

read more about Project References阅读有关项目参考的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM