Electron + Typescript + Webpack:Boilerplate示例

Mar*_*rev 6 boilerplate typescript webpack electron

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

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

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

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

提前致谢.

Tar*_*ani 21

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

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

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

src
|-- common
|-- main
|-- renderer
Run Code Online (Sandbox Code Playgroud)

主电子流程的代码将进入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')
    },
  })
]
Run Code Online (Sandbox Code Playgroud)

人们面临的另一个问题是,如果对此无能为力,那就__dirname成了问题/.所以我们在webpack配置中包含以下内容

  node: {
    __dirname: true
  },
Run Code Online (Sandbox Code Playgroud)

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

如何使用Electron运行和打包外部可执行文件?

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

两者之间的tsconfig.json需求是不同的main,renderer并且应该相互排斥.所以我们用

渲染/ 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"
    ]
}
Run Code Online (Sandbox Code Playgroud)

主/ 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"
    ]
}
Run Code Online (Sandbox Code Playgroud)

最后一件事是你的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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 谢谢举例,很有帮助。为主电子进程和渲染器保留两个不同的 webpack 配置和 typescript 配置有什么好处? (2认同)
  • @BillDeRose,这是因为渲染器 javascript 在浏览器中运行,而电子在 nodejs 中运行。他们有两个不同的代码包,这就是为什么有两个不同的配置。 (2认同)