首页 文章

Electron Typescript Webpack:Boilerplate示例

提问于
浏览
2

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

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

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

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

提前致谢 .

1 回答

  • 14

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

    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

    mainrenderertsconfig.json 需要不同,并且应该相互排除 . 所以我们用

    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"
        ]
    }
    

    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 随着你去

相关问题