Build Processes


The build command and this approach is being deprecated. Please use third-party build tools like webpack or grunt, or see our platon-truffle Boxes for an example.

In order to provide tight integration with platon truffle for those that desire it, platon truffle allows you to specify a custom build pipeline meant to bootstrap and configure your application. Truffle provides three methods of integration, described below.

Running an external command

If you’d like platon truffle to run an external command whenever it triggers a build, simply include that option as a string within your project configuration, like so:

module.exports = {
  // This will run the `webpack` command on each build.
  // The following environment variables will be set when running the command:
  // WORKING_DIRECTORY: root location of the project
  // BUILD_DESTINATION_DIRECTORY: expected destination of built assets (important for `truffle serve`)
  // BUILD_CONTRACTS_DIRECTORY: root location of your build contract files (.sol.js)
  build: "webpack"

Note that you’re given ample environment variables with which to integrate with platon truffle, detailed above.

Providing a custom function

You can also provide a custom build function like the one below. Note you’re given a plethora of information about your project which you can use to integrate tightly with Truffle.

module.exports = {
  build: function(options, callback) {
     // Do something when a build is required. `options` contains these values:
     // working_directory: root location of the project
     // contracts_directory: root directory of .sol files
     // destination_directory: directory where truffle expects the built assets (important for `truffle serve`)

Creating a custom module

You could also create a module or object that implements the builder interface (i.e., is an object which contains a build function like the one above). This is great for those who want to maintain tighter integration with Truffle and publish a package to make everyone else’s lives easier.

Here’s an example using platon truffle’s default builder:

var DefaultBuilder = require("truffle-default-builder");
module.exports = {
  build: new DefaultBuilder(...) // specify the default builder configuration here.

Bootstrapping your application

Whether you’re building an application to run in the browser, or a command line tool, a Javascript library or a native mobile application, bootstrapping your contracts is the same, and using your deployed contract artifacts follows the same general process no matter the app you’re building.

When configuring your build tool or application, you’ll need to perform the following steps:

  1. Get all your contract artifacts into your build pipeline / application. This includes all of the .json files within the ./build/contracts directory.
  2. Turn those .json contract artifacts into contract abstractions that are easy to use.
  3. Provision those contract abstractions with a Web3 provider.
  4. Use your contracts!

In Node, this is very easy to do. Let’s take a look at an example that shows off the “purest” way of performing the above steps, since it exists outside of any build process or tool.

// Step 1: Get a contract into my application
var json = require("./build/contracts/MyContract.json");

// Step 2: Turn that contract into an abstraction I can use
var contract = require("truffle-contract");
var MyContract = contract(json);

// Step 3: Provision the contract with a web3 provider
MyContract.setProvider(new Web3.providers.HttpProvider(""));

// Step 4: Use the contract!
MyContract.deployed().then(function(deployed) {
  return deployed.someFunction();

All build processes and contract bootstrapping will follow this pattern. The key when setting up your own custom build process is to ensure you’re consuming all of your contract artifacts and provisioning your abstractions correctly.