Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webpack 带网页包的条件变量名称_Webpack_Webpack 2 - Fatal编程技术网

Webpack 带网页包的条件变量名称

Webpack 带网页包的条件变量名称,webpack,webpack-2,Webpack,Webpack 2,我正在构建一个应用程序,每个人都可以使用源代码。根据终端系统将使用什么(可以使用electron和在C#WebBrowserControl下运行),需要应用不同的全局变量名 比如说 <--! This should not be applied to the end product if I don't specify that --> <script> const electron = require('electron'); const current

我正在构建一个应用程序,每个人都可以使用源代码。根据终端系统将使用什么(可以使用
electron
和在C#
WebBrowserControl
下运行),需要应用不同的全局变量名

比如说

<--! This should not be applied to the end product if I don't specify that -->
<script>
    const electron = require('electron');
    const currentWindow = electron.remote.getCurrentWindow();
</script>
<!-- This is the end of the conditional statement -->

有一个名为
DefinePlugin
的网页插件,可以满足您的需要:

然后,您可以从应用程序代码中作为全局变量访问
HOST

if (HOST === 'electron') {
    const electron = require('electron');
    const currentWindow = electron.remote.getCurrentWindow();
} else if (HOST === 'WebBrowserControl') {
    // etc...
}
根据您启动Web包构建的方式,您应该能够根据正在进行的构建类型,在
Webpack.config.js
中交换
HOST
的值

if (HOST === 'electron') {
    const electron = require('electron');
    const currentWindow = electron.remote.getCurrentWindow();
} else if (HOST === 'WebBrowserControl') {
    // etc...
}