【发布时间】:2018-09-26 19:34:28
【问题描述】:
我有一个用 TypeScript 编写的基于 Electron 的应用程序。为了捆绑我在我的 gulpfile 中运行 webpack 的代码,它当然可以针对 Electron 或浏览器。各自的配置如下:
const appCompile = gulp.src(`${sourceFolder}/Typescript/Main.ts`)
.pipe(webpackStream({
module: {
rules: [
{
loaders: ["babel-loader", "ts-loader"],
exclude: [/node_modules/]
},
]
},
resolve: {
modules: ["Source/Typescript", "node_modules"],
extensions: [".tsx", ".ts", ".js"]
},
output: {
filename: "Bundle.js"
},
mode: buildConfiguration.isDevelopment ? "development" : "production",
externals: externals,
target: targetPlatform.isWeb ? "web" : "electron-renderer",
devtool: buildConfiguration.isDevelopment ? "source-map" : "none"
}, webpack))
.pipe(gulp.dest(paths.scripts.dest));
目前我有一些代码行只打算在开发模式下执行,在 Electrons 渲染器(不是主!)进程中本地运行(因为它包含一些低级 fs 代码)。在运行 webpack 为 web 部署时,有什么方法可以防止发出这些行/调用?像 if (isElectron) { doSomethingLocally(); } 这样的语句的全局常量。
编辑:特别是像 import * as fs from "fs"; 这样的导入错误,当为 web 而不是 Electron 打包时,正如预期的那样。即使我可以使用像 const isElectron = navigator.userAgent.indexOf("Electron") !== -1; 这样的助手,这也不会帮助我“有条件地导入”。
【问题讨论】:
标签: typescript webpack gulp electron