From 81acad30c7f3e7e4622b3dc2e2755984b7cc876a Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 13:07:08 -0500 Subject: [PATCH 01/64] babel-plugin-webpack-aliases -> babel-plugin-module-resolver --- package-lock.json | 82 +++++++++++++---------------------------------- package.json | 2 +- 2 files changed, 24 insertions(+), 60 deletions(-) diff --git a/package-lock.json b/package-lock.json index 81d36ff81..e3f145370 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1313,66 +1313,16 @@ "util.promisify": "^1.0.0" } }, - "babel-plugin-webpack-aliases": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/babel-plugin-webpack-aliases/-/babel-plugin-webpack-aliases-1.1.3.tgz", - "integrity": "sha1-+7oor/c+SDc4949wRMGDrSF5toA=", + "babel-plugin-module-resolver": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/babel-plugin-module-resolver/-/babel-plugin-module-resolver-3.1.1.tgz", + "integrity": "sha512-1Q77Al4ydp6nYApJ7sQ2fmgz30WuQgJZegIYuyOdbdpxenB/bSezQ3hDPsumIXGlUS4vUIv+EwFjzzXZNWtARw==", "requires": { - "babel-types": "^6.5.2", - "find-up": "1.1.2" - }, - "dependencies": { - "find-up": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-1.1.2.tgz", - "integrity": "sha1-ay6YIrGizgpgq2TWEOzK1TyyTQ8=", - "requires": { - "path-exists": "^2.0.0", - "pinkie-promise": "^2.0.0" - } - }, - "path-exists": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-2.1.0.tgz", - "integrity": "sha1-D+tsZPD8UY2adU3V77YscCJ2H0s=", - "requires": { - "pinkie-promise": "^2.0.0" - } - } - } - }, - "babel-runtime": { - "version": "6.26.0", - "resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-6.26.0.tgz", - "integrity": "sha1-llxwWGaOgrVde/4E/yM3vItWR/4=", - "requires": { - "core-js": "^2.4.0", - "regenerator-runtime": "^0.11.0" - }, - "dependencies": { - "regenerator-runtime": { - "version": "0.11.1", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz", - "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==" - } - } - }, - "babel-types": { - "version": "6.26.0", - "resolved": "https://registry.npmjs.org/babel-types/-/babel-types-6.26.0.tgz", - "integrity": "sha1-o7Bz+Uq0nrb6Vc1lInozQ4BjJJc=", - "requires": { - "babel-runtime": "^6.26.0", - "esutils": "^2.0.2", - "lodash": "^4.17.4", - "to-fast-properties": "^1.0.3" - }, - "dependencies": { - "to-fast-properties": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-1.0.3.tgz", - "integrity": "sha1-uDVx+k2MJbguIxsG46MFXeTKGkc=" - } + "find-babel-config": "^1.1.0", + "glob": "^7.1.2", + "pkg-up": "^2.0.0", + "reselect": "^3.0.1", + "resolve": "^1.4.0" } }, "balanced-match": { @@ -3631,6 +3581,15 @@ } } }, + "find-babel-config": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/find-babel-config/-/find-babel-config-1.1.0.tgz", + "integrity": "sha1-rMAQQ6Z0n+w0Qpvmtk9ULrtdY1U=", + "requires": { + "json5": "^0.5.1", + "path-exists": "^3.0.0" + } + }, "find-cache-dir": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/find-cache-dir/-/find-cache-dir-1.0.0.tgz", @@ -7856,6 +7815,11 @@ "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz", "integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=" }, + "reselect": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/reselect/-/reselect-3.0.1.tgz", + "integrity": "sha1-79qpjqdFEyTQkrKyFjpqHXqaIUc=" + }, "resolve": { "version": "1.8.1", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.8.1.tgz", diff --git a/package.json b/package.json index b4d73f520..d73906e40 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "ascii-table": "0.0.9", "async": "^2.0.1", "babel-loader": "8.0.0-beta.4", - "babel-plugin-webpack-aliases": "^1.1.3", + "babel-plugin-module-resolver": "^3.1.1", "bip39": "^2.5.0", "chokidar": "^2.0.3", "clone-deep": "^4.0.0", From 25cb0a572c7ced708e8b778be28d6e596caa18fb Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 13:07:40 -0500 Subject: [PATCH 02/64] lodash.clonedeep (there's a bug in clone-deep pkg re: regex) --- package-lock.json | 5 +++++ package.json | 1 + 2 files changed, 6 insertions(+) diff --git a/package-lock.json b/package-lock.json index e3f145370..a4c0c4666 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5554,6 +5554,11 @@ "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", "integrity": "sha1-soqmKIorn8ZRA1x3EfZathkDMaY=" }, + "lodash.clonedeep": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz", + "integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8=" + }, "lodash.debounce": { "version": "4.0.8", "resolved": "https://registry.npmjs.org/lodash.debounce/-/lodash.debounce-4.0.8.tgz", diff --git a/package.json b/package.json index d73906e40..c0f2f5e1d 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "ipfs-api": "17.2.4", "is-valid-domain": "0.0.5", "live-plugin-manager-git-fix": "^0.12.1", + "lodash.clonedeep": "^4.5.0", "merge": "^1.2.0", "mocha": "^5.2.0", "multihashes": "^0.4.13", From d98476733bbbc28020a2920141a2b8ce3b18a25b Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 15:49:19 -0500 Subject: [PATCH 03/64] standalone eject-able webpack config --- webpack.config.js | 156 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 155 insertions(+), 1 deletion(-) diff --git a/webpack.config.js b/webpack.config.js index 0967ef424..befc28c80 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1 +1,155 @@ -{} +/* global module process require */ + +// some packages, plugins, and presets referenced/required in this webpack +// config are deps of embark and will be transitive dapp deps unless specified +// in the dapp's own package.json (perhaps with a different version/range or +// other specifier) + +// there's a bug in pkg clone-deep re: regex; for now use lodash.clonedeep +// see: https://github.com/jonschlinkert/clone-deep/pull/14 +const cloneDeep = require('lodash.clonedeep'); +const fs = require('fs'); +const glob = require('glob'); +const HardSourceWebpackPlugin = require('hard-source-webpack-plugin'); +const path = require('path'); + +const dappPath = process.env.DAPP_PATH; +const embarkAliases = require(path.join(dappPath, '.embark/embark-aliases.json')); +const embarkAssets = require(path.join(dappPath, '.embark/embark-assets.json')); + +// it's important to `embark reset` if a pkg version is specified in +// embark.json and changed/removed later, otherwise pkg resolution may behave +// unexpectedly, e.g. a different version may get used owing to lexographical +// order or because .embark/versions is still in place even though defaults are +// desired; once embark is always a lib within a dapp's node_modules tree, +// embark's pkg-version management functionality can probably be deprecated in +// favor of a dapp simply specifying dependencies in its own package.json, +// e.g. a different ipfs version than embark's default +let versions; +try { + versions = glob.sync(path.join(dappPath, '.embark/versions/*/*')); +} catch (e) { + versions = []; +} + +const entry = Object.keys(embarkAssets).reduce((obj, key) => { + embarkAssets[key] + .filter(file => file.filename.match(/\.js$/)) + .forEach(file => { + let file_path = file.path; + if (!file.path.match(/^\.\//)) { + file_path = './' + file_path; + } + obj[file.path] = file_path; + }); + return obj; +}, {}); + +// base config +// ----------------------------------------------------------------------------- + +const base = { + context: dappPath, + entry: entry, + 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|\.embark[\\/]versions)/, + options: { + plugins: [ + [ + 'module-resolver', { + 'alias': embarkAliases + } + ], + [ + '@babel/plugin-transform-runtime', { + corejs: 2, + useESModules: true + } + ] + ], + presets: [ + [ + '@babel/preset-env', { + modules: false, + targets: { + browsers: ['last 1 version', 'not dead', '> 0.2%'] + } + } + ], + '@babel/preset-react' + ] + } + } + ] + }, + output: { + filename: (chunkData) => chunkData.chunk.name, + // globalObject workaround for node-compatible UMD builds with webpack 4 + // see: https://github.com/webpack/webpack/issues/6522#issuecomment-371120689 + globalObject: 'typeof self !== \'undefined\' ? self : this', + libraryTarget: 'umd', + path: path.join(dappPath, '.embark') + }, + plugins: [ + new HardSourceWebpackPlugin() + ], + // profiling and generating verbose stats increases build time; if stats + // are generated embark will write the output to: + // path.join(dappPath, '.embark/stats.[json,report]') + // to visualize the stats info in a browser run: + // npx webpack-bundle-analyzer /.embark/stats.json + profile: true, stats: 'verbose', + resolve: { + alias: embarkAliases, + modules: [ + ...versions, + 'node_modules' + ] + } +}; + +// development config +// ----------------------------------------------------------------------------- + +const development = cloneDeep(base); +// full source maps increase build time but are useful during dapp development +// HOWEVER: source maps are broken (completely? not sure) until a webpack +// chunk corresponds to a file being written to buildDir per embark.json; +// currently embark is concatenating webpack chunks +// dev.devtool = 'source-map'; +// alternative: development.mode = 'none' +development.mode = 'development'; +development.name = 'development'; +const devBabelLoader = development.module.rules[3]; +devBabelLoader.options.compact = false; + +// production config +// ----------------------------------------------------------------------------- + +const production = cloneDeep(base); +production.mode = 'production'; +production.name = 'production'; + +// export a list of named configs +// ----------------------------------------------------------------------------- + +module.exports = [ + development, + production +]; From b41f5a397d74310bd89896858d7d88f231731f16 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 15:51:34 -0500 Subject: [PATCH 04/64] --pipeline option for `ebmark build|run` to spec webpack config name --- cmd/cmd.js | 6 +++++- cmd/cmd_controller.js | 6 ++++-- lib/core/engine.js | 4 +++- lib/pipeline/pipeline.js | 2 ++ lib/pipeline/webpackProcess.js | 2 +- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index 53ba06f1b..dbb43412c 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -99,6 +99,7 @@ class Cmd { .option('-c, --client [client]', __('Use a specific ethereum client or simulator (supported: %s)', 'geth, testrpc')) .option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug') .option('--locale [locale]', __('language to use (default: en)')) + .option('--pipeline [pipeline]', __('webpack config to use (default: production)')) .description(__('deploy and build dapp at ') + 'dist/ (default: development)') .action(function (env, _options) { i18n.setOrDetectLocale(_options.locale); @@ -107,6 +108,7 @@ class Cmd { _options.logLevel = _options.loglevel; // fix casing _options.onlyCompile = _options.contracts; _options.client = _options.client || 'geth'; + _options.webpackConfigName = _options.pipeline || 'production'; embark.build(_options); }); } @@ -123,6 +125,7 @@ class Cmd { .option('--logfile [logfile]', __('filename to output logs (default: %s)', 'none')) .option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug') .option('--locale [locale]', __('language to use (default: en)')) + .option('--pipeline [pipeline]', __('webpack config to use (default: development)')) .description(__('run dapp (default: %s)', 'development')) .action(function (env, options) { i18n.setOrDetectLocale(options.locale); @@ -135,7 +138,8 @@ class Cmd { runWebserver: !options.noserver, useDashboard: !options.nodashboard, logFile: options.logfile, - logLevel: options.loglevel + logLevel: options.loglevel, + webpackConfigName: options.pipeline || 'development' }); }); } diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 43f5523f1..93af42a4b 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -84,7 +84,8 @@ class EmbarkController { logLevel: options.logLevel, context: self.context, useDashboard: options.useDashboard, - webServerConfig: webServerConfig + webServerConfig: webServerConfig, + webpackConfigName: options.webpackConfigName }); engine.init(); @@ -191,7 +192,8 @@ class EmbarkController { logger: options.logger, config: options.config, plugins: options.plugins, - context: this.context + context: this.context, + webpackConfigName: options.webpackConfigName }); engine.init(); diff --git a/lib/core/engine.js b/lib/core/engine.js index c6857095f..3c54aa368 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -18,6 +18,7 @@ class Engine { this.useDashboard = options.useDashboard; this.webServerConfig = options.webServerConfig; this.ipcRole = options.ipcRole; + this.webpackConfigName = options.webpackConfigName; } init(_options) { @@ -102,7 +103,8 @@ class Engine { assetFiles: this.config.assetFiles, events: this.events, logger: this.logger, - plugins: this.plugins + plugins: this.plugins, + webpackConfigName: this.webpackConfigName }); this.events.on('code-generator-ready', function () { self.events.request('code', function (abi, contractsJSON) { diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 76f69e427..b5829f30b 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -14,6 +14,7 @@ class Pipeline { this.events = options.events; this.logger = options.logger; this.plugins = options.plugins; + this.webpackConfigName = options.webpackConfigName; this.pipelinePlugins = this.plugins.getPluginsFor('pipeline'); } @@ -81,6 +82,7 @@ class Pipeline { }); }); }, + webpackProcess.send({action: constants.pipeline.init, options: {webpackConfigName: self.webpackConfigName}}); function assetFileWrite(next) { async.eachOf(self.assetFiles, function (files, targetFile, cb) { async.map(files, diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index 32e579fb2..ef420626a 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -13,7 +13,7 @@ let webpackProcess; class WebpackProcess extends ProcessWrapper { constructor(options) { super(options); - this.env = options.env; + this.webpackConfigName = options.webpackConfigName; } build(file, importsList, callback) { From 51ac4ddee852b05195ccf6bcbc0e4e3b9e49cc26 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 15:52:09 -0500 Subject: [PATCH 05/64] `embark reset` should cleanup dapp's node_modules/.cache --- cmd/cmd_controller.js | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 93af42a4b..877ccecb9 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -406,6 +406,7 @@ class EmbarkController { var fs = require('../lib/core/fs.js'); fs.removeSync('./chains.json'); fs.removeSync('.embark/'); + fs.removeSync('node_modules/.cache'); fs.removeSync('dist/'); console.log(__("reset done!").green); } From c60d26e258d3c607a3f17df527839bcda3cbee71 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 15:52:33 -0500 Subject: [PATCH 06/64] make fs.embarkPath more flexible --- lib/core/fs.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/core/fs.js b/lib/core/fs.js index 927632c73..ccfe40e19 100644 --- a/lib/core/fs.js +++ b/lib/core/fs.js @@ -77,9 +77,8 @@ function removeSync() { return fs.removeSync.apply(fs.removeSync, arguments); } -// returns embarks root directory -function embarkPath(fileOrDir) { - return utils.joinPath(__dirname, '/../../', fileOrDir); +function embarkPath() { + return utils.joinPath(__dirname, '/../../', ...arguments); } function dappPath() { From 2f2f007d5c3ff913783db739b2718816a4014e8a Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 15:53:23 -0500 Subject: [PATCH 07/64] use embark's webpack config, or a dapp's webpack config if it exists --- lib/pipeline/webpackProcess.js | 291 ++++++++++++--------------------- 1 file changed, 106 insertions(+), 185 deletions(-) diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index ef420626a..0481e80d4 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -1,12 +1,8 @@ -const async = require('async'); -const webpack = require('webpack'); -const utils = require('../utils/utils'); -const fs = require('../core/fs'); const constants = require('../constants'); -const HardSourceWebpackPlugin = require('hard-source-webpack-plugin'); +const fs = require('../core/fs'); const ProcessWrapper = require('../core/processes/processWrapper'); -const path = require('path'); -const glob = require('glob'); +const webpack = require('webpack'); +const writeFile = require('util').promisify(require('fs').writeFile); let webpackProcess; @@ -14,195 +10,120 @@ class WebpackProcess extends ProcessWrapper { constructor(options) { super(options); this.webpackConfigName = options.webpackConfigName; + if (!process.env.DAPP_PATH) { + process.env.DAPP_PATH = fs.dappPath(); + } + if (!process.env.EMBARK_PATH) { + process.env.EMBARK_PATH = fs.embarkPath(); + } } - build(file, importsList, callback) { + async build(assets, importsList, callback) { + try { + await this.webpackRun(assets, importsList, callback); + } catch (e) { + console.error(e.message); + callback(e); + // ?? should return e or e.message + } + } + + async webpackRun(assets, importsList, callback) { const self = this; - let realCwd; + try { + await writeFile( + fs.dappPath('.embark/embark-aliases.json'), + JSON.stringify(importsList) + ); + await writeFile( + fs.dappPath('.embark/embark-assets.json'), + JSON.stringify(assets) + ); + } catch (e) { + console.error(e.message); + return callback(e); + // ?? should return e or e.message + } - async.waterfall([ - function changeCwd(next) { - realCwd = utils.pwd(); - process.chdir(fs.embarkPath('')); - next(); - }, + const dappConfigPath = fs.dappPath('webpack.config.js'); + const defaultConfigPath = fs.embarkPath('webpack.config.js'); - function runWebpack(next) { - self.webpackRun(file.filename, {}, true, importsList, true, realCwd, next); - }, - - function changeCwdBack(next) { - process.chdir(realCwd); - next(); + let config, configPath; + try { + if (fs.existsSync(dappConfigPath)) { + delete require.cache[dappConfigPath]; + configPath = dappConfigPath; + config = require(dappConfigPath); + } else { + configPath = defaultConfigPath; + config = require(defaultConfigPath); } - ], (err) => { - process.chdir(realCwd); - callback(err); - }); - } + // valid config types: https://webpack.js.org/configuration/configuration-types/ + // + function that returns a config object + // + function that returns a promise for a config object + // + array of named config objects + // + config object + if (typeof config === 'function') { + config = await config(self.webpackConfigName); + } else if (Array.isArray(config)) { + config = config.filter(cfg => cfg.name === self.webpackConfigName); + if (!config.length) { + const errMsg = `no webpack config has the name '${self.webpackConfigName}'`; + console.error(errMsg); + return callback(errMsg); + // ?? should the message be wrapped in new Error() + } + if (config.length > 1) { + console.warn( + `detected ${config.length} webpack configs having the name '${self.webpackConfigName}', using the first one` + ); + } + config = config[0]; + } else { + // proceed with the value obtained from require(dapp/default/Config) + } + } catch (e) { + console.error(`error while loading webpack config ${configPath}`); + console.error(e.message); + callback(e); + // ?? should return e or e.message + } - webpackRun(filename, options, includeModules, importsList, detectErrors, realCwd, callback) { - const self = this; - glob(fs.dappPath('.embark/versions/*/*'), (err, files) => { - let versions; + if (typeof config !== 'object' || config === null) { + const errMsg = 'bad webpack config, the resolved config was null or not an object'; + console.error(errMsg); + return callback(errMsg); + // ?? should the message be wrapped in new Error() + } + + webpack(config).run(async (err, stats) => { if (err) { console.error(err); - versions = []; - } else { - versions = files; + return callback(err); } - 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[\\/]/}] - ) - ] - }; - - 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|\.embark[\\/]versions)/, - options: { - presets: [ - [ - "@babel/preset-env", { - modules: false, - targets: { - browsers: ["last 1 version", "not dead", "> 0.2%"] - } - } - ], - "@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); - } - }), - plugins: [ - "babel-plugin-webpack-aliases", - [ - "@babel/plugin-transform-runtime", { - corejs: 2, - useESModules: true - } - ] - ].map(pkg => { - if (Array.isArray(pkg)) { - let _pkg = pkg[0]; - pkg[0] = require.resolve(_pkg); - return pkg; - } else { - return require.resolve(pkg); - } - }), - compact: false - } - } - ] - }; - - let dappBabelrc = path.join(realCwd, '.babelrc'); - if (fs.existsSync(dappBabelrc)) { - webpackOptions.module.rules[3].options.extends = dappBabelrc; + try { + if (config.stats && config.stats !== 'none') { + self._log('info', 'writing file '+ ('.embark/stats.report').bold.dim); + await writeFile( + fs.dappPath('.embark/stats.report'), + stats.toString(config.stats) + ); + self._log('info','writing file '+ ('.embark/stats.json').bold.dim); + await writeFile( + fs.dappPath('.embark/stats.json'), + JSON.stringify(stats.toJson(config.stats)) + ); } + } catch (e) { + console.error(e.message); + return callback(err); } - - 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 /.embark/stats.json` - ], (err) => { - callback(err); - }); - }); + if (config.stats && stats.hasErrors()) { + const errors = stats.toJson(config.stats).errors.join('\n'); + console.error(errors); + return callback(errors); + } + callback(); }); } } @@ -214,7 +135,7 @@ process.on('message', (msg) => { } if (msg.action === constants.pipeline.build) { - return webpackProcess.build(msg.file, msg.importsList, (err) => { + return webpackProcess.build(msg.assets, msg.importsList, (err) => { process.send({result: constants.pipeline.built, error: err}); }); } From 6282e092f18072a3a3c0a608b2216d692b54a255 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 15:56:48 -0500 Subject: [PATCH 08/64] note/question --- lib/pipeline/pipeline.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index b5829f30b..e965377e3 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -32,6 +32,9 @@ class Pipeline { self.events.request('embark-building-placeholder', (html) => { fs.mkdirpSync(self.buildDir); // create dist/ folder if not already exists fs.writeFile(self.buildDir + 'index.html', html, next); + // ^ ?? should this use path.join instead? templates' embark.json + // files have a '/' hardcoded on the end of buildDir but that doesn't + // seem too resilient }); }, function buildTheContracts(next) { From e9ec96859841b94b43ac6b88ab81737b08e035bc Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 15:57:38 -0500 Subject: [PATCH 09/64] whitespace --- lib/pipeline/pipeline.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index e965377e3..b8f7f234b 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -55,7 +55,7 @@ class Pipeline { next(); }, - function writeContracts(next) { + function writeContracts(next) { self.events.request('contracts:list', (_err, contracts) => { // ensure the .embark/contracts directory exists (create if not exists) fs.mkdirp(fs.dappPath(".embark/contracts", ''), (err) => { @@ -79,7 +79,7 @@ class Pipeline { if(idx < contracts.length - 1) importsHelperFile.write(',\n'); // add a comma if we have more contracts to add }); }, function(){ - importsHelperFile.write('\n}'); // close the module.exports = {} + importsHelperFile.write('\n}'); // close the module.exports = {} importsHelperFile.close(next); // close the write stream }); }); From f654c0a013ac56684d2956e3aa468fd10e7ef904 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 15:58:19 -0500 Subject: [PATCH 10/64] single webpack run --- lib/pipeline/pipeline.js | 49 ++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index b8f7f234b..d29531f27 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -85,7 +85,31 @@ class Pipeline { }); }); }, + function runWebpack(next) { + self.logger.info(__('running webpack...')); + let built = false; + const webpackProcess = new ProcessLauncher({ + modulePath: utils.joinPath(__dirname, 'webpackProcess.js'), + logger: self.logger, + events: self.events, + exitCallback: function (code) { + if (!built) { + return next(`Webpack build exited with code ${code} before the process finished`); + } + if (code) { + self.logger(__('Webpack build process exited with code ', code)); + } + } + }); webpackProcess.send({action: constants.pipeline.init, options: {webpackConfigName: self.webpackConfigName}}); + webpackProcess.send({action: constants.pipeline.build, assets: self.assetFiles, importsList}); + + webpackProcess.once('result', constants.pipeline.built, (msg) => { + built = true; + webpackProcess.kill(); + return next(msg.error); + }); + }, function assetFileWrite(next) { async.eachOf(self.assetFiles, function (files, targetFile, cb) { async.map(files, @@ -101,31 +125,6 @@ class Pipeline { // JS files async.waterfall([ - function runWebpack(next) { - let built = false; - const webpackProcess = new ProcessLauncher({ - modulePath: utils.joinPath(__dirname, 'webpackProcess.js'), - logger: self.logger, - events: self.events, - exitCallback: function (code) { - if (!built) { - return next(`File building of ${file.filename} exited with code ${code} before the process finished`); - } - if (code) { - self.logger(__('File building process exited with code ', code)); - } - } - }); - webpackProcess.send({action: constants.pipeline.init, options: {env: self.env}}); - webpackProcess.send({action: constants.pipeline.build, file, importsList}); - - webpackProcess.once('result', constants.pipeline.built, (msg) => { - built = true; - webpackProcess.kill(); - return next(msg.error); - }); - }, - function readFile(next) { fs.readFile('./.embark/' + file.filename, (err, data) => { if (err) { From 878186660bc130a78ee4181f83c8e6dd50b8f8b1 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 15:59:41 -0500 Subject: [PATCH 11/64] change timing of "writing file" messages --- lib/pipeline/pipeline.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index d29531f27..7e7fe3453 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -113,6 +113,11 @@ class Pipeline { function assetFileWrite(next) { async.eachOf(self.assetFiles, function (files, targetFile, cb) { async.map(files, + const isDir = targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1; + // if it's not a directory + if (!isDir) { + self.logger.info(__("writing file") + " " + (self.buildDir + targetFile).bold.dim); + } function (file, fileCb) { self.logger.trace("reading " + file.filename); @@ -156,7 +161,7 @@ class Pipeline { fs.mkdirpSync(self.buildDir + dir); // if it's a directory - if (targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1) { + if (isDir) { let targetDir = targetFile; if (targetDir.slice(-1) !== '/') { @@ -179,7 +184,6 @@ class Pipeline { return file.content; }).join("\n"); - self.logger.info(__("writing file") + " " + (self.buildDir + targetFile).bold.dim); if(new RegExp(/^index.html?/i).test(targetFile)){ targetFile = targetFile.replace('index', 'index-temp'); placeholderPage = targetFile; From 963ecc0d925dcb9375ce9aaf05ea18619f2ed843 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 16:00:06 -0500 Subject: [PATCH 12/64] formatting --- lib/pipeline/pipeline.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 7e7fe3453..63225cf37 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -111,13 +111,16 @@ class Pipeline { }); }, function assetFileWrite(next) { - async.eachOf(self.assetFiles, function (files, targetFile, cb) { - async.map(files, + async.eachOf( + self.assetFiles, + function (files, targetFile, cb) { const isDir = targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1; // if it's not a directory if (!isDir) { self.logger.info(__("writing file") + " " + (self.buildDir + targetFile).bold.dim); } + async.map( + files, function (file, fileCb) { self.logger.trace("reading " + file.filename); @@ -192,7 +195,8 @@ class Pipeline { } ); }, - next); + next + ); }, function removePlaceholderPage(next){ let placeholderFile = self.buildDir + placeholderPage; @@ -212,7 +216,8 @@ class Pipeline { if (self.pipelinePlugins.length <= 0) { return fileCb(null, {content: fileContent, filename: file.filename, path: file.path, basedir: file.basedir, modified: true}); } - async.eachSeries(self.pipelinePlugins, + async.eachSeries( + self.pipelinePlugins, function(plugin, pluginCB) { if (file.options && file.options.skipPipeline) { return pluginCB(); From 7291fd11871dec930aa92d4723225a52428ca476 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 16:38:50 -0500 Subject: [PATCH 13/64] can use `this`, don't need `self` --- lib/pipeline/webpackProcess.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index 0481e80d4..87aa1fa68 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -29,7 +29,6 @@ class WebpackProcess extends ProcessWrapper { } async webpackRun(assets, importsList, callback) { - const self = this; try { await writeFile( fs.dappPath('.embark/embark-aliases.json'), @@ -64,18 +63,18 @@ class WebpackProcess extends ProcessWrapper { // + array of named config objects // + config object if (typeof config === 'function') { - config = await config(self.webpackConfigName); + config = await config(this.webpackConfigName); } else if (Array.isArray(config)) { - config = config.filter(cfg => cfg.name === self.webpackConfigName); + config = config.filter(cfg => cfg.name === this.webpackConfigName); if (!config.length) { - const errMsg = `no webpack config has the name '${self.webpackConfigName}'`; + const errMsg = `no webpack config has the name '${this.webpackConfigName}'`; console.error(errMsg); return callback(errMsg); // ?? should the message be wrapped in new Error() } if (config.length > 1) { console.warn( - `detected ${config.length} webpack configs having the name '${self.webpackConfigName}', using the first one` + `detected ${config.length} webpack configs having the name '${this.webpackConfigName}', using the first one` ); } config = config[0]; @@ -103,12 +102,12 @@ class WebpackProcess extends ProcessWrapper { } try { if (config.stats && config.stats !== 'none') { - self._log('info', 'writing file '+ ('.embark/stats.report').bold.dim); + this._log('info', 'writing file '+ ('.embark/stats.report').bold.dim); await writeFile( fs.dappPath('.embark/stats.report'), stats.toString(config.stats) ); - self._log('info','writing file '+ ('.embark/stats.json').bold.dim); + this._log('info','writing file '+ ('.embark/stats.json').bold.dim); await writeFile( fs.dappPath('.embark/stats.json'), JSON.stringify(stats.toJson(config.stats)) From df84fce9538d00b1f1a88a864822e0816209c2b9 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 16:49:32 -0500 Subject: [PATCH 14/64] support .jsx --- webpack.config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webpack.config.js b/webpack.config.js index befc28c80..95292fefd 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -34,7 +34,7 @@ try { const entry = Object.keys(embarkAssets).reduce((obj, key) => { embarkAssets[key] - .filter(file => file.filename.match(/\.js$/)) + .filter(file => file.filename.match(/\.jsx?$/)) .forEach(file => { let file_path = file.path; if (!file.path.match(/^\.\//)) { @@ -66,7 +66,7 @@ const base = { loader: 'url-loader?limit=100000' }, { - test: /\.js$/, + test: /\.jsx?$/, loader: 'babel-loader', exclude: /(node_modules|bower_components|\.embark[\\/]versions)/, options: { From 1f56f717b87da7ea1cf35710c393368587593cde Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 17:01:21 -0500 Subject: [PATCH 15/64] 'running webpack' message should indicate config name --- lib/pipeline/pipeline.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 63225cf37..d830de593 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -86,7 +86,7 @@ class Pipeline { }); }, function runWebpack(next) { - self.logger.info(__('running webpack...')); + self.logger.info(__(`running webpack with '${self.webpackConfigName}' config...`)); let built = false; const webpackProcess = new ProcessLauncher({ modulePath: utils.joinPath(__dirname, 'webpackProcess.js'), From 04fcb1510abd16fc31db962af84d345406a38e8a Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 17:01:50 -0500 Subject: [PATCH 16/64] rev comment --- lib/pipeline/webpackProcess.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index 87aa1fa68..27f364e7c 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -79,7 +79,7 @@ class WebpackProcess extends ProcessWrapper { } config = config[0]; } else { - // proceed with the value obtained from require(dapp/default/Config) + // proceed with the value obtained from require(dapp/default/ConfigPath) } } catch (e) { console.error(`error while loading webpack config ${configPath}`); From 35f92f2ae85e429336d30ed873e198eec57ce05e Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 17:02:06 -0500 Subject: [PATCH 17/64] more DRY re: config --- lib/pipeline/webpackProcess.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index 27f364e7c..3bd700b91 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -50,13 +50,12 @@ class WebpackProcess extends ProcessWrapper { let config, configPath; try { if (fs.existsSync(dappConfigPath)) { - delete require.cache[dappConfigPath]; configPath = dappConfigPath; - config = require(dappConfigPath); + delete require.cache[configPath]; } else { configPath = defaultConfigPath; - config = require(defaultConfigPath); } + config = require(configPath); // valid config types: https://webpack.js.org/configuration/configuration-types/ // + function that returns a config object // + function that returns a promise for a config object From 2670476d5eea88c978d3c73287b8a8920a6bd74a Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 18:02:54 -0500 Subject: [PATCH 18/64] `embark eject-webpack` command --- cmd/cmd.js | 10 ++++++++++ cmd/cmd_controller.js | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/cmd/cmd.js b/cmd/cmd.js index dbb43412c..c565adcf7 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -18,6 +18,7 @@ class Cmd { this.simulator(); this.test(); this.reset(); + this.ejectWebpack(); this.graph(); this.upload(); this.versionCmd(); @@ -280,6 +281,15 @@ class Cmd { }); } + ejectWebpack() { + program + .command('eject-webpack') + .description(__('copy the default webpack config into your dapp for customization')) + .action(function () { + embark.ejectWebpack(); + }); + } + versionCmd() { program .command('version') diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 877ccecb9..6db6c8b7c 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -411,6 +411,22 @@ class EmbarkController { console.log(__("reset done!").green); } + ejectWebpack() { + var fs = require('../lib/core/fs.js'); + var dappConfig = fs.dappPath('webpack.config.js'); + var embarkConfig = fs.embarkPath('webpack.config.js'); + if (fs.existsSync(dappConfig)) { + console.error(`${dappConfig} ${__('already exists')}`.bold.red); + console.error(__('not overwritten, rename or re/move the file and re-run this command').yellow); + process.exit(1); + } else { + fs.copySync(embarkConfig, dappConfig); + console.log(`${embarkConfig}`.green); + console.log(__('copied to').dim.green); + console.log(`${dappConfig}`.green); + } + } + upload(options) { this.context = options.context || [constants.contexts.upload, constants.contexts.build]; From 3ad834d93d0b445c579eea27a1797c0902ea2e1c Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 18:30:19 -0500 Subject: [PATCH 19/64] rev comment to reflect code changes --- lib/pipeline/webpackProcess.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index 3bd700b91..4b56fff08 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -78,7 +78,7 @@ class WebpackProcess extends ProcessWrapper { } config = config[0]; } else { - // proceed with the value obtained from require(dapp/default/ConfigPath) + // proceed with the value obtained from require(configPath) } } catch (e) { console.error(`error while loading webpack config ${configPath}`); From b3c8e5fa45dde025376a01c168ce93cea0876666 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 18:33:19 -0500 Subject: [PATCH 20/64] missing spaces --- lib/pipeline/webpackProcess.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index 4b56fff08..cd837ee41 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -106,7 +106,7 @@ class WebpackProcess extends ProcessWrapper { fs.dappPath('.embark/stats.report'), stats.toString(config.stats) ); - this._log('info','writing file '+ ('.embark/stats.json').bold.dim); + this._log('info', 'writing file ' + ('.embark/stats.json').bold.dim); await writeFile( fs.dappPath('.embark/stats.json'), JSON.stringify(stats.toJson(config.stats)) From 46209bef65ecbdf5b5ad583dab4e4f924c8ea262 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 16 Aug 2018 19:51:56 -0500 Subject: [PATCH 21/64] rev comments --- webpack.config.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/webpack.config.js b/webpack.config.js index 95292fefd..05dcd9206 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -2,8 +2,8 @@ // some packages, plugins, and presets referenced/required in this webpack // config are deps of embark and will be transitive dapp deps unless specified -// in the dapp's own package.json (perhaps with a different version/range or -// other specifier) +// in the dapp's own package.json, perhaps with a different version/range or +// other specifier // there's a bug in pkg clone-deep re: regex; for now use lodash.clonedeep // see: https://github.com/jonschlinkert/clone-deep/pull/14 @@ -21,10 +21,11 @@ const embarkAssets = require(path.join(dappPath, '.embark/embark-assets.json')); // embark.json and changed/removed later, otherwise pkg resolution may behave // unexpectedly, e.g. a different version may get used owing to lexographical // order or because .embark/versions is still in place even though defaults are -// desired; once embark is always a lib within a dapp's node_modules tree, -// embark's pkg-version management functionality can probably be deprecated in -// favor of a dapp simply specifying dependencies in its own package.json, -// e.g. a different ipfs version than embark's default +// desired; once embark and its dependencies are always resolvable (in the +// usual node sense) relative to a dapp, embark's pkg version-management +// functionality can probably be deprecated in favor of a dapp simply +// specifying dependencies in its own package.json, e.g. a different ipfs +// version than embark's default let versions; try { versions = glob.sync(path.join(dappPath, '.embark/versions/*/*')); @@ -133,8 +134,8 @@ const development = cloneDeep(base); // chunk corresponds to a file being written to buildDir per embark.json; // currently embark is concatenating webpack chunks // dev.devtool = 'source-map'; -// alternative: development.mode = 'none' development.mode = 'development'; +// alternative: development.mode = 'none'; development.name = 'development'; const devBabelLoader = development.module.rules[3]; devBabelLoader.options.compact = false; From 16856eadf083aff577e49bc400aa3db523038616 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 12:32:58 -0500 Subject: [PATCH 22/64] rev notes --- webpack.config.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/webpack.config.js b/webpack.config.js index 05dcd9206..e3fa49050 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,10 +1,20 @@ /* global module process require */ -// some packages, plugins, and presets referenced/required in this webpack +// NOTE: some packages, plugins, and presets referenced/required in this webpack // config are deps of embark and will be transitive dapp deps unless specified // in the dapp's own package.json, perhaps with a different version/range or // other specifier +// NOTE: embark modifies process.env.NODE_PATH so that embark's own +// node_modules directory will be searched by node's require(). That allows an +// ejected version of this config, inside a dapp, to function correctly without +// embark being an explicit dependency of the dapp. However, webpack and babel +// do not directly support NODE_PATH, so in various parts of the config, +// modules such as plugins and presets must be resolved with +// require.resolve(). That is only necessary if a module is in embark's +// node_modules; if it's in the dapp's node_modules then it can be specified +// directly + // there's a bug in pkg clone-deep re: regex; for now use lodash.clonedeep // see: https://github.com/jonschlinkert/clone-deep/pull/14 const cloneDeep = require('lodash.clonedeep'); From 4acf0ad58d0c22144e19bb5c927411beb4ba9623 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 12:33:15 -0500 Subject: [PATCH 23/64] webpack compression plugin (activated in production config) --- webpack.config.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/webpack.config.js b/webpack.config.js index e3fa49050..1386eb2c5 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -18,6 +18,7 @@ // there's a bug in pkg clone-deep re: regex; for now use lodash.clonedeep // see: https://github.com/jonschlinkert/clone-deep/pull/14 const cloneDeep = require('lodash.clonedeep'); +const CompressionPlugin = require('compression-webpack-plugin'); const fs = require('fs'); const glob = require('glob'); const HardSourceWebpackPlugin = require('hard-source-webpack-plugin'); @@ -156,6 +157,7 @@ devBabelLoader.options.compact = false; const production = cloneDeep(base); production.mode = 'production'; production.name = 'production'; +production.plugins.push(new CompressionPlugin()); // export a list of named configs // ----------------------------------------------------------------------------- From d6aafa17bec8a20742fd34c6471e6d520d66faed Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 13:08:13 -0500 Subject: [PATCH 24/64] compression-webpack-plugin dep --- package-lock.json | 12 ++++++++++++ package.json | 1 + 2 files changed, 13 insertions(+) diff --git a/package-lock.json b/package-lock.json index a4c0c4666..bc9660a65 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2112,6 +2112,18 @@ "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.2.1.tgz", "integrity": "sha1-E3kY1teCg/ffemt8WmPhQOaUJeY=" }, + "compression-webpack-plugin": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/compression-webpack-plugin/-/compression-webpack-plugin-1.1.11.tgz", + "integrity": "sha512-ZVWKrTQhtOP7rDx3M/koXTnRm/iwcYbuCdV+i4lZfAIe32Mov7vUVM0+8Vpz4q0xH+TBUZxq+rM8nhtkDH50YQ==", + "requires": { + "cacache": "^10.0.1", + "find-cache-dir": "^1.0.0", + "neo-async": "^2.5.0", + "serialize-javascript": "^1.4.0", + "webpack-sources": "^1.0.1" + } + }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", diff --git a/package.json b/package.json index c0f2f5e1d..5f0d97f7d 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "clone-deep": "^4.0.0", "colors": "^1.1.2", "commander": "^2.15.1", + "compression-webpack-plugin": "^1.1.11", "css-loader": "^0.28.11", "decompress": "^4.2.0", "deep-equal": "^1.0.1", From 7123d2730a34deafec9dee69e596e25db3f2e1f0 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 12:34:20 -0500 Subject: [PATCH 25/64] rev webpack cfg such that when embark is global the cfg works from w/in a dapp (ejected) or w/in embark (default) --- webpack.config.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/webpack.config.js b/webpack.config.js index 1386eb2c5..596326c01 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -25,6 +25,8 @@ const HardSourceWebpackPlugin = require('hard-source-webpack-plugin'); const path = require('path'); const dappPath = process.env.DAPP_PATH; +const embarkPath = process.env.EMBARK_PATH; + const embarkAliases = require(path.join(dappPath, '.embark/embark-aliases.json')); const embarkAssets = require(path.join(dappPath, '.embark/embark-assets.json')); @@ -84,12 +86,12 @@ const base = { options: { plugins: [ [ - 'module-resolver', { + require.resolve('babel-plugin-module-resolver'), { 'alias': embarkAliases } ], [ - '@babel/plugin-transform-runtime', { + require.resolve('@babel/plugin-transform-runtime'), { corejs: 2, useESModules: true } @@ -97,14 +99,14 @@ const base = { ], presets: [ [ - '@babel/preset-env', { + require.resolve('@babel/preset-env'), { modules: false, targets: { browsers: ['last 1 version', 'not dead', '> 0.2%'] } } ], - '@babel/preset-react' + require.resolve('@babel/preset-react') ] } } @@ -131,8 +133,15 @@ const base = { alias: embarkAliases, modules: [ ...versions, - 'node_modules' + 'node_modules', + path.join(embarkPath, 'node_modules') ] + }, + resolveLoader: { + modules: [ + 'node_modules', + path.join(embarkPath, 'node_modules') + ], } }; From b7fd109237c52ba7bc44a4e85615d49cc19362d9 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 12:37:15 -0500 Subject: [PATCH 26/64] setup env vars w/in cmd.js --- cmd/cmd.js | 22 ++++++++++++++++++++++ cmd/cmd_controller.js | 5 ----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index c565adcf7..57b68e7c9 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -1,8 +1,30 @@ const program = require('commander'); const EmbarkController = require('./cmd_controller.js'); const i18n = require('../lib/core/i18n/i18n.js'); +const utils = require('../lib/utils/utils.js'); + let embark = new EmbarkController; +// set PWD to process.cwd() since Windows doesn't have a value for PWD +if (!process.env.PWD) { + process.env.PWD = process.cwd(); +} + +// set the anchor for embark's fs.dappPath() +if (!process.env.DAPP_PATH) { + process.env.DAPP_PATH = process.env.PWD; +} + +// set the anchor for embark's fs.embarkPath() +if (!process.env.EMBARK_PATH) { + process.env.EMBARK_PATH = utils.joinPath(__dirname, '..'); +} + +// enable lookup of embark's own node_modules from within dapp scripts +process.env.NODE_PATH = utils.joinPath(process.env.EMBARK_PATH, 'node_modules') + + (process.platform === 'win32' ? ';' : ':') + + (process.env.NODE_PATH || ''); + class Cmd { constructor() { program.version(embark.version); diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 6db6c8b7c..5865526a5 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -5,11 +5,6 @@ require('colors'); let version = require('../package.json').version; -// Set PWD to CWD since Windows doesn't have a value for PWD -if (!process.env.PWD) { - process.env.PWD = process.cwd(); -} - class EmbarkController { constructor(options) { From 019f0b68c81a6a36ad53c8991b4462fb8b23ef34 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 12:37:49 -0500 Subject: [PATCH 27/64] anchor fs.dappPath, fs.embarkPath to env vars setup in cmd.js --- lib/core/fs.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/core/fs.js b/lib/core/fs.js index ccfe40e19..cb52d5914 100644 --- a/lib/core/fs.js +++ b/lib/core/fs.js @@ -78,11 +78,23 @@ function removeSync() { } function embarkPath() { - return utils.joinPath(__dirname, '/../../', ...arguments); + const _embarkPath = process.env.EMBARK_PATH; + if (!_embarkPath) { + const errMsg = 'process.env.EMBARK_PATH was not set'.bold.red; + console.error(errMsg); + process.exit(1); + } + return utils.joinPath(_embarkPath, ...arguments); } function dappPath() { - return utils.joinPath(utils.pwd(), ...arguments); + const _dappPath = process.env.DAPP_PATH; + if (!_dappPath) { + const errMsg = 'process.env.DAPP_PATH was not set'.bold.red; + console.error(errMsg); + process.exit(1); + } + return utils.joinPath(_dappPath, ...arguments); } function createWriteStream() { From 40c14a08de8673077b65274a180292c35e8e040c Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 12:38:19 -0500 Subject: [PATCH 28/64] whitespace --- lib/core/config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/core/config.js b/lib/core/config.js index 3ca8fda60..13372037d 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -123,11 +123,11 @@ Config.prototype._updateBlockchainCors = function(){ } let cors = corsParts.join(','); - if(blockchainConfig.rpcCorsDomain === 'auto'){ + if(blockchainConfig.rpcCorsDomain === 'auto'){ if(cors.length) blockchainConfig.rpcCorsDomain = cors; else blockchainConfig.rpcCorsDomain = ''; } - if(blockchainConfig.wsOrigins === 'auto'){ + if(blockchainConfig.wsOrigins === 'auto'){ if(cors.length) blockchainConfig.wsOrigins = cors; else blockchainConfig.wsOrigins = ''; } From 8be1090a445c070399d82228055a9913fa6eea97 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 12:41:07 -0500 Subject: [PATCH 29/64] prefer joinPath to +'ing strings together --- lib/core/config.js | 2 +- lib/pipeline/pipeline.js | 21 +++++++++------------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/core/config.js b/lib/core/config.js index 13372037d..a43d4c5dc 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -169,7 +169,7 @@ Config.prototype._getFileOrOject = function(object, filePath, property) { if (typeof (this.configDir) === 'object') { return this.configDir[property]; } - return this.configDir + filePath; + return utils.joinPath(this.configDir, filePath); }; Config.prototype.loadBlockchainConfigFile = function() { diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index d830de593..2f95ba544 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -31,10 +31,7 @@ class Pipeline { function createPlaceholderPage(next){ self.events.request('embark-building-placeholder', (html) => { fs.mkdirpSync(self.buildDir); // create dist/ folder if not already exists - fs.writeFile(self.buildDir + 'index.html', html, next); - // ^ ?? should this use path.join instead? templates' embark.json - // files have a '/' hardcoded on the end of buildDir but that doesn't - // seem too resilient + fs.writeFile(utils.joinPath(self.buildDir, 'index.html'), html, next); }); }, function buildTheContracts(next) { @@ -117,7 +114,7 @@ class Pipeline { const isDir = targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1; // if it's not a directory if (!isDir) { - self.logger.info(__("writing file") + " " + (self.buildDir + targetFile).bold.dim); + self.logger.info(__("writing file") + " " + (utils.joinPath(self.buildDir, targetFile)).bold.dim); } async.map( files, @@ -160,8 +157,8 @@ class Pipeline { self.logger.error(__('errors found while generating') + ' ' + targetFile); } let dir = targetFile.split('/').slice(0, -1).join('/'); - self.logger.trace("creating dir " + self.buildDir + dir); - fs.mkdirpSync(self.buildDir + dir); + self.logger.trace("creating dir " + utils.joinPath(self.buildDir, dir)); + fs.mkdirpSync(utils.joinPath(self.buildDir, dir)); // if it's a directory if (isDir) { @@ -173,9 +170,9 @@ class Pipeline { async.each(contentFiles, function (file, mapCb) { let filename = file.filename.replace(file.basedir + '/', ''); - self.logger.info("writing file " + (self.buildDir + targetDir + filename).bold.dim); + self.logger.info("writing file " + (utils.joinPath(self.buildDir, targetDir, filename)).bold.dim); - fs.copy(file.path, self.buildDir + targetDir + filename, {overwrite: true}, mapCb); + fs.copy(file.path, utils.joinPath(self.buildDir, targetDir, filename), {overwrite: true}, mapCb); }, cb); return; } @@ -191,7 +188,7 @@ class Pipeline { targetFile = targetFile.replace('index', 'index-temp'); placeholderPage = targetFile; } - fs.writeFile(self.buildDir + targetFile, content, cb); + fs.writeFile(utils.joinPath(self.buildDir, targetFile), content, cb); } ); }, @@ -199,8 +196,8 @@ class Pipeline { ); }, function removePlaceholderPage(next){ - let placeholderFile = self.buildDir + placeholderPage; - fs.access(self.buildDir + placeholderPage, (err) => { + let placeholderFile = utils.joinPath(self.buildDir, placeholderPage); + fs.access(utils.joinPath(self.buildDir, placeholderPage), (err) => { if (err) return next(); // index-temp doesn't exist, do nothing // rename index-temp.htm/l to index.htm/l, effectively replacing our placeholder page From f0f95554a5d539c4e785f1253bb7d743c9c2821a Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 12:41:39 -0500 Subject: [PATCH 30/64] prefer fs.dappPath --- lib/core/plugins.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/core/plugins.js b/lib/core/plugins.js index ec8c35987..0aa5accef 100644 --- a/lib/core/plugins.js +++ b/lib/core/plugins.js @@ -1,6 +1,5 @@ const async = require('async'); var Plugin = require('./plugin.js'); -var utils = require('../utils/utils.js'); var fs = require('../core/fs.js'); var Plugins = function(options) { @@ -75,7 +74,7 @@ Plugins.prototype.loadInternalPlugin = function(pluginName, pluginConfig) { }; Plugins.prototype.loadPlugin = function(pluginName, pluginConfig) { - var pluginPath = utils.joinPath(utils.pwd(), 'node_modules', pluginName); + var pluginPath = fs.dappPath('node_modules', pluginName); var plugin = require(pluginPath); var pluginWrapper = new Plugin({ From 76502fa693199de26db31aac241b462ed48a9cf8 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 12:41:54 -0500 Subject: [PATCH 31/64] rm utils.pwd() -- redundant now that fs.dappPath is anchored to env var --- lib/utils/utils.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 21b2ec2e5..1f95b4edb 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -200,10 +200,6 @@ function proposeAlternative(word, _dictionary, _exceptions) { return propose(word, dictionary, {threshold: 0.3}); } -function pwd() { - return process.env.PWD || process.cwd(); -} - function getExternalContractUrl(file) { const constants = require('../constants'); let url; @@ -489,7 +485,6 @@ module.exports = { extractTar, extractZip, proposeAlternative, - pwd: pwd, getExternalContractUrl, toChecksumAddress, sha3, From 53a4fd2089371412b9d2557beb66298a932c2e36 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 12:42:50 -0500 Subject: [PATCH 32/64] rm redundant setup of env vars --- lib/core/processes/processWrapper.js | 5 ----- lib/pipeline/webpackProcess.js | 6 ------ 2 files changed, 11 deletions(-) diff --git a/lib/core/processes/processWrapper.js b/lib/core/processes/processWrapper.js index 3b934a106..aa5efdd9d 100644 --- a/lib/core/processes/processWrapper.js +++ b/lib/core/processes/processWrapper.js @@ -5,11 +5,6 @@ process.on('uncaughtException', function(e){ const constants = require('../../constants'); const Events = require('./eventsWrapper'); -// Set PWD to CWD since Windows doesn't have a value for PWD -if (!process.env.PWD) { - process.env.PWD = process.cwd(); -} - class ProcessWrapper { /** diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index cd837ee41..336f3c126 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -10,12 +10,6 @@ class WebpackProcess extends ProcessWrapper { constructor(options) { super(options); this.webpackConfigName = options.webpackConfigName; - if (!process.env.DAPP_PATH) { - process.env.DAPP_PATH = fs.dappPath(); - } - if (!process.env.EMBARK_PATH) { - process.env.EMBARK_PATH = fs.embarkPath(); - } } async build(assets, importsList, callback) { From 285534abeb7f7321bd0a720fcec6de915242045b Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 16:45:25 -0500 Subject: [PATCH 33/64] rev comment --- webpack.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webpack.config.js b/webpack.config.js index 596326c01..7e2d3aa3c 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -11,7 +11,7 @@ // embark being an explicit dependency of the dapp. However, webpack and babel // do not directly support NODE_PATH, so in various parts of the config, // modules such as plugins and presets must be resolved with -// require.resolve(). That is only necessary if a module is in embark's +// require.resolve(). That is only necessary if a plugin/preset is in embark's // node_modules; if it's in the dapp's node_modules then it can be specified // directly From bfeee5eb4d4d54eb1a234d5d741bb7a3709df1d2 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 16:45:35 -0500 Subject: [PATCH 34/64] load embark.json --- webpack.config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/webpack.config.js b/webpack.config.js index 7e2d3aa3c..a3b2a4c11 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -29,6 +29,7 @@ const embarkPath = process.env.EMBARK_PATH; const embarkAliases = require(path.join(dappPath, '.embark/embark-aliases.json')); const embarkAssets = require(path.join(dappPath, '.embark/embark-assets.json')); +const embarkJson = require(path.join(dappPath, 'embark.json')); // it's important to `embark reset` if a pkg version is specified in // embark.json and changed/removed later, otherwise pkg resolution may behave From 48e67973cf2f5b177f4fec5b78d52ad474c254c4 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 16:45:47 -0500 Subject: [PATCH 35/64] rev entry --- webpack.config.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/webpack.config.js b/webpack.config.js index a3b2a4c11..67663afdf 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -47,18 +47,19 @@ try { versions = []; } -const entry = Object.keys(embarkAssets).reduce((obj, key) => { - embarkAssets[key] - .filter(file => file.filename.match(/\.jsx?$/)) - .forEach(file => { - let file_path = file.path; - if (!file.path.match(/^\.\//)) { - file_path = './' + file_path; - } - obj[file.path] = file_path; - }); - return obj; -}, {}); +const entry = Object.keys(embarkAssets) + .filter(key => key.match(/\.js?$/)) + .reduce((obj, key) => { + obj[key] = embarkAssets[key] + .map(file => { + let file_path = file.path; + if (!file.path.match(/^\.\//)) { + file_path = './' + file_path; + } + return file_path; + }); + return obj; + }, {}); // base config // ----------------------------------------------------------------------------- From d994f7507a1be5cde0c404bed4c869bf7f295aa9 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 16:45:55 -0500 Subject: [PATCH 36/64] webpack writes to buildDir --- webpack.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webpack.config.js b/webpack.config.js index 67663afdf..8c7761dc1 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -120,7 +120,7 @@ const base = { // see: https://github.com/webpack/webpack/issues/6522#issuecomment-371120689 globalObject: 'typeof self !== \'undefined\' ? self : this', libraryTarget: 'umd', - path: path.join(dappPath, '.embark') + path: path.join(dappPath, embarkJson.buildDir) }, plugins: [ new HardSourceWebpackPlugin() From 198f7c37ac50a57d09bac6e8c6af860a8a029ffb Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 16:46:16 -0500 Subject: [PATCH 37/64] source maps gen'd by development config --- webpack.config.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/webpack.config.js b/webpack.config.js index 8c7761dc1..86eb70fba 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -152,10 +152,7 @@ const base = { const development = cloneDeep(base); // full source maps increase build time but are useful during dapp development -// HOWEVER: source maps are broken (completely? not sure) until a webpack -// chunk corresponds to a file being written to buildDir per embark.json; -// currently embark is concatenating webpack chunks -// dev.devtool = 'source-map'; +development.devtool = 'source-map'; development.mode = 'development'; // alternative: development.mode = 'none'; development.name = 'development'; From 2e965578756c95b7c97fc82dd3406e17955e30b8 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 16:46:36 -0500 Subject: [PATCH 38/64] rev comment --- webpack.config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/webpack.config.js b/webpack.config.js index 86eb70fba..27e408637 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -154,7 +154,8 @@ const development = cloneDeep(base); // full source maps increase build time but are useful during dapp development development.devtool = 'source-map'; development.mode = 'development'; -// alternative: development.mode = 'none'; +// alternatively: +// development.mode = 'none'; development.name = 'development'; const devBabelLoader = development.module.rules[3]; devBabelLoader.options.compact = false; From e1fb746aa7e41b786907ce3157029bf80e305f36 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 16:47:37 -0500 Subject: [PATCH 39/64] relocate webpack.config.js under lib/pipeline/ --- cmd/cmd_controller.js | 2 +- webpack.config.js => lib/pipeline/webpack.config.js | 0 lib/pipeline/webpackProcess.js | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename webpack.config.js => lib/pipeline/webpack.config.js (100%) diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index 5865526a5..c00a1fd0f 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -409,7 +409,7 @@ class EmbarkController { ejectWebpack() { var fs = require('../lib/core/fs.js'); var dappConfig = fs.dappPath('webpack.config.js'); - var embarkConfig = fs.embarkPath('webpack.config.js'); + var embarkConfig = fs.embarkPath('lib/pipeline', 'webpack.config.js'); if (fs.existsSync(dappConfig)) { console.error(`${dappConfig} ${__('already exists')}`.bold.red); console.error(__('not overwritten, rename or re/move the file and re-run this command').yellow); diff --git a/webpack.config.js b/lib/pipeline/webpack.config.js similarity index 100% rename from webpack.config.js rename to lib/pipeline/webpack.config.js diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index 336f3c126..d102567fd 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -39,7 +39,7 @@ class WebpackProcess extends ProcessWrapper { } const dappConfigPath = fs.dappPath('webpack.config.js'); - const defaultConfigPath = fs.embarkPath('webpack.config.js'); + const defaultConfigPath = fs.embarkPath('lib/pipeline', 'webpack.config.js'); let config, configPath; try { From 0161a9b40277b5025687d636a158f15b056e5287 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 16:48:04 -0500 Subject: [PATCH 40/64] "writing file" messages for files that will be written by webpack --- lib/pipeline/pipeline.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 2f95ba544..7ed665c40 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -84,6 +84,11 @@ class Pipeline { }, function runWebpack(next) { self.logger.info(__(`running webpack with '${self.webpackConfigName}' config...`)); + Object.keys(self.assetFiles) + .filter(key => key.match(/\.js?$/)) + .forEach(key => { + self.logger.info(__("writing file") + " " + (utils.joinPath(self.buildDir, key)).bold.dim); + }); let built = false; const webpackProcess = new ProcessLauncher({ modulePath: utils.joinPath(__dirname, 'webpackProcess.js'), From 7fb9c552dc1817f21a66d2c866a9d1c4b448f1db Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 16:48:43 -0500 Subject: [PATCH 41/64] assetFileWrite no longer processes files corresponding to .js keys --- lib/pipeline/pipeline.js | 40 +++++++++------------------------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 7ed665c40..bd2babc2f 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -114,7 +114,13 @@ class Pipeline { }, function assetFileWrite(next) { async.eachOf( - self.assetFiles, + // assetFileWrite should not process .js files + Object.keys(self.assetFiles) + .filter(key => !key.match(/\.js?$/)) + .reduce((obj, key) => { + obj[key] = self.assetFiles[key]; + return obj; + }, {}), function (files, targetFile, cb) { const isDir = targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1; // if it's not a directory @@ -125,36 +131,8 @@ class Pipeline { files, function (file, fileCb) { self.logger.trace("reading " + file.filename); - - // Not a JS file - if (file.filename.indexOf('.js') < 0) { - return file.content(function (fileContent) { - self.runPlugins(file, fileContent, fileCb); - }); - } - - // JS files - async.waterfall([ - function readFile(next) { - fs.readFile('./.embark/' + file.filename, (err, data) => { - if (err) { - return next(err); - } - next(null, data.toString()); - }); - }, - - function runPluginsOnContent(fileContent, next) { - self.runPlugins(file, fileContent, next); - } - - ], function (err, contentFile) { - if (err) { - self.logger.error(err.message || err); - return fileCb(err); - } - - fileCb(null, contentFile); + return file.content(function (fileContent) { + self.runPlugins(file, fileContent, fileCb); }); }, function (err, contentFiles) { From 67147f2cadc11578e5565c93fdd06557a331ccfd Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 17:14:28 -0500 Subject: [PATCH 42/64] make the linter happy --- lib/pipeline/webpack.config.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/pipeline/webpack.config.js b/lib/pipeline/webpack.config.js index 27e408637..11f81aff8 100644 --- a/lib/pipeline/webpack.config.js +++ b/lib/pipeline/webpack.config.js @@ -19,7 +19,6 @@ // see: https://github.com/jonschlinkert/clone-deep/pull/14 const cloneDeep = require('lodash.clonedeep'); const CompressionPlugin = require('compression-webpack-plugin'); -const fs = require('fs'); const glob = require('glob'); const HardSourceWebpackPlugin = require('hard-source-webpack-plugin'); const path = require('path'); @@ -122,9 +121,7 @@ const base = { libraryTarget: 'umd', path: path.join(dappPath, embarkJson.buildDir) }, - plugins: [ - new HardSourceWebpackPlugin() - ], + plugins: [new HardSourceWebpackPlugin()], // profiling and generating verbose stats increases build time; if stats // are generated embark will write the output to: // path.join(dappPath, '.embark/stats.[json,report]') @@ -143,7 +140,7 @@ const base = { modules: [ 'node_modules', path.join(embarkPath, 'node_modules') - ], + ] } }; From f1edc28f6dfeda56795673f4087fd7a90280c376 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 17:55:39 -0500 Subject: [PATCH 43/64] anchoredPath helper (more DRY) --- lib/core/fs.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/core/fs.js b/lib/core/fs.js index cb52d5914..5e340e966 100644 --- a/lib/core/fs.js +++ b/lib/core/fs.js @@ -77,24 +77,21 @@ function removeSync() { return fs.removeSync.apply(fs.removeSync, arguments); } -function embarkPath() { - const _embarkPath = process.env.EMBARK_PATH; - if (!_embarkPath) { - const errMsg = 'process.env.EMBARK_PATH was not set'.bold.red; - console.error(errMsg); +function anchoredPath(envAnchor, ...args) { + const anchor = process.env[envAnchor]; + if (!anchor) { + console.error(`process.env.${envAnchor} was not set`.bold.red); process.exit(1); } - return utils.joinPath(_embarkPath, ...arguments); + return utils.joinPath(anchor, ...args); +} + +function embarkPath() { + return anchoredPath('EMBARK_PATH', ...arguments); } function dappPath() { - const _dappPath = process.env.DAPP_PATH; - if (!_dappPath) { - const errMsg = 'process.env.DAPP_PATH was not set'.bold.red; - console.error(errMsg); - process.exit(1); - } - return utils.joinPath(_dappPath, ...arguments); + return anchoredPath('DAPP_PATH', ...arguments); } function createWriteStream() { From 421927d81594c4adfc7755dd5f8021fbc77011d6 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 17:55:59 -0500 Subject: [PATCH 44/64] paths more DRY --- lib/pipeline/webpack.config.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/pipeline/webpack.config.js b/lib/pipeline/webpack.config.js index 11f81aff8..f024c4664 100644 --- a/lib/pipeline/webpack.config.js +++ b/lib/pipeline/webpack.config.js @@ -28,8 +28,11 @@ const embarkPath = process.env.EMBARK_PATH; const embarkAliases = require(path.join(dappPath, '.embark/embark-aliases.json')); const embarkAssets = require(path.join(dappPath, '.embark/embark-assets.json')); +const embarkNodeModules = path.join(embarkPath, 'node_modules'); const embarkJson = require(path.join(dappPath, 'embark.json')); +const buildDir = path.join(dappPath, embarkJson.buildDir); + // it's important to `embark reset` if a pkg version is specified in // embark.json and changed/removed later, otherwise pkg resolution may behave // unexpectedly, e.g. a different version may get used owing to lexographical @@ -119,7 +122,7 @@ const base = { // see: https://github.com/webpack/webpack/issues/6522#issuecomment-371120689 globalObject: 'typeof self !== \'undefined\' ? self : this', libraryTarget: 'umd', - path: path.join(dappPath, embarkJson.buildDir) + path: buildDir }, plugins: [new HardSourceWebpackPlugin()], // profiling and generating verbose stats increases build time; if stats @@ -133,13 +136,13 @@ const base = { modules: [ ...versions, 'node_modules', - path.join(embarkPath, 'node_modules') + embarkNodeModules ] }, resolveLoader: { modules: [ 'node_modules', - path.join(embarkPath, 'node_modules') + embarkNodeModules ] } }; From d4f8dd66089d56cafb112cd7dcbdf288810e0d0d Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 17 Aug 2018 18:02:13 -0500 Subject: [PATCH 45/64] rev comment re: NODE_PATH --- cmd/cmd.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index 57b68e7c9..798886fac 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -20,7 +20,9 @@ if (!process.env.EMBARK_PATH) { process.env.EMBARK_PATH = utils.joinPath(__dirname, '..'); } -// enable lookup of embark's own node_modules from within dapp scripts +// enable lookup of *global* embark's own node_modules from within dapp scripts +// this makes embark's dependencies trasitive dependencies of a dapp without the +// dapp explicitly specifying embark as a dependency in the dapp's package.json process.env.NODE_PATH = utils.joinPath(process.env.EMBARK_PATH, 'node_modules') + (process.platform === 'win32' ? ';' : ':') + (process.env.NODE_PATH || ''); From ba98f76a10b8fbfe5a1c73e2c45f2257f734cf27 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Sat, 18 Aug 2018 18:15:07 -0500 Subject: [PATCH 46/64] no separator if proces.env.NODE_PATH is falsy --- cmd/cmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index 798886fac..68ae7a9fa 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -24,7 +24,7 @@ if (!process.env.EMBARK_PATH) { // this makes embark's dependencies trasitive dependencies of a dapp without the // dapp explicitly specifying embark as a dependency in the dapp's package.json process.env.NODE_PATH = utils.joinPath(process.env.EMBARK_PATH, 'node_modules') - + (process.platform === 'win32' ? ';' : ':') + + (process.env.NODE_PATH ? (process.platform === 'win32' ? ';' : ':') : '') + (process.env.NODE_PATH || ''); class Cmd { From 792a8f57ebb4526d4693f790b4d3b5d9634b98f9 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Sat, 18 Aug 2018 18:16:03 -0500 Subject: [PATCH 47/64] consistent handling of error messages, no double reporting --- lib/pipeline/webpackProcess.js | 43 ++++++++++++++-------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index d102567fd..847c278fc 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -6,6 +6,16 @@ const writeFile = require('util').promisify(require('fs').writeFile); let webpackProcess; +function errorMessage(e) { + if (typeof e === 'string') { + return e; + } else if (e && e.message) { + return e.message; + } else { + return e; + } +} + class WebpackProcess extends ProcessWrapper { constructor(options) { super(options); @@ -16,9 +26,7 @@ class WebpackProcess extends ProcessWrapper { try { await this.webpackRun(assets, importsList, callback); } catch (e) { - console.error(e.message); - callback(e); - // ?? should return e or e.message + callback(errorMessage(e)); } } @@ -33,9 +41,7 @@ class WebpackProcess extends ProcessWrapper { JSON.stringify(assets) ); } catch (e) { - console.error(e.message); - return callback(e); - // ?? should return e or e.message + return callback(errorMessage(e)); } const dappConfigPath = fs.dappPath('webpack.config.js'); @@ -60,15 +66,10 @@ class WebpackProcess extends ProcessWrapper { } else if (Array.isArray(config)) { config = config.filter(cfg => cfg.name === this.webpackConfigName); if (!config.length) { - const errMsg = `no webpack config has the name '${this.webpackConfigName}'`; - console.error(errMsg); - return callback(errMsg); - // ?? should the message be wrapped in new Error() + return callback(`no webpack config has the name '${this.webpackConfigName}'`); } if (config.length > 1) { - console.warn( - `detected ${config.length} webpack configs having the name '${this.webpackConfigName}', using the first one` - ); + console.warn(`detected ${config.length} webpack configs having the name '${this.webpackConfigName}', using the first one`); } config = config[0]; } else { @@ -76,22 +77,16 @@ class WebpackProcess extends ProcessWrapper { } } catch (e) { console.error(`error while loading webpack config ${configPath}`); - console.error(e.message); - callback(e); - // ?? should return e or e.message + callback(errorMessage(e)); } if (typeof config !== 'object' || config === null) { - const errMsg = 'bad webpack config, the resolved config was null or not an object'; - console.error(errMsg); - return callback(errMsg); - // ?? should the message be wrapped in new Error() + return callback('bad webpack config, the resolved config was null or not an object'); } webpack(config).run(async (err, stats) => { if (err) { - console.error(err); - return callback(err); + return callback(errorMessage(err)); } try { if (config.stats && config.stats !== 'none') { @@ -107,12 +102,10 @@ class WebpackProcess extends ProcessWrapper { ); } } catch (e) { - console.error(e.message); - return callback(err); + return callback(errorMessage(e)); } if (config.stats && stats.hasErrors()) { const errors = stats.toJson(config.stats).errors.join('\n'); - console.error(errors); return callback(errors); } callback(); From 2c54349c339cd0aa557c3bc832e8755d5131f09d Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Sat, 18 Aug 2018 19:36:50 -0500 Subject: [PATCH 48/64] rev comment re: NODE_PATH --- cmd/cmd.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index 68ae7a9fa..e62d495e5 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -20,9 +20,12 @@ if (!process.env.EMBARK_PATH) { process.env.EMBARK_PATH = utils.joinPath(__dirname, '..'); } -// enable lookup of *global* embark's own node_modules from within dapp scripts -// this makes embark's dependencies trasitive dependencies of a dapp without the -// dapp explicitly specifying embark as a dependency in the dapp's package.json +// NOTE: setting NODE_PATH at runtime won't effect lookup behavior in the +// current process, but will take effect in child processes; this enables +// lookup of *global* embark's own node_modules from within dapp scripts (such +// as an ejected webpack.config.js), making embark's dependencies trasitive +// dependencies of a dapp without the dapp explicitly specifying embark as a +// dependency in the dapp's package.json process.env.NODE_PATH = utils.joinPath(process.env.EMBARK_PATH, 'node_modules') + (process.env.NODE_PATH ? (process.platform === 'win32' ? ';' : ':') : '') + (process.env.NODE_PATH || ''); From cec7dc57507bd8c965c20f5af5c9cfe442acc3aa Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Sat, 18 Aug 2018 19:55:34 -0500 Subject: [PATCH 49/64] DRY resolve --- lib/pipeline/webpack.config.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/pipeline/webpack.config.js b/lib/pipeline/webpack.config.js index f024c4664..4096b0c2c 100644 --- a/lib/pipeline/webpack.config.js +++ b/lib/pipeline/webpack.config.js @@ -63,6 +63,16 @@ const entry = Object.keys(embarkAssets) return obj; }, {}); +function resolve(pkgName) { + if (Array.isArray(pkgName)) { + const _pkgName = pkgName[0]; + pkgName[0] = require.resolve(_pkgName); + return pkgName; + } else { + return require.resolve(pkgName); + } +} + // base config // ----------------------------------------------------------------------------- @@ -90,28 +100,28 @@ const base = { options: { plugins: [ [ - require.resolve('babel-plugin-module-resolver'), { + 'babel-plugin-module-resolver', { 'alias': embarkAliases } ], [ - require.resolve('@babel/plugin-transform-runtime'), { + '@babel/plugin-transform-runtime', { corejs: 2, useESModules: true } ] - ], + ].map(resolve), presets: [ [ - require.resolve('@babel/preset-env'), { + '@babel/preset-env', { modules: false, targets: { browsers: ['last 1 version', 'not dead', '> 0.2%'] } } ], - require.resolve('@babel/preset-react') - ] + '@babel/preset-react' + ].map(resolve) } } ] From e0fda460bac03c9236e6e47e2eace1549024577b Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Sat, 18 Aug 2018 20:04:16 -0500 Subject: [PATCH 50/64] --pipeline option for `embark console`, defaults to 'development' --- cmd/cmd.js | 4 +++- cmd/cmd_controller.js | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index e62d495e5..8b7314352 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -179,6 +179,7 @@ class Cmd { .option('--logfile [logfile]', __('filename to output logs (default: %s)', 'none')) .option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug') .option('--locale [locale]', __('language to use (default: en)')) + .option('--pipeline [pipeline]', __('webpack config to use (default: development)')) .description(__('Start the Embark console')) .action(function (env, options) { i18n.setOrDetectLocale(options.locale); @@ -187,7 +188,8 @@ class Cmd { client: options.client || 'geth', locale: options.locale, logFile: options.logfile, - logLevel: options.loglevel + logLevel: options.loglevel, + webpackConfigName: options.pipeline || 'development' }); }); } diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index c00a1fd0f..ab90a3fdb 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -256,7 +256,8 @@ class EmbarkController { logFile: options.logFile, logLevel: options.logLevel, context: this.context, - ipcRole: 'client' + ipcRole: 'client', + webpackConfigName: options.webpackConfigName }); engine.init(); async.waterfall([ From 46c4aea5207bebf4de867d2823d68af59d5b8fd3 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Sat, 18 Aug 2018 22:14:43 -0500 Subject: [PATCH 51/64] rev comment re: NODE_PATH in webpack config --- lib/pipeline/webpack.config.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/pipeline/webpack.config.js b/lib/pipeline/webpack.config.js index 4096b0c2c..4737824a8 100644 --- a/lib/pipeline/webpack.config.js +++ b/lib/pipeline/webpack.config.js @@ -5,15 +5,15 @@ // in the dapp's own package.json, perhaps with a different version/range or // other specifier -// NOTE: embark modifies process.env.NODE_PATH so that embark's own -// node_modules directory will be searched by node's require(). That allows an -// ejected version of this config, inside a dapp, to function correctly without -// embark being an explicit dependency of the dapp. However, webpack and babel -// do not directly support NODE_PATH, so in various parts of the config, -// modules such as plugins and presets must be resolved with -// require.resolve(). That is only necessary if a plugin/preset is in embark's -// node_modules; if it's in the dapp's node_modules then it can be specified -// directly +// NOTE: embark modifies process.env.NODE_PATH so that when running dapp +// scripts in embark's child processes, embark's own node_modules directory +// will be searched by node's require(); that allows an ejected version of this +// config, inside a dapp, to function correctly without embark being an +// explicit dependency of the dapp; however, webpack and babel do not directly +// support NODE_PATH, so in various parts of the config, modules such as babel +// plugins and presets must be resolved with require.resolve(); that is only +// necessary if a plugin/preset is in embark's node_modules vs. the dapp's +// node_modules // there's a bug in pkg clone-deep re: regex; for now use lodash.clonedeep // see: https://github.com/jonschlinkert/clone-deep/pull/14 From c8cd5f42290021172615d5811f57b04847fb4ae2 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 20 Aug 2018 10:28:43 -0500 Subject: [PATCH 52/64] consistent indentation --- lib/pipeline/webpack.config.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/pipeline/webpack.config.js b/lib/pipeline/webpack.config.js index 4737824a8..e8c571372 100644 --- a/lib/pipeline/webpack.config.js +++ b/lib/pipeline/webpack.config.js @@ -50,18 +50,18 @@ try { } const entry = Object.keys(embarkAssets) - .filter(key => key.match(/\.js?$/)) - .reduce((obj, key) => { - obj[key] = embarkAssets[key] - .map(file => { - let file_path = file.path; - if (!file.path.match(/^\.\//)) { - file_path = './' + file_path; - } - return file_path; - }); - return obj; - }, {}); + .filter(key => key.match(/\.js?$/)) + .reduce((obj, key) => { + obj[key] = embarkAssets[key] + .map(file => { + let file_path = file.path; + if (!file.path.match(/^\.\//)) { + file_path = './' + file_path; + } + return file_path; + }); + return obj; + }, {}); function resolve(pkgName) { if (Array.isArray(pkgName)) { From f66552b044093f1c90d497d8271d3259b403d34f Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 20 Aug 2018 10:36:29 -0500 Subject: [PATCH 53/64] errorMessage helper moved to utils --- lib/pipeline/webpackProcess.js | 11 +---------- lib/utils/utils.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index 847c278fc..c701a8572 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -3,19 +3,10 @@ const fs = require('../core/fs'); const ProcessWrapper = require('../core/processes/processWrapper'); const webpack = require('webpack'); const writeFile = require('util').promisify(require('fs').writeFile); +const {errorMessage} = require('../utils/utils'); let webpackProcess; -function errorMessage(e) { - if (typeof e === 'string') { - return e; - } else if (e && e.message) { - return e.message; - } else { - return e; - } -} - class WebpackProcess extends ProcessWrapper { constructor(options) { super(options); diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 1f95b4edb..764237e2f 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -458,6 +458,16 @@ function interceptLogs(consoleContext, logger) { }; } +function errorMessage(e) { + if (typeof e === 'string') { + return e; + } else if (e && e.message) { + return e.message; + } else { + return e; + } +} + module.exports = { joinPath, dirname, @@ -499,5 +509,6 @@ module.exports = { groupBy, sample, last, - interceptLogs + interceptLogs, + errorMessage }; From d14ee86c3259ea725e1023444f541488db0b9179 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 20 Aug 2018 11:02:38 -0500 Subject: [PATCH 54/64] if dapp has a webpack.config.js, make a backup copy and proceed --- cmd/cmd_controller.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index ab90a3fdb..b9627a64a 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -411,16 +411,22 @@ class EmbarkController { var fs = require('../lib/core/fs.js'); var dappConfig = fs.dappPath('webpack.config.js'); var embarkConfig = fs.embarkPath('lib/pipeline', 'webpack.config.js'); - if (fs.existsSync(dappConfig)) { - console.error(`${dappConfig} ${__('already exists')}`.bold.red); - console.error(__('not overwritten, rename or re/move the file and re-run this command').yellow); - process.exit(1); - } else { - fs.copySync(embarkConfig, dappConfig); - console.log(`${embarkConfig}`.green); - console.log(__('copied to').dim.green); - console.log(`${dappConfig}`.green); + let ext = 1; + let dappConfigOld = dappConfig; + while (fs.existsSync(dappConfigOld)) { + dappConfigOld = dappConfig + `.${ext}`; + ext++; } + if (dappConfigOld !== dappConfig) { + fs.copySync(dappConfig, dappConfigOld); + console.warn(`${dappConfig}`.yellow); + console.warn(__('copied to').dim.yellow); + console.warn(`${dappConfigOld}\n`.yellow); + } + fs.copySync(embarkConfig, dappConfig); + console.log(`${embarkConfig}`.green); + console.log(__('copied to').dim.green); + console.log(`${dappConfig}`.green); } upload(options) { From 1e97a6478b446cc82058f08a3dcd671b5aabdaab Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 20 Aug 2018 13:36:02 -0500 Subject: [PATCH 55/64] prefer path.delimiter --- cmd/cmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index 8b7314352..dc87eb5a4 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -27,7 +27,7 @@ if (!process.env.EMBARK_PATH) { // dependencies of a dapp without the dapp explicitly specifying embark as a // dependency in the dapp's package.json process.env.NODE_PATH = utils.joinPath(process.env.EMBARK_PATH, 'node_modules') - + (process.env.NODE_PATH ? (process.platform === 'win32' ? ';' : ':') : '') + + (process.env.NODE_PATH ? path.delimiter : '') + (process.env.NODE_PATH || ''); class Cmd { From 04a5e3335d34a54c1f2b5e975d8480edd2ce707b Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 20 Aug 2018 13:38:04 -0500 Subject: [PATCH 56/64] self.logger -> self.logger.error --- lib/pipeline/pipeline.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index bd2babc2f..577537eef 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -99,7 +99,7 @@ class Pipeline { return next(`Webpack build exited with code ${code} before the process finished`); } if (code) { - self.logger(__('Webpack build process exited with code ', code)); + self.logger.error(__('Webpack build process exited with code ', code)); } } }); From 382122c6d984543e74a2396eb7b1ad252b414880 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 20 Aug 2018 13:39:45 -0500 Subject: [PATCH 57/64] mapCb -> eachCb --- lib/pipeline/pipeline.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 577537eef..361aba0f5 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -151,11 +151,11 @@ class Pipeline { targetDir = targetDir + '/'; } - async.each(contentFiles, function (file, mapCb) { + async.each(contentFiles, function (file, eachCb) { let filename = file.filename.replace(file.basedir + '/', ''); self.logger.info("writing file " + (utils.joinPath(self.buildDir, targetDir, filename)).bold.dim); - fs.copy(file.path, utils.joinPath(self.buildDir, targetDir, filename), {overwrite: true}, mapCb); + fs.copy(file.path, utils.joinPath(self.buildDir, targetDir, filename), {overwrite: true}, eachCb); }, cb); return; } From f4b11ce29f21c79a72bb219a64b8562113a627c5 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 20 Aug 2018 13:52:48 -0500 Subject: [PATCH 58/64] explanatory comment --- lib/pipeline/webpack.config.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/pipeline/webpack.config.js b/lib/pipeline/webpack.config.js index e8c571372..6bb955245 100644 --- a/lib/pipeline/webpack.config.js +++ b/lib/pipeline/webpack.config.js @@ -52,6 +52,10 @@ try { const entry = Object.keys(embarkAssets) .filter(key => key.match(/\.js?$/)) .reduce((obj, key) => { + // webpack entry paths should start with './' if they're relative to the + // webpack context; embark.json "app" keys correspond to lists of .js + // source paths relative to the top-level dapp dir and may be missing the + // leading './' obj[key] = embarkAssets[key] .map(file => { let file_path = file.path; From 8bec33177eb6685b0741f60ce0e0859d99bc23a3 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 20 Aug 2018 13:57:04 -0500 Subject: [PATCH 59/64] rm some else clauses --- lib/pipeline/webpack.config.js | 3 +-- lib/pipeline/webpackProcess.js | 2 -- lib/utils/utils.js | 3 +-- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/pipeline/webpack.config.js b/lib/pipeline/webpack.config.js index 6bb955245..a243306d9 100644 --- a/lib/pipeline/webpack.config.js +++ b/lib/pipeline/webpack.config.js @@ -72,9 +72,8 @@ function resolve(pkgName) { const _pkgName = pkgName[0]; pkgName[0] = require.resolve(_pkgName); return pkgName; - } else { - return require.resolve(pkgName); } + return require.resolve(pkgName); } // base config diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index c701a8572..1010e36e4 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -63,8 +63,6 @@ class WebpackProcess extends ProcessWrapper { console.warn(`detected ${config.length} webpack configs having the name '${this.webpackConfigName}', using the first one`); } config = config[0]; - } else { - // proceed with the value obtained from require(configPath) } } catch (e) { console.error(`error while loading webpack config ${configPath}`); diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 764237e2f..808a45fea 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -463,9 +463,8 @@ function errorMessage(e) { return e; } else if (e && e.message) { return e.message; - } else { - return e; } + return e; } module.exports = { From 9e62d9c1a94f4d2adbc6fa1de54e56855ce0873a Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 20 Aug 2018 14:07:53 -0500 Subject: [PATCH 60/64] isDir test should support possible use of '\' by devs on Windows --- lib/pipeline/pipeline.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 361aba0f5..1f0a742be 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -122,7 +122,7 @@ class Pipeline { return obj; }, {}), function (files, targetFile, cb) { - const isDir = targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1; + const isDir = targetFile.slice(-1) === '/' || targetFile.slice(-1) === '\\' || targetFile.indexOf('.') === -1; // if it's not a directory if (!isDir) { self.logger.info(__("writing file") + " " + (utils.joinPath(self.buildDir, targetFile)).bold.dim); From 65d2153638c15eb59976b1985eaf04d554ddb187 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 20 Aug 2018 14:14:26 -0500 Subject: [PATCH 61/64] prefer early return for config.stats tests --- lib/pipeline/webpackProcess.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/pipeline/webpackProcess.js b/lib/pipeline/webpackProcess.js index 1010e36e4..8e75cdc17 100644 --- a/lib/pipeline/webpackProcess.js +++ b/lib/pipeline/webpackProcess.js @@ -77,27 +77,28 @@ class WebpackProcess extends ProcessWrapper { if (err) { return callback(errorMessage(err)); } + if (!config.stats || config.stats === 'none') { + return callback(); + } try { - if (config.stats && config.stats !== 'none') { - this._log('info', 'writing file '+ ('.embark/stats.report').bold.dim); - await writeFile( - fs.dappPath('.embark/stats.report'), - stats.toString(config.stats) - ); - this._log('info', 'writing file ' + ('.embark/stats.json').bold.dim); - await writeFile( - fs.dappPath('.embark/stats.json'), - JSON.stringify(stats.toJson(config.stats)) - ); + this._log('info', 'writing file '+ ('.embark/stats.report').bold.dim); + await writeFile( + fs.dappPath('.embark/stats.report'), + stats.toString(config.stats) + ); + this._log('info', 'writing file ' + ('.embark/stats.json').bold.dim); + await writeFile( + fs.dappPath('.embark/stats.json'), + JSON.stringify(stats.toJson(config.stats)) + ); + if (stats.hasErrors()) { + const errors = stats.toJson(config.stats).errors.join('\n'); + return callback(errors); } + callback(); } catch (e) { return callback(errorMessage(e)); } - if (config.stats && stats.hasErrors()) { - const errors = stats.toJson(config.stats).errors.join('\n'); - return callback(errors); - } - callback(); }); } } From 6149921562a079e0ff400a79ab1e3dc347e2b439 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 20 Aug 2018 14:19:26 -0500 Subject: [PATCH 62/64] rev comments --- lib/pipeline/webpack.config.js | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/lib/pipeline/webpack.config.js b/lib/pipeline/webpack.config.js index a243306d9..89186029e 100644 --- a/lib/pipeline/webpack.config.js +++ b/lib/pipeline/webpack.config.js @@ -1,22 +1,14 @@ -/* global module process require */ - -// NOTE: some packages, plugins, and presets referenced/required in this webpack +// some packages, plugins, and presets referenced/required in this webpack // config are deps of embark and will be transitive dapp deps unless specified -// in the dapp's own package.json, perhaps with a different version/range or -// other specifier +// in the dapp's own package.json -// NOTE: embark modifies process.env.NODE_PATH so that when running dapp -// scripts in embark's child processes, embark's own node_modules directory -// will be searched by node's require(); that allows an ejected version of this -// config, inside a dapp, to function correctly without embark being an -// explicit dependency of the dapp; however, webpack and babel do not directly -// support NODE_PATH, so in various parts of the config, modules such as babel -// plugins and presets must be resolved with require.resolve(); that is only -// necessary if a plugin/preset is in embark's node_modules vs. the dapp's -// node_modules +// embark modifies process.env.NODE_PATH so that when running dapp scripts in +// embark's child processes, embark's own node_modules directory will be +// searched by node's require(); however, webpack and babel do not directly +// support NODE_PATH, so modules such as babel plugins and presets must be +// resolved with require.resolve(); that is only necessary if a plugin/preset +// is in embark's node_modules vs. the dapp's node_modules -// there's a bug in pkg clone-deep re: regex; for now use lodash.clonedeep -// see: https://github.com/jonschlinkert/clone-deep/pull/14 const cloneDeep = require('lodash.clonedeep'); const CompressionPlugin = require('compression-webpack-plugin'); const glob = require('glob'); @@ -35,13 +27,7 @@ const buildDir = path.join(dappPath, embarkJson.buildDir); // it's important to `embark reset` if a pkg version is specified in // embark.json and changed/removed later, otherwise pkg resolution may behave -// unexpectedly, e.g. a different version may get used owing to lexographical -// order or because .embark/versions is still in place even though defaults are -// desired; once embark and its dependencies are always resolvable (in the -// usual node sense) relative to a dapp, embark's pkg version-management -// functionality can probably be deprecated in favor of a dapp simply -// specifying dependencies in its own package.json, e.g. a different ipfs -// version than embark's default +// unexpectedly let versions; try { versions = glob.sync(path.join(dappPath, '.embark/versions/*/*')); From 42343246405f4072ca43e94038872954d4ffdd5b Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 20 Aug 2018 16:17:21 -0500 Subject: [PATCH 63/64] need to require('path') --- cmd/cmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index dc87eb5a4..a154cea63 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -27,7 +27,7 @@ if (!process.env.EMBARK_PATH) { // dependencies of a dapp without the dapp explicitly specifying embark as a // dependency in the dapp's package.json process.env.NODE_PATH = utils.joinPath(process.env.EMBARK_PATH, 'node_modules') - + (process.env.NODE_PATH ? path.delimiter : '') + + (process.env.NODE_PATH ? require('path').delimiter : '') + (process.env.NODE_PATH || ''); class Cmd { From 8fa5bddde56eebba0082880f4b39155ea0731a7c Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Wed, 22 Aug 2018 18:25:46 -0500 Subject: [PATCH 64/64] rev comment re: webpack-bundle-analyzer --- lib/pipeline/webpack.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pipeline/webpack.config.js b/lib/pipeline/webpack.config.js index 89186029e..db190f496 100644 --- a/lib/pipeline/webpack.config.js +++ b/lib/pipeline/webpack.config.js @@ -128,7 +128,7 @@ const base = { // are generated embark will write the output to: // path.join(dappPath, '.embark/stats.[json,report]') // to visualize the stats info in a browser run: - // npx webpack-bundle-analyzer /.embark/stats.json + // npx webpack-bundle-analyzer .embark/stats.json profile: true, stats: 'verbose', resolve: { alias: embarkAliases,