Skip to content

Commit 20a5e98

Browse files
committed
improve static assets handling (__static)
1 parent cadec5a commit 20a5e98

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

index.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,20 @@ module.exports = (api, options) => {
3737
// Import the yargs options from electron-builder
3838
const configureBuildCommand = require('electron-builder/out/builder')
3939
.configureBuildCommand
40-
// Parse the raw arguments using electron-builder yargs config
40+
// Parse the raw arguments using electron-builder yargs config
4141
const builderArgs = yargs
4242
.command(['build', '*'], 'Build', configureBuildCommand)
4343
.parse(rawArgs)
4444
const rendererConfig = api.resolveChainableWebpackConfig()
45+
// Configure base webpack config to work properly with electron
46+
rendererConfig.target('electron-renderer').output.publicPath('./')
47+
rendererConfig.node.set('__dirname', false).set('__filename', false)
48+
// Set process.env.BASE_URL and __static to absolute file path
49+
rendererConfig.plugin('define').tap(args => {
50+
args[0]['process.env'].BASE_URL = '__dirname'
51+
args[0].__static = '__dirname'
52+
return args
53+
})
4554
// Base config used in electron-builder build
4655
const defaultBuildConfig = {
4756
directories: {
@@ -74,6 +83,10 @@ module.exports = (api, options) => {
7483
mainConfig.output
7584
.path(api.resolve(outputDir + '/bundled'))
7685
.filename('background.js')
86+
// Set __static to __dirname (files in public get copied here)
87+
mainConfig
88+
.plugin('define')
89+
.use(webpack.DefinePlugin, [{ __static: '__dirname' }])
7790
mainConfig.plugin('uglify').use(UglifyJSPlugin, [
7891
{
7992
parallel: true
@@ -94,9 +107,6 @@ module.exports = (api, options) => {
94107
}
95108

96109
console.log('Bundling render process:')
97-
// Configure base webpack config to work properly with electron
98-
rendererConfig.target('electron-renderer').output.publicPath('./')
99-
rendererConfig.node.set('__dirname', false).set('__filename', false)
100110
// Build the render process with the custom args and config
101111
await buildRenderer(vueArgs, api, options, rendererConfig)
102112
// Copy fonts to css/fonts. Fixes some issues with static font imports
@@ -176,6 +186,11 @@ module.exports = (api, options) => {
176186
.target('electron-renderer')
177187
.node.set('__dirname', false)
178188
.set('__filename', false)
189+
// Set __static to absolute path to public folder
190+
rendererConfig.plugin('define').tap(args => {
191+
args[0].__static = JSON.stringify(api.resolve('./public'))
192+
return args
193+
})
179194
// Configure webpack for main process
180195
const mainConfig = new Config()
181196
mainConfig
@@ -189,6 +204,12 @@ module.exports = (api, options) => {
189204
parallel: true
190205
}
191206
])
207+
// Set __static to absolute path to public folder
208+
mainConfig.plugin('define').use(webpack.DefinePlugin, [
209+
{
210+
__static: JSON.stringify(api.resolve('./public'))
211+
}
212+
])
192213
mainConfig
193214
.plugin('env')
194215
.use(webpack.EnvironmentPlugin, [{ NODE_ENV: 'development' }])

0 commit comments

Comments
 (0)