Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Move and optimize build process from ganache-cli to ganache-core (#…
Browse files Browse the repository at this point in the history
…162)

This pull request copies the build process from `ganache-cli` to `ganache-core` while also optimizing the resulting webpacked bundle to only include `ganache-core`'s internals plus pure-js implementations of our native transitive dependencies. All other dependencies are installed on `install`.

Additionally, ganache-core and ganache-cli will now default to using native dependencies, if successfully `install`ed, only falling back to the pure-js implementations if those dependencies can't be installed (i.e., because node-gyp on Windows is tricky).

---

This PR also purges some unnecessary `dependencies` from package.json, moves others to `devDependencies`, and updates and pins the remaining direct dependencies to their latest patch versions (at time of writing), with the exception of:

- `level-sublevel`; as version `6.6.5` needlessly regressed to a dependency on a vulnerable version of `levelup`. `6.6.5` actually looks like it was accidentally published from an older branch named `master2`. `¯\_(ツ)_/¯`
- `ethereumjs-tx`; as version [`1.3.5`](https://github.com/ethereumjs/ethereumjs-tx/releases/tag/v1.3.5) introduced bug ethereumjs/ethereumjs-tx#113 which was not fixed in [`1.3.7`](https://github.com/ethereumjs/ethereumjs-tx/releases/tag/v1.3.7) in my testing.

---

On `npm publish` our tests will automatically be run, the webpack build created, and the tests run again on the new webpacked build. We _may_ want to just trust that Travis will build and test everything for us, but having everything run on `publish` ensures that we don't ship an old or broken `./build` directory (since `./build` will *not* be created on `npm install`).

Our new `.npmignore` will ignore most of our unneeded files and directories on npm publish, but it will include `./lib` and `./build`.

Before `npm publish`ing to production we'll want to test the interaction of the new build steps, our `.npmignore`, and `./build` on a variety of OSes (mainly Windows in different configurations to ensure the non-native fallback method works appropriately), perhaps by temporarily publishing to npm under a different name.

---

With all that said and done, once axic/scrypt.js#3 is merged and published to npm we'll likely get rid of our node webpack build all together; currently, the only other consideration is `eth-block-tracker`, which is potentially node 6 incompatible, so we may still want to use its es5 build. _Note: `secp256k1` already automatically falls back to a pure-JS implementation if a native build isn't possible._
  • Loading branch information
davidmurdoch authored and mikeseese committed Sep 10, 2018
1 parent aba6346 commit 22e3af6
Show file tree
Hide file tree
Showing 45 changed files with 5,882 additions and 1,865 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ TODO
.DS_Store
.tern-port
.vscode
yarn.lock
build
13 changes: 13 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.npmignore
.tern-project
.tern-port
.dockerignore
.travis.yml
.eslintrc.js
*.log
Dockerfile
ISSUE_TEMPLATE.md
.vscode/
test/
webpack/
perf/
45 changes: 34 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
var Provider = require("./lib/provider");
var Server = require("./lib/server");

// This interface exists so as not to cause breaking changes.
module.exports = {
server: function(options) {
return Server.create(options);
},
provider: function(options) {
return new Provider(options);
// make sourcemaps work!
require("source-map-support/register");

const debug = require("debug")("ganache");

// we use optional dependencies which may, or may not exist, so try native first
try {
// make sure these exist before we try to load ganache with native modules
const optionalDependencies = require("./package.json").optionalDependencies;
const wrongWeb3 = require("web3/package.json").version !== optionalDependencies["web3"];
const wrongEthereumJs = require("ethereumjs-wallet/package.json").version !== optionalDependencies["ethereumjs-wallet"];
if (wrongWeb3 || wrongEthereumJs) {
useBundled();
} else {
module.exports = require("./public-exports.js");
module.exports._webpacked = false;
debug("Optional dependencies installed; exporting ganache-core with native optional dependencies.");
}
} catch (nativeError) {
debug(nativeError);

// grabbing the native/optional deps failed, try using our webpacked build.
useBundled();
}

function useBundled() {
try {
module.exports = require("./build/ganache.core.node.js");
module.exports._webpacked = true;
debug("Optional dependencies not installed; exporting ganache-core from `./build` directory.");
} catch(webpackError) {
debug("ganache-core could not be exported; optional dependencies nor webpack build available for export.");
throw webpackError;
}
};
}
15 changes: 2 additions & 13 deletions lib/database.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var LevelUpArrayAdapter = require("./database/leveluparrayadapter");
var LevelUpObjectAdapter = require("./database/levelupobjectadapter");
var levelup = require('levelup');
var encode = require('encoding-down')
var filedown = require("./database/filedown");
var cachedown = require("cachedown");
var Sublevel = require("level-sublevel");
Expand Down Expand Up @@ -32,19 +33,7 @@ Database.prototype.initialize = function(callback) {

getDir(function(err, directory) {
if (err) return callback(err);

levelup(directory, {
valueEncoding: "json",
db: function (location) {
if (self.options.db) return self.options.db;

// This cache size was chosen from a plethora of hand testing.
// It seems a not-too-large cache (100) size is the right amount.
// When higher (say 10000), it seems the benefits wear off.
// See /perf/transactions.js for a benchmark.
return cachedown(location, filedown).maxSize(100);
}
}, finishInitializing)
levelup(self.options.db || encode(cachedown(directory, filedown).maxSize(100)), {valueEncoding: "json"}, finishInitializing);
});

function finishInitializing(err, db) {
Expand Down
3 changes: 3 additions & 0 deletions lib/provider.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// make sourcemaps work!
require('source-map-support/register')

let ProviderEngine = require("web3-provider-engine");
let SubscriptionSubprovider = require('web3-provider-engine/subproviders/subscriptions');

Expand Down
3 changes: 3 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// make sourcemaps work!
require('source-map-support/register')

var Provider = require("./provider");
var webSocketServer = require("./webSocketServer");
var httpServer = require("./httpServer");
Expand Down
Loading

0 comments on commit 22e3af6

Please sign in to comment.