Custom build process
Throughout the history of Truffle, default constructors are not for everyone. It has some obvious shortcomings and is less mature than other build systems. As such, Truffle provides three ways to extend the default build system, but allow you to experience most of Truffle's features.
execute external commands
If you want to execute an external command every time a build is triggered. An option can be included in the project's configuration.
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)
// WEB3_PROVIDER_LOCATION: rpc configuration as a string, as a URL needed for web3's http provider.
//
build: "webpack"
}
It should be noted that you need to provide the corresponding environment variables to integrate these external script commands into Truffle. For details, see the notes in the configuration.
provide a custom function
You can provide a custom build function. The framework provides you with project-related parameters to facilitate deep integration 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: metadata about your contract files, code, etc.
// contracts_directory: root directory of .sol files
// rpc: rpc configuration defined in the configuration
// destination_directory: directory where truffle expects the built assets (important for `truffle serve`)
}
}
Create a custom module
You can also implement the build interface by creating a module or object (an build
object containing functions, like in the previous section). This is suitable for situations where you need to integrate Truffle, but have your own release process.
Below is an example using the default building blocks.
var DefaultBuilder = require("truffle-default-builder");
module.exports = {
build: new DefaultBuilder(...) // specify the default builder configuration here.
}
Initialize the front end
Because you're using your own build process, Truffle no longer knows how to initialize your frontend. Here is a list of things to do:
- import
Web3
library - Initialize an instance of web3 and set a provider to point to your Ethereum client. It's important to check
web3
if the object already exists, because if someone goes through a wallet browser, like Metamask or Mist, there's a good chance the object already exists, and you should use this object instead of initializing a brand new one. Check out the examples to learn more. require
orimport
compiledsol.js
files from a./build/contracts
directory. It needs to be called for each fileMyContract.setProvider()
to setprovider
. This needs to be consistent withweb3
instance usageprovider
. can be usedweb3.currentProvider
to get the current oneprovider
.
var MyContract = require("./build/contracts/MyContract.sol.js");
MyContract.setProvider(web3.currentProvider);
Using WEBPACK
我们还在致力于与Webpack的紧密集成文章翻译来源于学什么技术好网