Misconception Webpack
One thing that understands in advance is that webpack does not bundle the files needed through fs or other modules that request a file path. These types of assets are usually referred to as Static assets , as they are not related in any way. webpack will only link require d or import ed (ES6) files. Also, depending on your webpack configuration, your project root may not always match what is displayed in your production builds.
Based on the documentation on electronic vue Project structure / File tree , you will find that only webpack packages and the static/ directory are available in production assemblies. electron-vue also has a convenient global __static variable, which can provide a path to this static/ folder during both development and production. You can use this variable, similar to how __dirname and path.join access your JSON files or indeed any files.
Static Asset Solution
It seems that the current version of electron-vue template has already resolved this for you, but I will tell you how it is with webpack , since it can be applied not only to JSON files, but also how it can be applied to any installation of webpack + electron . The following solution assumes that you are creating webpack outputs for a separate folder, in this case we will use dist/ , suppose your webpack configuration is in the project root directory and assumes that process.env.NODE_ENV set to development during development.
static/ directory static/
During development, we need a place to store our static assets, so put them in a directory called static/ . Here we can put files, such as JSON, that we know that we will need to read using fs or another module, which requires the full path to the file.
Now we need to make this static/ resource directory available in production assemblies.
But webpack does not handle this folder at all, what can we do?
Use a simple copy-webpack-plugin . In our webpack configuration file webpack we can add this plugin when creating for production and configure it to copy the static/ folder to our dist/ folder.
new CopyWebpackPlugin([ { from: path.join(__dirname, '/static'), to: path.join(__dirname, '/dist/static'), ignore: ['.*'] } ])
Well, so the assets are in production, but how do I get the path to this folder in both development and production?
Creating the __static global variable
What is the point of making this __static variable?
Using __dirname not reliable in webpack + electron settings. At design time, __dirname may refer to the directory that exists in your src/ files. During production, since webpack our src/ files into one script, this path that you created for static/ no longer exists. Also, those files that you put inside src/ that were not require d or import ed never get into your production assembly.
When handling differences in project structure from development to production, trying to get a static/ path will be very annoying during development to always check your process.env.NODE_ENV .
So simplify this by creating one source of truth.
Using webpack.DefinePlugin , we can only set our __static variable in development to get a path pointing to <projectRoot>/static/ . If you have multiple webpack configurations, you can use this to configure the main and renderer process.
new webpack.DefinePlugin({ '__static': `"${path.join(__dirname, '/static').replace(/\\/g, '\\\\')}"` })
In the production process, we need to set the __static variable manually in our code. Here is what we can do ...
index.html ( renderer process)
<script> if (process.env.NODE_ENV !== 'development') window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\') </script>
main.js ( main process)
// Set `__static` path to static files in production if (process.env.NODE_ENV !== 'development') { global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\') } // rest of application code below
Now start using the __static variable
Let's say we have a simple JSON file that we must read with fs , here that we can execute now ...
static/someFile.json
{"foo":"bar"}
someScript.js ( renderer or main process)
import fs from 'fs' import path from 'path' const someFile = fs.readFileSync(path.join(__static, '/someFile.json'), 'utf8') console.log(JSON.parse(someFile))
Conclusion
webpack was made to webpack assets together that require d or import ed into one good package. Assets referenced by fs or other modules that need a file path are considered Static assets , and webpack does not directly process them. Using copy-webpack-plugin and webpack.DefinePlugin , we can configure the robust __static variable, which gives the path to our static/ resource directory in both development and production.
To finish, I personally have not seen any other webpack + electron templates handle this situation, since this is not a very common situation, but I think we can all agree that one source of truth is the Static Assets Directory is a great approach to mitigating developer fatigue.