Setting up your typescript project
A typical Coinweb project written in Typescript will typically use a combination of the wallet library and one or more contract modules. As contract modules are standalone packages, a project setup will easily consist of multiple packages, in which case using tools like yarn workspaces or lerna might be appropriate.
We will use the following example as our default project, showing a combination of an end-user project and using on-chain smart contracts.
There is a main-project which consists of normal typescript (maybe a React
project), and as dependencies it uses two other contract modules:
contract-module-a
, and contract-module-b
.
Setting up a Yarn Workspace with Dependencies
-
Initialize a new project:
mkdir my-workspace
cd my-workspace
yarn init -
Modify your
package.json
to add theworkspaces
field: This defines which directories are considered Yarn workspaces.{
"name": "my-workspace",
"private": true,
"workspaces": ["packages/*"],
"nohoist": ["**"]
}The
nohoist
option is currently required in order to be compatible with thecweb-tool audit
command. -
Create the
packages
directory and your individual packages:
- Windows
- macOS
- Linux
foreach ($dir in "main-project", "contract-module-a", "contract-module-b") {
mkdir $dir
}
mkdir packages
cd packages
mkdir main-project contract-module-a contract-module-b
mkdir packages
cd packages
mkdir main-project contract-module-a contract-module-b
-
Now your directory structure should look like this:
my-workspace/
│
├── packages/
│ ├── main-project/
│ ├── contract-module-a/
│ └── contract-module-b/
│
└── package.json -
Initialize each package: Navigate to each package directory and initialize it.
cd main-project
yarn init
cd ../contract-module-a
yarn init
cd ../contract-module-b
yarn init -
Adding Dependencies:
Inside
main-project
,contract-module-a
, andcontract-module-b
:
- main-project
- contract-module-a
- contract-module-b
cd main-project
yarn add @coinweb/wallet-lib
yarn add @coinweb/cweb-tool --dev
cd ../contract-module-a
yarn add contract-module-b contract-module-ext
yarn add webpack webpack-cli ts-loader terser-webpack-plugin @coinweb/cweb-tool --dev
npx cweb-tool init
cd ../contract-module-b
yarn add contract-module-ext
yarn add webpack webpack-cli ts-loader terser-webpack-plugin @coinweb/cweb-tool --dev
npx cweb-tool init
For TypeScript development:
- Use
tsc --init
inside each package to generate atsconfig.json
. - Configure the
tsconfig.json
based on your needs. - Use tools like
tslint
oreslint
with TypeScript plugins for linting. - Follow the instructions in the next section to setup
webpack
to bundle your TypeScript code.
Webpack Configuration:
Create a file named webpack.config.js
in the roots of contract-module-a
and
in contract-module-b
:
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
module.exports = (env, argv) => {
const isProduction = argv.mode === 'production';
return {
entry: './src/main.ts',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'onchain_lib'),
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json'],
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
optimization: {
minimize: isProduction,
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: true,
},
},
}),
],
},
};
};
tsconfig.json
settings:
For the contract modules, ensure your tsconfig.json
has at least the following
settings:
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "node",
"target": "ES5"
}
}
package.json
scripts:
In your contract-module-a/package.json
, and contract-module-b/package.json
files, add the following to the scripts
section:
"scripts": {
"build": "webpack --mode production",
"dev": "webpack --mode development",
"postinstall": "cweb-tool audit",
}
With these scripts:
-
Running
yarn build
will generate a production-ready, minified output atonchain_lib/main.js
. -
Running
yarn dev
will generate a development version of the output without minification. -
Running
yarn add ...
will audit whether the new dependency works for the contract module.
By default, the production mode in Webpack will enable various optimizations including minification. The TerserPlugin will take care of the minification, and with the configuration provided, it will also drop all console logs. Adjust as per your needs!