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-05-15 21:08:08 +00:00
|
|
|
const ProcessWrapper = require('../process/processWrapper');
|
2018-07-18 03:26:56 +00:00
|
|
|
const path = require('path');
|
2018-05-10 14:10:09 +00:00
|
|
|
|
|
|
|
let webpackProcess;
|
|
|
|
|
2018-05-15 19:56:15 +00:00
|
|
|
class WebpackProcess extends ProcessWrapper {
|
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-05-10 14:10:09 +00:00
|
|
|
let defaultOptions = {
|
|
|
|
entry: fs.dappPath(filename),
|
|
|
|
output: {
|
|
|
|
libraryTarget: 'umd',
|
|
|
|
path: fs.dappPath('.embark'),
|
|
|
|
filename: filename
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
alias: importsList,
|
|
|
|
modules: [
|
|
|
|
fs.embarkPath('node_modules'),
|
|
|
|
fs.dappPath('node_modules')
|
|
|
|
]
|
|
|
|
},
|
2018-07-20 01:47:59 +00:00
|
|
|
plugins: [
|
|
|
|
new HardSourceWebpackPlugin({
|
|
|
|
cacheDirectory: fs.dappPath('node_modules/.cache/hard-source'),
|
|
|
|
// ufglify would still be saving under embark's node_modules/.cache
|
|
|
|
environmentHash: {
|
|
|
|
root: fs.dappPath()
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
new HardSourceWebpackPlugin.ExcludeModulePlugin([{
|
|
|
|
test: /app[\\/]|contracts[\\/]/
|
|
|
|
}])
|
|
|
|
]
|
2018-05-10 14:10:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let webpackOptions = utils.recursiveMerge(defaultOptions, options);
|
|
|
|
|
|
|
|
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",
|
|
|
|
exclude: /(node_modules|bower_components)/,
|
|
|
|
options: {
|
2018-07-18 03:26:56 +00:00
|
|
|
extends: path.join(realCwd, '.babelrc'),
|
2018-05-10 14:10:09 +00:00
|
|
|
presets: ['babel-preset-es2016', 'babel-preset-es2017', 'babel-preset-react'].map(require.resolve),
|
|
|
|
plugins: ["babel-plugin-webpack-aliases"].map(require.resolve),
|
|
|
|
compact: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
webpack(webpackOptions).run((err, stats) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
if (!detectErrors) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stats.hasErrors()) {
|
|
|
|
return callback(stats.toJson().errors.join("\n"));
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|