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...
}