2018-05-10 14:10:09 +00:00
|
|
|
const async = require('async');
|
|
|
|
const webpack = require('webpack');
|
|
|
|
const utils = require('../utils/utils');
|
|
|
|
const fs = require('../core/fs');
|
|
|
|
const constants = require('../constants');
|
2018-05-10 16:22:58 +00:00
|
|
|
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
|
2018-07-27 21:33:50 +00:00
|
|
|
const ProcessWrapper = require('../core/processes/processWrapper');
|
2018-07-18 03:26:56 +00:00
|
|
|
const path = require('path');
|
2018-07-30 21:26:25 +00:00
|
|
|
const glob = require('glob');
|
2018-05-10 14:10:09 +00:00
|
|
|
|
|
|
|
let webpackProcess;
|
|
|
|
|
2018-05-15 19:56:15 +00:00
|
|
|
class WebpackProcess extends ProcessWrapper {
|
2018-07-26 00:58:24 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
this.env = options.env;
|
|
|
|
}
|
|
|
|
|
2018-05-10 14:10:09 +00:00
|
|
|
build(file, importsList, callback) {
|
|
|
|
const self = this;
|
|
|
|
let realCwd;
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
function changeCwd(next) {
|
|
|
|
realCwd = utils.pwd();
|
|
|
|
process.chdir(fs.embarkPath(''));
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
|
|
|
|
function runWebpack(next) {
|
2018-07-18 03:26:56 +00:00
|
|
|
self.webpackRun(file.filename, {}, true, importsList, true, realCwd, next);
|
2018-05-10 14:10:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
function changeCwdBack(next) {
|
|
|
|
process.chdir(realCwd);
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
], (err) => {
|
|
|
|
process.chdir(realCwd);
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-18 03:26:56 +00:00
|
|
|
webpackRun(filename, options, includeModules, importsList, detectErrors, realCwd, callback) {
|
2018-07-20 01:52:18 +00:00
|
|
|
const self = this;
|
2018-07-30 20:36:05 +00:00
|
|
|
glob(fs.dappPath('.embark/versions/*/*'), (err, files) => {
|
2018-07-30 21:26:25 +00:00
|
|
|
let versions;
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
versions = [];
|
|
|
|
} else {
|
|
|
|
versions = files;
|
|
|
|
}
|
|
|
|
let defaultOptions = {
|
|
|
|
mode: self.env === 'production' ? 'production' : 'none',
|
|
|
|
// devtool: self.env === 'development' ? 'source-map' : false,
|
|
|
|
// pipeline would need to copy .map files to dist/ target dir
|
|
|
|
// note: generating full source maps ('source-map') roughly doubles build time
|
|
|
|
entry: fs.dappPath(filename),
|
|
|
|
output: {
|
|
|
|
globalObject: 'typeof self !== \'undefined\' ? self : this',
|
|
|
|
libraryExport: 'default',
|
|
|
|
libraryTarget: 'umd',
|
|
|
|
path: fs.dappPath('.embark'),
|
|
|
|
filename: filename,
|
|
|
|
umdNamedDefine: true
|
|
|
|
},
|
|
|
|
// profile: true,
|
|
|
|
// stats: 'verbose',
|
|
|
|
// note: generating and writing to disk verbose stats increases build time
|
|
|
|
resolve: {
|
|
|
|
alias: importsList,
|
|
|
|
modules: [
|
|
|
|
fs.dappPath('node_modules'),
|
|
|
|
...versions,
|
|
|
|
fs.embarkPath('node_modules')
|
|
|
|
]
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
new HardSourceWebpackPlugin({
|
|
|
|
cacheDirectory: fs.dappPath('node_modules/.cache/hard-source'),
|
|
|
|
// ufglify (wp mode: production) will still save its cache in embark's node_modules/.cache/
|
|
|
|
environmentHash: {
|
|
|
|
root: fs.dappPath()
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
new HardSourceWebpackPlugin.ExcludeModulePlugin(
|
|
|
|
[{test: /app[\\/]|contracts[\\/]/}]
|
|
|
|
)
|
2018-05-10 14:10:09 +00:00
|
|
|
]
|
2018-07-30 21:26:25 +00:00
|
|
|
};
|
2018-05-10 14:10:09 +00:00
|
|
|
|
2018-07-30 21:26:25 +00:00
|
|
|
let webpackOptions = utils.recursiveMerge(defaultOptions, options);
|
2018-05-10 14:10:09 +00:00
|
|
|
|
2018-07-30 21:26:25 +00:00
|
|
|
if (includeModules) {
|
|
|
|
webpackOptions.module = {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: /\.css$/,
|
|
|
|
use: [{loader: "style-loader"}, {loader: "css-loader"}]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.scss$/,
|
|
|
|
use: [{loader: "style-loader"}, {loader: "css-loader"}]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
|
|
|
|
loader: 'url-loader?limit=100000'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.js$/,
|
|
|
|
loader: "babel-loader",
|
2018-07-30 20:36:05 +00:00
|
|
|
exclude: /(node_modules|bower_components|\.embark[\\/]versions)/,
|
2018-07-30 21:26:25 +00:00
|
|
|
options: {
|
|
|
|
presets: [
|
|
|
|
[
|
|
|
|
"@babel/preset-env", {
|
|
|
|
modules: false,
|
|
|
|
targets: {
|
|
|
|
browsers: ["last 1 version", "not dead", "> 0.2%"]
|
|
|
|
}
|
2018-07-20 01:52:18 +00:00
|
|
|
}
|
2018-07-30 21:26:25 +00:00
|
|
|
],
|
|
|
|
"@babel/preset-react"
|
|
|
|
].map(pkg => {
|
|
|
|
if (Array.isArray(pkg)) {
|
|
|
|
let _pkg = pkg[0];
|
|
|
|
pkg[0] = require.resolve(_pkg);
|
|
|
|
return pkg;
|
|
|
|
} else {
|
|
|
|
return require.resolve(pkg);
|
2018-07-20 01:52:18 +00:00
|
|
|
}
|
2018-07-30 21:26:25 +00:00
|
|
|
}),
|
|
|
|
plugins: [
|
|
|
|
"babel-plugin-webpack-aliases",
|
|
|
|
"@babel/plugin-transform-runtime"
|
|
|
|
].map(require.resolve),
|
|
|
|
compact: false
|
|
|
|
}
|
2018-05-10 14:10:09 +00:00
|
|
|
}
|
2018-07-30 21:26:25 +00:00
|
|
|
]
|
|
|
|
};
|
2018-05-10 14:10:09 +00:00
|
|
|
|
2018-07-30 21:26:25 +00:00
|
|
|
let dappBabelrc = path.join(realCwd, '.babelrc');
|
|
|
|
if (fs.existsSync(dappBabelrc)) {
|
|
|
|
webpackOptions.module.rules[3].options.extends = dappBabelrc;
|
|
|
|
}
|
2018-05-10 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 21:26:25 +00:00
|
|
|
webpack(webpackOptions).run((err, stats) => {
|
|
|
|
async.waterfall([
|
|
|
|
function checkStatsError(next) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
if (!detectErrors) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
if (stats.hasErrors()) {
|
|
|
|
return next(
|
|
|
|
stats.toJson(webpackOptions.stats).errors.join("\n")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}//,
|
|
|
|
// function writeStatsReport(next) {
|
|
|
|
// if (detectErrors) {
|
|
|
|
// self._log('info', 'writing file '+ ('.embark/stats.report').bold.dim);
|
|
|
|
// }
|
|
|
|
// fs.writeFile(
|
|
|
|
// path.join(fs.dappPath('.embark'), 'stats.report'),
|
|
|
|
// stats.toString(webpackOptions.stats),
|
|
|
|
// next
|
|
|
|
// );
|
|
|
|
// },
|
|
|
|
// function writeStatsJSON(next) {
|
|
|
|
// if (detectErrors) {
|
|
|
|
// self._log('info','writing file '+ ('.embark/stats.json').bold.dim);
|
|
|
|
// }
|
|
|
|
// fs.writeFile(
|
|
|
|
// path.join(fs.dappPath('.embark'), 'stats.json'),
|
|
|
|
// JSON.stringify(stats.toJson(webpackOptions.stats)),
|
|
|
|
// next
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// note: to visualize the stats info in a browser, do...
|
|
|
|
// `npx webpack-bundle-analyzer <dapp_dir>/.embark/stats.json`
|
|
|
|
], (err) => {
|
|
|
|
callback(err);
|
|
|
|
});
|
2018-07-20 01:51:46 +00:00
|
|
|
});
|
2018-05-10 14:10:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process.on('message', (msg) => {
|
|
|
|
if (msg.action === constants.pipeline.init) {
|
|
|
|
webpackProcess = new WebpackProcess(msg.options);
|
|
|
|
return process.send({result: constants.pipeline.initiated});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.action === constants.pipeline.build) {
|
|
|
|
return webpackProcess.build(msg.file, msg.importsList, (err) => {
|
|
|
|
process.send({result: constants.pipeline.built, error: err});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|