Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

WIP: #26 Move to electron-builder #236

Closed
wants to merge 13 commits into from
Closed
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# production
/build
/release-builds
/dist

# misc
.DS_Store
Expand Down
1 change: 1 addition & 0 deletions config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = {
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
appIndexJs: resolveApp('src/index.js'),
electronJs: resolveApp('src/electron.js'),
appPackageJson: resolveApp('package.json'),
appSrc: resolveApp('src'),
testsSetup: resolveApp('src/setupTests.js'),
Expand Down
20 changes: 14 additions & 6 deletions config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const cssFilename = 'static/css/[name].[contenthash:8].css';
// To have this structure working with relative paths, we have to use custom options.
const extractTextPluginOptions = shouldUseRelativeAssetPaths
? // Making sure that the publicPath goes back to to build folder.
{ publicPath: Array(cssFilename.split('/').length).join('../') }
{ publicPath: Array(cssFilename.split('/').length).join('../') }
: {};

// Options for PostCSS as we reference these options twice
Expand Down Expand Up @@ -118,16 +118,20 @@ module.exports = {
// We generate sourcemaps in production. This is slow but gives good results.
// You can exclude the *.map files from the build during deployment.
devtool: shouldUseSourceMap ? 'source-map' : false,
// In production, we only want to load the polyfills and the app code.
entry: [require.resolve('./polyfills'), paths.appIndexJs],
// In production, we only want to load the polyfills, the app code, and the
// electron builder entry script.
entry: {
main: [require.resolve('./polyfills'), paths.appIndexJs],
electron: paths.electronJs
},
output: {
// The build folder.
path: paths.appBuild,
// Generated JS file names (with nested folders).
// There will be one main bundle, and one file per asynchronous chunk.
// We don't currently advertise code splitting but Webpack supports it.
filename: 'static/js/[name].[chunkhash:8].js',
chunkFilename: 'static/js/[name].[chunkhash:8].chunk.js',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the hashes are added so that Chrome knows when to invalidate its cache of the JS bundles, right?

Right now that's fine since this file never changes, but presumably once we add the ability to update, we'll need to know when the file's changed.

What was the reason for this change? I suspect I'm misunderstanding something haha.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I hadn't considered whether the updater uses the hash to update changed files as well. I made this change under assumption that it wasn't needed for electron, but if it is needed then that makes this difficult.

I did this because the package.json has to reference the entry script (electron.js). Since the file is generated by webpack, it breaks the static reference if there's a dynamic chunk name as part of the file path.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh gotcha. Ok, let's leave it as-is for now. If that does become an issue, it's one we can address later on (really we should disable Chrome's browser cache altogether... and it might already be)

filename: '[name].js',
chunkFilename: '[name].chunk.js',
// We inferred the "public path" (such as / or /my-project) from homepage.
publicPath: publicPath,
// Point sourcemap entries to original disk location (format as URL on Windows)
Expand Down Expand Up @@ -370,6 +374,7 @@ module.exports = {
new HtmlWebpackPlugin({
inject: true,
template: paths.appHtml,
excludeChunks: ['electron'],
minify: {
removeComments: true,
collapseWhitespace: true,
Expand Down Expand Up @@ -477,11 +482,14 @@ module.exports = {
// such as fs, path, electron are ignored by webpack
target: 'electron-renderer',
externals: [
function(context, request, callback) {
function (context, request, callback) {
if (externals.indexOf(request) !== -1) {
return callback(null, 'commonjs ' + request);
}
callback();
},
],
node: {
__dirname: false
}
};
23 changes: 14 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
"productName": "Guppy",
"version": "0.2.0",
"private": true,
"main": "src/main.js",
"main": "build/electron.js",
"homepage": "./",
"build": {
"appId": "com.electron.guppy",
"files": [
"build/**/*"
],
"asarUnpack": [
"node_modules/yarn/"
]
},
"repository": {
"type": "git",
"url": "https://github.com/joshwcomeau/guppy.git"
Expand All @@ -14,12 +23,8 @@
"start": "cross-env NODE_ENV=development npm run start:react",
"start:react": "cross-env BROWSER=none node scripts/start.js",
"build": "node scripts/build.js",
"package:linux": "electron-packager . --platform=linux --prune=true --out=release-builds --overwrite",
"package:mac": "electron-packager . --platform=darwin --arch=x64 --icon=src/assets/icons/mac/logo.icns --prune=true --out=release-builds --overwrite",
"package:win": "electron-packager . --platform=win32 --arch=x64 --icon=src/assets/icons/win/logo.ico --prune=true --out=release-builds --overwrite",
"dist:linux": "cross-env GENERATE_SOURCEMAP=false npm run build && npm run package:linux",
"dist:mac": "cross-env GENERATE_SOURCEMAP=false npm run build && npm run package:mac",
"dist:win": "cross-env GENERATE_SOURCEMAP=false npm run build && npm run package:win",
"dist": "cross-env GENERATE_SOURCEMAP=false npm run build && electron-builder",
"dist:all": "cross-env GENERATE_SOURCEMAP=false npm run build && electron-builder -mwl",
"test": "node scripts/test.js --env=node",
"coverage": "yarn test --coverage",
"report-coverage": "yarn coverage && codecov",
Expand Down Expand Up @@ -73,7 +78,7 @@
"dotenv": "5.0.0",
"dotenv-expand": "4.2.0",
"electron": "2.0.1",
"electron-packager": "12.1.0",
"electron-builder": "20.28.4",
"eslint": "4.15.0",
"eslint-config-react-app": "3.0.0-next.66cc7a90",
"eslint-loader": "1.9.0",
Expand Down Expand Up @@ -204,4 +209,4 @@
"icon": null,
"createdAt": 1529502079329
}
}
}
Binary file added public/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
</div>
</body>

</html>
</html>
11 changes: 6 additions & 5 deletions src/main.js → src/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const chalkRaw = require('chalk');

const killProcessId = require('./services/kill-process-id.service');
const electronStore = require('./services/electron-store.service');
const icon256 = require('./assets/icons/png/256x256.png');

const chalk = new chalkRaw.constructor({ level: 3 });

Expand Down Expand Up @@ -43,7 +44,7 @@ function createWindow() {
height: 768,
minWidth: 777,
titleBarStyle: 'hidden',
icon: path.join(__dirname, 'assets/icons/png/256x256.png'),
icon: path.join(__dirname, icon256)
});

// set up some chrome extensions
Expand Down Expand Up @@ -82,7 +83,7 @@ function createWindow() {
mainWindow.loadURL(startUrl);

// Emitted when the window is closed.
mainWindow.on('closed', function() {
mainWindow.on('closed', function () {
Jtfinlay marked this conversation as resolved.
Show resolved Hide resolved
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
Expand All @@ -96,7 +97,7 @@ function createWindow() {
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function() {
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
Expand All @@ -112,7 +113,7 @@ app.on('before-quit', ev => {
}
});

app.on('activate', function() {
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
Expand Down Expand Up @@ -184,7 +185,7 @@ const manageApplicationLocation = () => {
message: 'Move to Applications folder?',
detail:
"I see that I'm not in the Applications folder. I can move myself there if you'd like!",
icon: path.join(__dirname, 'assets/icons/png/256x256.png'),
icon: path.join(__dirname, icon256),
cancelId: 1,
defaultId: 0,
checkboxLabel: 'Do not show this message again',
Expand Down
2 changes: 1 addition & 1 deletion src/services/platform.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const PACKAGE_MANAGER_CMD = path.join(
remote.app.getAppPath(),
'node_modules/yarn/bin',
formatCommandForPlatform(PACKAGE_MANAGER)
);
).replace('app.asar', 'app.asar.unpacked');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this required? Can you please give some details?
What was the error message if you're not replacing it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once unpacking yarn, it is placed under the directory \app.asar.unpacked\node_modules.. instead of within the \app.asar\node_modules.. asar file. Even though it was unpacked, code is still referencing it within the app.asar, so replacing the path.

Same issue is discussed here. I elected to workaround since this was the only case I knew of, but we could instead use the hazardous package referenced in the discussion if desired.


// Forward the host env, and append the
// project's .bin directory to PATH to allow
Expand Down
Loading