-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shota Matsushita
committed
Nov 26, 2016
1 parent
aef4bf2
commit e8203fb
Showing
6 changed files
with
325 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
dist | ||
coverage | ||
.nyc_output | ||
main.js | ||
main.js.map | ||
.DS_Store | ||
node_modules | ||
release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* eslint-disable strict, no-shadow, consistent-return, | ||
no-use-before-define, no-unused-vars, no-console */ | ||
|
||
'use strict'; | ||
|
||
/** Build file to package the app for release */ | ||
|
||
require('babel-polyfill'); | ||
const os = require('os'); | ||
const webpack = require('webpack'); | ||
const electronCfg = require('./webpack.config.electron').default; | ||
const cfg = require('./webpack.config.production').default; | ||
const packager = require('electron-packager'); | ||
const del = require('del'); | ||
const exec = require('child_process').exec; | ||
const pkg = require('./package.json'); | ||
|
||
/** | ||
* First two values are node path and current script path | ||
* https://nodejs.org/docs/latest/api/process.html#process_process_argv | ||
*/ | ||
const argv = require('minimist')(process.argv.slice(2)); | ||
|
||
/** | ||
* Do not package node modules from 'devDependencies' | ||
* and 'dependencies' that are set as external | ||
*/ | ||
const toNodePath = name => `/node_modules/${name}($|/)`; | ||
const devDeps = Object | ||
.keys(pkg.devDependencies) | ||
.map(toNodePath); | ||
|
||
const depsExternal = Object | ||
.keys(pkg.dependencies) | ||
.filter(name => !electronCfg.externals.includes(name)) | ||
.map(toNodePath); | ||
|
||
|
||
const appName = argv.name || argv.n || pkg.productName; | ||
const shouldUseAsar = argv.asar || argv.a || false; | ||
const shouldBuildAll = argv.all || false; | ||
|
||
|
||
const DEFAULT_OPTS = { | ||
dir: './', | ||
name: appName, | ||
asar: shouldUseAsar, | ||
ignore: [ | ||
'^/test($|/)', | ||
'^/release($|/)', | ||
'^/main.development.js', | ||
] | ||
.concat(devDeps) | ||
.concat(depsExternal), | ||
}; | ||
|
||
const icon = argv.icon || argv.i || 'App/app'; | ||
if (icon) DEFAULT_OPTS.icon = icon; | ||
|
||
const version = argv.version || argv.v; | ||
if (version) { | ||
DEFAULT_OPTS.version = version; | ||
startPack(); | ||
} else { | ||
// use the same version as the currently-installed electron-prebuilt | ||
exec('npm list electron --dev', (err, stdout) => { | ||
if (err) { | ||
DEFAULT_OPTS.version = '1.2.0'; | ||
} else { | ||
DEFAULT_OPTS.version = stdout.split('electron@')[1].replace(/\s/g, ''); | ||
} | ||
|
||
startPack(); | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* @desc Execute the webpack build process on given config object | ||
* @param {Object} cfg | ||
* @return {Promise} | ||
*/ | ||
function build(cfg) { | ||
return new Promise((resolve, reject) => { | ||
webpack(cfg, (err, stats) => { | ||
if (err) return reject(err); | ||
resolve(stats); | ||
}); | ||
}); | ||
} | ||
|
||
|
||
/** @desc Build, clear previous releases and pack new versions */ | ||
async function startPack() { | ||
console.log('start pack...'); | ||
|
||
try { | ||
/** | ||
* - Build the 'Main process' and 'Renderer Process' files. | ||
* - Clear the ./release directory | ||
*/ | ||
await build(electronCfg); | ||
await build(cfg); | ||
const paths = await del('release'); | ||
|
||
// Start the packing process | ||
if (shouldBuildAll) { | ||
// build for all platforms | ||
const archs = ['ia32', 'x64']; | ||
const platforms = ['linux', 'win32', 'darwin']; | ||
|
||
platforms.forEach((plat) => { | ||
archs.forEach((arch) => { | ||
pack(plat, arch, log(plat, arch)); | ||
}); | ||
}); | ||
} else { | ||
// build for current platform only | ||
pack(os.platform(), os.arch(), log(os.platform(), os.arch())); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @desc | ||
* @param {String} plat | ||
* @param {String} arch | ||
* @param {Function} cb | ||
*/ | ||
function pack(plat, arch, cb) { | ||
// there is no darwin ia32 electron | ||
if (plat === 'darwin' && arch === 'ia32') return; | ||
|
||
const iconObj = { | ||
icon: DEFAULT_OPTS.icon + (() => { | ||
let extension = '.png'; | ||
if (plat === 'darwin') extension = '.icns'; | ||
if (plat === 'win32') extension = '.ico'; | ||
|
||
return extension; | ||
})(), | ||
}; | ||
|
||
const opts = Object.assign({}, DEFAULT_OPTS, iconObj, { | ||
platform: plat, | ||
arch, | ||
prune: true, | ||
'app-version': pkg.version || DEFAULT_OPTS.version, | ||
out: `release/${plat}-${arch}`, | ||
}); | ||
|
||
packager(opts, cb); | ||
} | ||
|
||
|
||
/** | ||
* @desc Log out success / error of building for given platform and architecture | ||
* @param {String} plat | ||
* @param {String} arch | ||
* @return {Function} | ||
*/ | ||
function log(plat, arch) { | ||
return (err, filepath) => { | ||
if (err) return console.error(err); | ||
console.log(`${plat}-${arch} finished!`); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Build config for electron 'Main Process' file | ||
*/ | ||
|
||
import webpack from 'webpack'; | ||
import validate from 'webpack-validator'; | ||
import merge from 'webpack-merge'; | ||
import baseConfig from './webpack.config.base'; | ||
|
||
export default validate(merge(baseConfig, { | ||
devtool: 'source-map', | ||
|
||
entry: ['babel-polyfill', './main.development'], | ||
|
||
// 'main.js' in root | ||
output: { | ||
path: __dirname, | ||
filename: './main.js', | ||
}, | ||
|
||
plugins: [ | ||
// Minify the output | ||
new webpack.optimize.UglifyJsPlugin({ | ||
compressor: { | ||
warnings: false, | ||
}, | ||
}), | ||
|
||
// Add source map support for stack traces in node | ||
// https://github.com/evanw/node-source-map-support | ||
new webpack.BannerPlugin( | ||
'require("source-map-support").install();', | ||
{ raw: true, entryOnly: false } | ||
), | ||
|
||
// NODE_ENV should be production so that modules do not perform certain development checks | ||
new webpack.DefinePlugin({ | ||
'process.env': { | ||
NODE_ENV: JSON.stringify('production'), | ||
}, | ||
}), | ||
], | ||
|
||
/** | ||
* Set targed to Electron speciffic node.js env. | ||
* https://github.com/chentsulin/webpack-target-electron-renderer#how-this-module-works | ||
*/ | ||
target: 'electron-main', | ||
|
||
/** | ||
* Disables webpack processing of __dirname and __filename. | ||
* If you run the bundle in node.js it falls back to these values of node.js. | ||
* https://github.com/webpack/webpack/issues/2010 | ||
*/ | ||
node: { | ||
__dirname: false, | ||
__filename: false, | ||
}, | ||
|
||
externals: [ | ||
'source-map-support', | ||
], | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Build config for electron 'Renderer Process' file | ||
*/ | ||
|
||
import webpack from 'webpack'; | ||
import validate from 'webpack-validator'; | ||
import ExtractTextPlugin from 'extract-text-webpack-plugin'; | ||
import merge from 'webpack-merge'; | ||
import baseConfig from './webpack.config.base'; | ||
|
||
const config = validate(merge(baseConfig, { | ||
devtool: 'cheap-module-source-map', | ||
|
||
entry: [ | ||
'babel-polyfill', | ||
'./app/index', | ||
], | ||
|
||
output: { | ||
publicPath: '../dist/', | ||
}, | ||
|
||
module: { | ||
loaders: [ | ||
{ | ||
test: /\.global\.css$/, | ||
loader: ExtractTextPlugin.extract( | ||
'style-loader', | ||
'css-loader', | ||
), | ||
}, | ||
|
||
// Pipe other styles through css modules and apend to style.css | ||
{ | ||
test: /^((?!\.global).)*\.css$/, | ||
loader: ExtractTextPlugin.extract( | ||
'style-loader', | ||
'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]' | ||
), | ||
}, | ||
], | ||
}, | ||
|
||
plugins: [ | ||
// https://webpack.github.io/docs/list-of-plugins.html#occurrenceorderplugin | ||
// https://github.com/webpack/webpack/issues/864 | ||
new webpack.optimize.OccurrenceOrderPlugin(), | ||
|
||
// NODE_ENV should be production so that modules do not perform certain development checks | ||
new webpack.DefinePlugin({ | ||
'process.env.NODE_ENV': JSON.stringify('production'), | ||
}), | ||
|
||
// Minify without warning messages and IE8 support | ||
new webpack.optimize.UglifyJsPlugin({ | ||
compressor: { | ||
screw_ie8: true, | ||
warnings: false, | ||
}, | ||
}), | ||
|
||
// Set the ExtractTextPlugin output filename | ||
new ExtractTextPlugin('style.css', { allChunks: true }), | ||
], | ||
|
||
// https://github.com/chentsulin/webpack-target-electron-renderer#how-this-module-works | ||
target: 'electron-renderer', | ||
})); | ||
|
||
export default config; |