Electron + Typescript + Webpack:Boilerplate示例

问题描述 投票:3回答:1

我不知道如何开始这个问题,但主要的问题是我无法让3种技术协同工作:Electron + Typescript + Webpack

我遇到了很少的样板,但是在它们中,要么整个打字稿是用tsc(而不是Webpack)构建的,要么只有render-part是用Webpack构建的,而main-process(main.js)部分是用纯js编写的。 。

所以我想知道是否有人知道或者知道在哪里找到一个样板项目来启动新的Electron + Typescript + Webpack项目?

据我所知,它应该被配置为单独构建应用程序的主进程和渲染进程部分(可能,它们的配置可能不同)。

提前致谢。

typescript webpack electron boilerplate
1个回答
18
投票

我在下面的链接中添加了一个示例模板/样板文件

https://github.com/tarunlalwani/electron-webpack-typescript-boilerplate

所以想法是打破3个文件夹中的代码

src
|-- common
|-- main
|-- renderer

主电子过程的代码将进入main文件夹,UI /渲染器将进入renderer文件夹。

现在你想在两者中使用TypeScript并且有2个webpack配置,一个用于捆绑main,另一个用于捆绑renderer

const path = require('path');

console.log(__dirname);
let common_config = {
  node: {
    __dirname: true
  },
  mode: process.env.ENV || 'development',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: [
          /node_modules/,
           path.resolve(__dirname, "src/ui")
        ]
      }
    ]
  },
  resolve: {
    extensions: [ '.tsx', '.ts', '.js' ]
  },
};

module.exports = [
  Object.assign({}, common_config, {
    target: 'electron-main',
    entry: {
      renderrer: './src/main/index.ts',
    },
    output: {
      filename: '[name]-bundle.js',
      path: path.resolve(__dirname, 'src/main/dist')
    },
  }),
  Object.assign({}, common_config, {
    target: 'electron-renderer',
    entry: {
      ui: './src/renderer/index.ts',
    },
    output: {
      filename: '[name]-bundle.js',
      path: path.resolve(__dirname, 'src/renderer/dist')
    },
  })
]

人们面临的另一个问题是__dirname如果对此无所作为就会变成/。所以我们在webpack配置中包含以下内容

  node: {
    __dirname: true
  },

这可确保相对路径可用。现在我们可以在开发环境中使用os.cwd()并在生产中使用process.resourcePath。有关详细信息,请参阅以下主题

How to run and pack external executable using Electron?

webpack配置的目标需要不同。所以对于main我们使用electron-main和渲染器我们使用electron-renderer

对于tsconfig.jsonmainrenderer需要有所不同,并且应该互相排斥。所以我们用

渲染/ tsconfig.json

{
    "compileOnSave": false,
    "compilerOptions": {
        "target": "es2015",
        "moduleResolution": "node",
        "pretty": true,
        "newLine": "LF",
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "sourceMap": true,
        "skipLibCheck": true,
        "allowJs": true,
        "jsx": "preserve"
    },
    "exclude": [
      "node_modules",
      "src/main"
    ]
}

主/ tsconfig.json

{
    "compileOnSave": false,
    "compilerOptions": {
        "target": "es2015",
        "moduleResolution": "node",
        "pretty": true,
        "newLine": "LF",
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "sourceMap": true,
        "skipLibCheck": true,
        "allowJs": true,
        "jsx": "preserve"
    },
    "exclude": [
      "node_modules",
      "src/renderer"
    ]
}

最后一件事是你的package.json,它位于下方

{
  "name": "boilerplate",
  "version": "1.0.0",
  "main": "src/main/dist/renderrer-bundle.js",
  "license": "MIT",
  "scripts": {
    "start": "npm-run-all build run-electron",
    "build": "webpack --config webpack.config.js",
    "run-electron": "electron ."
  },
  "dependencies": {
    "electron": "^1.8.2",
    "jquery": "^3.3.1",
    "typescript": "^2.7.2",
    "webpack": "^4.0.1"
  },
  "devDependencies": {
    "@types/electron": "^1.6.10",
    "@types/jquery": "^3.3.0",
    "@types/node": "^9.4.6",
    "html-webpack-plugin": "^2.30.1",
    "npm-run-all": "^4.1.2",
    "ts-loader": "^4.0.0",
    "tslint": "^5.9.1",
    "webpack-cli": "^2.0.9"
  }
}

这应该是你的开始,然后你可以添加东西链接tslintjslint随着你去

© www.soinside.com 2019 - 2024. All rights reserved.