Skip to content

Commit

Permalink
packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
Shota Matsushita committed Nov 26, 2016
1 parent aef4bf2 commit e8203fb
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .gitignore
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
2 changes: 1 addition & 1 deletion App/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="UTF-8">
<title>difftron</title>
<title>Difftron</title>
<script>
if (!process.env.HOT) {
const link = document.createElement('link');
Expand Down
170 changes: 170 additions & 0 deletions package.js
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!`);
};
}
18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "difftron",
"productName": "Difftron",
"version": "1.0.0",
"description": "diff tool based on Electron",
"main": "main.js",
Expand All @@ -9,7 +10,13 @@
"start-hot": "cross-env HOT=1 NODE_ENV=development electron -r babel-register -r babel-polyfill ./main.development",
"test": "ava",
"test:watch": "ava --watch",
"coverage": "nyc ava && nyc report --reporter=html && open coverage/index.html || xdg-open coverage/index.html"
"coverage": "nyc ava && nyc report --reporter=html && open coverage/index.html || xdg-open coverage/index.html",
"build-main": "cross-env NODE_ENV=production node -r babel-register ./node_modules/webpack/bin/webpack --config webpack.config.electron.js --progress --profile --colors",
"build-renderer": "cross-env NODE_ENV=production node -r babel-register ./node_modules/webpack/bin/webpack --config webpack.config.production.js --progress --profile --colors",
"build": "npm run build-main && npm run build-renderer",
"start": "cross-env NODE_ENV=production electron ./",
"package": "cross-env NODE_ENV=production node -r babel-register -r babel-polyfill package.js",
"package-all": "npm run package -- --all"
},
"repository": {
"type": "git",
Expand All @@ -26,6 +33,7 @@
},
"homepage": "https://github.com/romiogaku/difftron#readme",
"devDependencies": {
"asar": "^0.12.3",
"ava": "^0.17.0",
"babel-core": "^6.17.0",
"babel-eslint": "^7.0.0",
Expand All @@ -42,9 +50,12 @@
"concurrently": "^3.1.0",
"cross-env": "^3.1.3",
"css-loader": "^0.25.0",
"del": "^2.2.2",
"devtron": "^1.4.0",
"electron": "^1.4.4",
"electron-devtools-installer": "^2.0.1",
"electron-packager": "^8.3.0",
"electron-rebuild": "^1.4.0",
"eslint": "^3.8.1",
"eslint-config-airbnb": "^12.0.0",
"eslint-formatter-pretty": "^1.1.0",
Expand All @@ -58,6 +69,8 @@
"eslint-plugin-react": "^6.4.1",
"express": "^4.14.0",
"extract-text-webpack-plugin": "^1.0.1",
"json-loader": "^0.5.4",
"minimist": "^1.2.0",
"nyc": "^10.0.0",
"proxyquire": "^1.7.10",
"reactotron-react-js": "^1.5.2",
Expand All @@ -84,7 +97,8 @@
"redux-persist": "^3.5.0",
"redux-saga": "^0.12.1",
"reduxsauce": "^0.2.0",
"seamless-immutable": "^6.3.0"
"seamless-immutable": "^6.3.0",
"source-map-support": "^0.4.6"
},
"ava": {
"files": [
Expand Down
63 changes: 63 additions & 0 deletions webpack.config.electron.js
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',
],
}));
70 changes: 70 additions & 0 deletions webpack.config.production.js
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;

0 comments on commit e8203fb

Please sign in to comment.