Skip to content

Commit 346701f

Browse files
committed
feat(build/serve): add process.env.IS_ELECTRON, fixes #181
1 parent 28b5be0 commit 346701f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

__tests__/webpackConfig.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ describe('chainWebpack', () => {
9191
expect(config.toConfig()).toEqual(new Config().toConfig())
9292
}
9393
)
94+
95+
test.each([true, false])(
96+
'process.env.IS_ELECTRON is set properly',
97+
async isElectron => {
98+
// Set build mode (Electron or other)
99+
if (isElectron) {
100+
process.env.IS_ELECTRON = true
101+
} else {
102+
delete process.env.IS_ELECTRON
103+
}
104+
const config = mockChain()
105+
// Definition is set accordingly
106+
expect(
107+
config.plugin('define').toConfig().definitions['process.env']
108+
.IS_ELECTRON
109+
)[`toBe${isElectron ? 'Truthy' : 'Falsy'}`]()
110+
}
111+
)
94112
})
95113

96114
describe.each(['production', 'development'])('getExternals in %s', env => {

lib/webpackConfig.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ async function chainWebpack (api, pluginOptions, config) {
1212
.target('electron-renderer')
1313
.node.set('__dirname', false)
1414
.set('__filename', false)
15+
// Set IS_ELECTRON
16+
config.plugin('define').tap(args => {
17+
args[0]['process.env'].IS_ELECTRON = true
18+
return args
19+
})
1520
if (process.env.NODE_ENV === 'production') {
1621
// Set process.env.BASE_URL and __static to absolute file path
1722
config.plugin('define').tap(args => {

0 commit comments

Comments
 (0)