From d09b94a5db363adb09eda3593a081bcdee639381 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 17 Jan 2018 15:29:34 -0500 Subject: [PATCH] refactor pipeline to remove duplicated code --- lib/pipeline/pipeline.js | 172 ++++++++++++++------------------------- 1 file changed, 59 insertions(+), 113 deletions(-) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 11d7c9a1..950ed89a 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -55,26 +55,7 @@ class Pipeline { async.waterfall([ function findImports(next) { - webpack({ - entry: utils.joinPath(fs.dappPath(), file.filename), - output: { - libraryTarget: 'umd', - path: utils.joinPath(fs.dappPath(), '.embark'), - filename: file.filename - }, - resolve: { - alias: importsList, - modules: [ - fs.embarkPath('node_modules'), - utils.joinPath(fs.dappPath(), 'node_modules') - ] - }, - externals: function(context, request, callback) { - callback(); - } - }).run((_err, _stats) => { - next(); - }); + self.webpackRun(file.filename, {}, false, importsList, next); }, function changeCwd(next) { @@ -84,20 +65,7 @@ class Pipeline { }, function findImportsPhase2(next) { - webpack({ - entry: utils.joinPath(fs.dappPath(), file.filename), - output: { - libraryTarget: 'umd', - path: utils.joinPath(fs.dappPath(), '.embark'), - filename: file.filename - }, - resolve: { - alias: importsList, - modules: [ - fs.embarkPath('node_modules'), - utils.joinPath(fs.dappPath(), 'node_modules') - ] - }, + self.webpackRun(file.filename, { externals: function(context, request, callback) { if (request === utils.joinPath(fs.dappPath(), file.filename)) { callback(); @@ -111,89 +79,12 @@ class Pipeline { } callback(null, "amd " + Math.random()); } - }, - 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: { - presets: ['es2016', 'es2017', 'react'], - plugins: ["babel-plugin-webpack-aliases"] - } - } - ] } - }).run((_err, _stats) => { - next(); - }); - + }, true, importsList, next); }, function runWebpack(next) { - webpack({ - entry: utils.joinPath(fs.dappPath(), file.filename), - output: { - libraryTarget: 'umd', - path: utils.joinPath(fs.dappPath(), '.embark'), - filename: file.filename - }, - resolve: { - alias: importsList, - modules: [ - fs.embarkPath('node_modules'), - utils.joinPath(fs.dappPath(), 'node_modules') - ] - }, - externals: function(context, request, callback) { - if (request === "Embark/contracts/all") { - return callback(null, fs.readFileSync(utils.joinPath(fs.dappPath(), '.embark', 'embark.js'))); - // should be .toString() ? - } - callback(); - }, - 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: { - presets: ['es2016', 'es2017', 'react'], - plugins: ["babel-plugin-webpack-aliases"] - } - } - ] - } - }).run((_err, _stats) => { - next(); - }); - + self.webpackRun(file.filename, {}, true, importsList, next); }, function changeCwdBack(next) { @@ -261,6 +152,61 @@ class Pipeline { }); } + webpackRun(filename, options, includeModules, importsList, callback) { + let defaultOptions = { + entry: utils.joinPath(fs.dappPath(), filename), + output: { + libraryTarget: 'umd', + path: utils.joinPath(fs.dappPath(), '.embark'), + filename: filename + }, + resolve: { + alias: importsList, + modules: [ + fs.embarkPath('node_modules'), + utils.joinPath(fs.dappPath(), 'node_modules') + ] + }, + externals: function(context, request, callback) { + callback(); + } + }; + + 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: { + presets: ['es2016', 'es2017', 'react'], + plugins: ["babel-plugin-webpack-aliases"] + } + } + ] + }; + } + + webpack(webpackOptions).run((_err, _stats) => { + callback(); + }); + } + buildContracts(contractsJSON) { fs.mkdirpSync(utils.joinPath(fs.dappPath(), this.buildDir, 'contracts'));