2018-01-22 23:38:06 +00:00
|
|
|
'use strict';
|
|
|
|
const path = require('path');
|
|
|
|
const webpack = require('webpack');
|
|
|
|
const threadLoader = require('thread-loader');
|
|
|
|
|
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
|
|
const CopyWebpackPlugin = require('copy-webpack-plugin');
|
|
|
|
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
|
2018-04-05 20:53:36 +00:00
|
|
|
const WebappWebpackPlugin = require('webapp-webpack-plugin');
|
|
|
|
// const AutoDllPlugin = require('autodll-webpack-plugin');
|
2018-03-27 04:04:08 +00:00
|
|
|
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
|
2018-01-22 23:38:06 +00:00
|
|
|
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
|
|
|
|
const SriPlugin = require('webpack-subresource-integrity');
|
2018-04-05 20:53:36 +00:00
|
|
|
const MiniCSSExtractPlugin = require('mini-css-extract-plugin');
|
2018-01-22 23:38:06 +00:00
|
|
|
const ClearDistPlugin = require('./plugins/clearDist');
|
|
|
|
|
|
|
|
const config = require('./config');
|
|
|
|
|
|
|
|
const DEFAULT_OPTIONS = {
|
|
|
|
isProduction: false,
|
|
|
|
isElectronBuild: false,
|
|
|
|
isHTMLBuild: false,
|
|
|
|
outputDir: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = function(opts = {}) {
|
|
|
|
const options = Object.assign({}, DEFAULT_OPTIONS, opts);
|
|
|
|
const isDownloadable = options.isHTMLBuild || options.isElectronBuild;
|
2018-04-05 20:53:36 +00:00
|
|
|
const commitHash = process.env.npm_package_gitHead;
|
2018-01-22 23:38:06 +00:00
|
|
|
|
|
|
|
// ====================
|
|
|
|
// ====== Entry =======
|
|
|
|
// ====================
|
|
|
|
const entry = {
|
|
|
|
client: './common/index.tsx'
|
|
|
|
};
|
|
|
|
|
|
|
|
if (options.isProduction) {
|
|
|
|
entry.vendor = config.vendorModules;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ====================
|
|
|
|
// ====== Rules =======
|
|
|
|
// ====================
|
|
|
|
const rules = [];
|
|
|
|
|
|
|
|
// Typescript
|
|
|
|
if (options.isProduction || !process.env.SLOW_BUILD_SPEED) {
|
|
|
|
rules.push(config.typescriptRule);
|
2018-01-25 02:41:39 +00:00
|
|
|
} else {
|
|
|
|
threadLoader.warmup(config.typescriptRule.use[0].options, [
|
|
|
|
config.typescriptRule.use[0].loader
|
|
|
|
]);
|
2018-01-22 23:38:06 +00:00
|
|
|
rules.push({
|
|
|
|
...config.typescriptRule,
|
2018-01-25 02:41:39 +00:00
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: 'thread-loader',
|
|
|
|
options: {
|
|
|
|
workers: 4
|
|
|
|
}
|
|
|
|
},
|
|
|
|
...config.typescriptRule.use
|
|
|
|
]
|
2018-01-22 23:38:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-20 20:08:57 +00:00
|
|
|
// Styles (CSS, SCSS)
|
2018-04-16 23:30:58 +00:00
|
|
|
const sassLoader = {
|
|
|
|
loader: 'sass-loader',
|
|
|
|
options: {
|
|
|
|
data: `$is-electron: ${options.isElectronBuild};`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-22 23:38:06 +00:00
|
|
|
if (options.isProduction) {
|
2018-01-25 02:41:39 +00:00
|
|
|
rules.push(
|
|
|
|
{
|
|
|
|
test: /\.css$/,
|
2018-04-05 20:53:36 +00:00
|
|
|
use: [
|
|
|
|
MiniCSSExtractPlugin.loader,
|
|
|
|
'css-loader'
|
|
|
|
]
|
2018-01-25 02:41:39 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.scss$/,
|
2018-04-05 20:53:36 +00:00
|
|
|
use: [
|
|
|
|
MiniCSSExtractPlugin.loader,
|
|
|
|
'css-loader',
|
2018-04-16 23:30:58 +00:00
|
|
|
sassLoader
|
2018-04-05 20:53:36 +00:00
|
|
|
]
|
2018-01-25 02:41:39 +00:00
|
|
|
}
|
|
|
|
);
|
2018-01-22 23:38:06 +00:00
|
|
|
} else {
|
2018-01-25 02:41:39 +00:00
|
|
|
rules.push(
|
|
|
|
{
|
|
|
|
test: /\.css$/,
|
|
|
|
include: path.resolve(config.path.src, 'vendor'),
|
|
|
|
use: ['style-loader', 'css-loader']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.scss$/,
|
|
|
|
include: ['components', 'containers', 'sass']
|
|
|
|
.map(dir => path.resolve(config.path.src, dir))
|
|
|
|
.concat([config.path.modules]),
|
2018-01-22 23:38:06 +00:00
|
|
|
|
2018-04-16 23:30:58 +00:00
|
|
|
use: ['style-loader', 'css-loader', sassLoader]
|
2018-01-25 02:41:39 +00:00
|
|
|
}
|
|
|
|
);
|
2018-01-22 23:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Web workers
|
|
|
|
rules.push({
|
|
|
|
test: /\.worker\.js$/,
|
|
|
|
loader: 'worker-loader'
|
|
|
|
});
|
|
|
|
|
|
|
|
// Images
|
|
|
|
rules.push({
|
2018-01-25 02:41:39 +00:00
|
|
|
include: [path.resolve(config.path.assets), path.resolve(config.path.modules)],
|
2018-01-22 23:38:06 +00:00
|
|
|
test: /\.(gif|png|jpe?g|svg)$/i,
|
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: 'file-loader',
|
|
|
|
options: {
|
|
|
|
hash: 'sha512',
|
|
|
|
digest: 'hex',
|
|
|
|
name: '[path][name].[ext]?[hash:6]'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: 'image-webpack-loader',
|
|
|
|
options: {
|
|
|
|
bypassOnDebug: true,
|
|
|
|
optipng: {
|
|
|
|
optimizationLevel: 4
|
|
|
|
},
|
|
|
|
gifsicle: {
|
|
|
|
interlaced: false
|
|
|
|
},
|
|
|
|
mozjpeg: {
|
|
|
|
quality: 80
|
|
|
|
},
|
|
|
|
svgo: {
|
|
|
|
plugins: [{ removeViewBox: true }, { removeEmptyAttrs: false }, { sortAttrs: true }]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
// Fonts
|
|
|
|
rules.push({
|
2018-01-25 02:41:39 +00:00
|
|
|
include: [path.resolve(config.path.assets), path.resolve(config.path.modules)],
|
2018-01-22 23:38:06 +00:00
|
|
|
test: /\.(ico|eot|otf|webp|ttf|woff|woff2)(\?.*)?$/,
|
|
|
|
loader: 'file-loader'
|
|
|
|
});
|
|
|
|
|
|
|
|
// ====================
|
|
|
|
// ====== Plugins =====
|
|
|
|
// ====================
|
|
|
|
const plugins = [
|
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
title: config.title,
|
|
|
|
template: path.resolve(config.path.src, 'index.html'),
|
|
|
|
inject: true
|
|
|
|
}),
|
|
|
|
|
|
|
|
new CopyWebpackPlugin([
|
|
|
|
{
|
|
|
|
from: config.path.static,
|
|
|
|
// to the root of dist path
|
|
|
|
to: './'
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
|
|
|
|
new webpack.LoaderOptionsPlugin({
|
|
|
|
minimize: options.isProduction,
|
|
|
|
debug: !options.isProduction,
|
|
|
|
options: {
|
|
|
|
// css-loader relies on context
|
|
|
|
context: process.cwd()
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
new webpack.DefinePlugin({
|
|
|
|
'process.env.NODE_ENV': JSON.stringify(options.isProduction ? 'production' : 'development'),
|
|
|
|
'process.env.BUILD_DOWNLOADABLE': JSON.stringify(isDownloadable),
|
|
|
|
'process.env.BUILD_HTML': JSON.stringify(options.isHTMLBuild),
|
|
|
|
'process.env.BUILD_ELECTRON': JSON.stringify(options.isElectronBuild)
|
2018-01-25 02:41:39 +00:00
|
|
|
})
|
2018-01-22 23:38:06 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if (options.isProduction) {
|
|
|
|
plugins.push(
|
2018-04-05 20:53:36 +00:00
|
|
|
new MiniCSSExtractPlugin({
|
|
|
|
filename: `[name].[contenthash:8].css`
|
2018-01-22 23:38:06 +00:00
|
|
|
}),
|
2018-04-05 20:53:36 +00:00
|
|
|
new WebappWebpackPlugin({
|
2018-02-07 04:28:28 +00:00
|
|
|
logo: path.resolve(config.path.assets, 'images/favicon.png'),
|
2018-04-05 20:53:36 +00:00
|
|
|
cacheDirectory: false, // Cache makes builds nondeterministic
|
|
|
|
inject: true,
|
|
|
|
prefix: 'common/assets/meta-[hash]',
|
|
|
|
favicons: {
|
|
|
|
appDescription: 'Ethereum web interface',
|
|
|
|
display: 'standalone',
|
|
|
|
theme_color: '#007896'
|
|
|
|
}
|
2018-01-22 23:38:06 +00:00
|
|
|
}),
|
|
|
|
new SriPlugin({
|
|
|
|
hashFuncNames: ['sha256', 'sha384'],
|
|
|
|
enabled: true
|
|
|
|
}),
|
|
|
|
new ProgressPlugin(),
|
2018-04-05 20:53:36 +00:00
|
|
|
new ClearDistPlugin()
|
2018-01-25 02:41:39 +00:00
|
|
|
);
|
|
|
|
} else {
|
2018-01-22 23:38:06 +00:00
|
|
|
plugins.push(
|
2018-04-05 20:53:36 +00:00
|
|
|
// new AutoDllPlugin({
|
|
|
|
// inject: true, // will inject the DLL bundles to index.html
|
|
|
|
// filename: '[name]_[hash].js',
|
|
|
|
// debug: true,
|
|
|
|
// context: path.join(config.path.root),
|
|
|
|
// entry: {
|
|
|
|
// vendor: [...config.vendorModules, 'babel-polyfill', 'bootstrap-sass', 'font-awesome']
|
|
|
|
// }
|
|
|
|
// }),
|
2018-01-22 23:38:06 +00:00
|
|
|
new HardSourceWebpackPlugin({
|
|
|
|
environmentHash: {
|
|
|
|
root: process.cwd(),
|
2018-04-05 20:53:36 +00:00
|
|
|
directories: ['common/webpack_config'],
|
2018-01-22 23:38:06 +00:00
|
|
|
files: ['package.json']
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
new webpack.HotModuleReplacementPlugin(),
|
|
|
|
new FriendlyErrorsPlugin()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.isElectronBuild) {
|
|
|
|
// target: 'electron-renderer' kills scrypt, so manually pull in some
|
|
|
|
// of its configuration instead
|
2018-01-25 02:41:39 +00:00
|
|
|
plugins.push(
|
|
|
|
new webpack.ExternalsPlugin('commonjs', [
|
|
|
|
'desktop-capturer',
|
|
|
|
'electron',
|
|
|
|
'ipc',
|
|
|
|
'ipc-renderer',
|
|
|
|
'remote',
|
|
|
|
'web-frame',
|
|
|
|
'clipboard',
|
|
|
|
'crash-reporter',
|
|
|
|
'native-image',
|
|
|
|
'screen',
|
|
|
|
'shell'
|
|
|
|
])
|
|
|
|
);
|
2018-01-22 23:38:06 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 20:53:36 +00:00
|
|
|
// ====================
|
|
|
|
// === Optimization ===
|
|
|
|
// ====================
|
|
|
|
const optimization = {};
|
|
|
|
if (options.isProduction) {
|
|
|
|
optimization.splitChunks = {
|
|
|
|
chunks: 'all'
|
|
|
|
};
|
|
|
|
optimization.concatenateModules = false;
|
|
|
|
}
|
|
|
|
|
2018-01-22 23:38:06 +00:00
|
|
|
// ====================
|
|
|
|
// ====== DevTool =====
|
|
|
|
// ====================
|
|
|
|
let devtool = false;
|
|
|
|
if (!options.isProduction) {
|
2018-02-06 21:33:58 +00:00
|
|
|
if (process.env.VSCODE_DEBUG) {
|
|
|
|
devtool = 'cheap-module-source-map';
|
2018-01-25 02:41:39 +00:00
|
|
|
} else {
|
2018-01-22 23:38:06 +00:00
|
|
|
devtool = 'cheap-module-eval-source-map';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ====================
|
|
|
|
// ====== Output ======
|
|
|
|
// ====================
|
|
|
|
const output = {
|
|
|
|
path: path.resolve(config.path.output, options.outputDir),
|
2018-04-05 20:53:36 +00:00
|
|
|
filename: options.isProduction ? `[name].${commitHash}.js` : '[name].js',
|
2018-01-22 23:38:06 +00:00
|
|
|
publicPath: isDownloadable && options.isProduction ? './' : '/',
|
2018-04-06 20:16:12 +00:00
|
|
|
crossOriginLoading: 'anonymous',
|
|
|
|
// Fix workers & HMR https://github.com/webpack/webpack/issues/6642
|
|
|
|
globalObject: options.isProduction ? undefined : 'self'
|
2018-01-25 02:41:39 +00:00
|
|
|
};
|
2018-01-22 23:38:06 +00:00
|
|
|
|
|
|
|
// The final bundle
|
|
|
|
return {
|
2018-01-25 02:41:39 +00:00
|
|
|
devtool,
|
2018-01-22 23:38:06 +00:00
|
|
|
entry,
|
|
|
|
output,
|
|
|
|
module: { rules },
|
|
|
|
plugins,
|
|
|
|
target: 'web',
|
|
|
|
resolve: config.resolve,
|
|
|
|
performance: {
|
|
|
|
hints: options.isProduction ? 'warning' : false
|
|
|
|
},
|
2018-04-05 20:53:36 +00:00
|
|
|
optimization,
|
|
|
|
mode: options.isProduction ? 'production' : 'development',
|
2018-01-22 23:38:06 +00:00
|
|
|
stats: {
|
|
|
|
// Reduce build output
|
|
|
|
children: false,
|
|
|
|
chunks: false,
|
|
|
|
chunkModules: false,
|
|
|
|
chunkOrigins: false,
|
|
|
|
modules: false
|
|
|
|
}
|
|
|
|
};
|
2018-01-25 02:41:39 +00:00
|
|
|
};
|