Package detail

sol-merger

RyuuGan8.8kBSD-2-Clause4.4.2

Merges all import files into single file.

smartcontract, sol, merger, solidity

readme

Build status

Build status

Quick Usage

const { merge } = require('sol-merger');

// Get the merged code as a string
const mergedCode = await merge('./contracts/MyContract.sol');
// Print it out or write it to a file etc.
console.log(mergedCode);

CLI Usage

Right now it only works with solidity files that are in node_modules or relative to your solidity file.

Simple usage:

npm i --save-dev sol-merger

Then add following line to your package.json.

{
  "scripts": {
    "build-contracts": "sol-merger \"./contracts/*.sol\" ./build"
  }
}

This will allow you to use npm run build-contracts in your project directory.

Note that contracts glob should be surrounded with "

If no output file specified then output file will be created at the same directory and appended with _merged (by default), i.e. MyContract_merged.sol. You can also change this behaviour by specifying --append option:

sol-merger --append _me "./contracts/*.sol"
sol-merger -a _me "./contracts/*.sol"

You may need to use npm run to invoke the program standalone, as in npm run sol-merger.

You can also get help via --help command

sol-merger -h
sol-merger --help

More info about glob available at node-glob repository

See tests for more examples.

Post processing support

It is possible right now to write your own plugins to process exports. Currently state is not used in plugins. To see available plugins take a look plugins page.

All default plugins are available via shortcut syntax using filename:

sol-merger --export-plugin SPDXLicenseRemovePlugin "test/contracts/*.sol" compiled

It is also possible to use external plugins via relative path to plugin or path relative to node_modules:

sol-merger --export-plugin ./dist/lib/plugins/SPDXLicenseRemovePlugin.js "test/contracts/*.sol" compiled

sol-merger --export-plugin sol-merger/lib/plugins/SPDXLicenseRemovePlugin.js "test/contracts/*.sol" compiled

Note that file extension is required for plugin to be loaded.

Additional root folders for searching contracts

By default, sol-merger only searches for contracts in the node_modules folder. However, you can include additional root folders to search for contracts. This can be done by passing a CLI argument or a parameter through the code.

If additional root folders are specified, sol-merger will first search for the contract in the node_modules folder and then in the additional root folders. Multiple folders can be included by passing a CLI argument multiple times.

Example of usage in CLI:

sol-merger --additional-root "./test/contracts/imports" "test/contracts/ImportWithAdditionalRoot.sol" compiled

Example of usage in code:

const merger = new Merger({
  delimeter: '\n\n',
  additionalRoots: ['./test/contracts/imports'],
});

Debuging

It is possible to setup environment variable to see what is happening under the hood when contracts are being compiled:

export DEBUG="sol-merger*"

Plugin for VSCode

I created a plugin for VSCode so you can use it instead of sol-merger: Solidity Contracts Merger.

Hope you will like it.

changelog

4.4.2 (2025-04-03)

4.4.1 (2023-12-10)

Bug Fixes

  • exports: fix calculating export is clause for the contacts (acc581e), closes #73

4.4.0 (2023-04-15)

Features

  • sol-merger: add support for additional roots for resolving contracts (a4447fa)

4.3.0 (2023-02-24)

Features

  • grammar: update grammar to 0.8.19 (5708118)

4.2.0 (2023-02-04)

Features

  • grammar: update to new grammar 0.8.18 (7a454c7)

Reverts

  • Revert "chore: add build on prepublishOnly" (1891c55)

4.1.1 (2022-04-03)

4.1.0 (2022-04-03)

Features

  • grammar: update grammar to support using directive at root scope (2579237)
  • using-for: add support for Using-For at root level (fac2708)

4.0.0 (2022-02-22)

Bug Fixes

  • types: fix typo in the Type name, RegisteredImport (18481aa)
  • typos: fix typo in ExportsAnalyzerResult abstract property (ba01183)

Features

  • grammar: update grammar to support 0.8.11 version (63487f3)
  • sol-0.8: add support for errors exports (7061943)
  • sol-0.8: add support for export of constants at root level (1925195)
  • sol-0.8: add support for export of function at root level (7a9e2db)
  • sol-0.8: add support for export of user defined types at root level (6ae568b)
  • testing: add testing in docker environment (30fcfa8)
  • update node.js version (4eb7990)

BREAKING CHANGES

  • lower node.js versions will not be supported
  • typos: rename abstact to abstract
  • types: has to import another type of needed.

3.1.0 (2020-06-26)

Features

  • cli: add support for export plugins in cli (663bb8a)
  • exports: add plugins to post process exports (4553fb7), closes #37
  • plugins: add support for plugins shortcuts (68e0301)

3.0.1 (2020-05-17)

Features

  • abstract: add support for abstract class (555ffaf)
  • comments: add global comments as exports (bf0b6e4)
  • comments: add support for comments delimeter (1db6cda)

3.0.0 (2020-05-17)

Features

BREAKING CHANGES

  • solidity: ImportsAnalyzer analizeImport is now private

2.0.1 (2020-02-07)

Bug Fixes

  • imports: change analyzer for imports statements (3b31a00)

2.0.0 (2020-02-05)

Features

  • import-registry: extract import registry from FileAnalyzer, make removeComments false by default (65bc4e6)
  • Merger: add support for circular dependencies (a7dd280), closes #27
  • nodejs: update minimum version to node 10 (95179e0)

BREAKING CHANGES

  • import-registry: removeComments false by default
  • nodejs: update minimum version of nodejs supported to node 10

1.2.2 (2020-01-07)

Bug Fixes

  • bin: don't use package.json, so it will not be copied (d7e7d5e)

1.2.1 (2020-01-07)

1.2.0 (2020-01-05)

Bug Fixes

  • FileAnalyzer: remove global from regex, remove contract if it is analyzed (685bdc3), closes #24

Features

  • comments: add comments support to CLI tool (3f4ad12)
  • comments: add support for comments inside exports (776d053), closes #19
  • FileAnalyzer: use external library to parse contracts. (cbee328), closes #24
  • merge: add support for optional options in merge function (06c428a)

1.1.1 (2019-10-22)

Bug Fixes

  • FileAnalyzer: update regex for inheriting from more than two contracts (b09483b)

1.1.0 (2019-10-20)

Bug Fixes

  • command: update promise all then. (c570da0)

Features

  • fileAnalyzer: add support to use parameters in is clause (423adb1), closes #21

1.0.1 (2019-09-10)

Bug Fixes

1.0.0 (2019-07-18)

Bug Fixes

  • build: update zeppelin-solidity to be compiled in latest node (2cfd913)

Features

  • core: allow basic named imports (4e82f8d)
  • deps: remove bluebird from deps, update deps (9f7b0f5)
  • FileAnalyzer: add support for analyzing solidity files (b9ee308)
  • GlobalRename: add support for global renames (8f43b8e)
  • imports: add basic named imports support (a42c11e)
  • pragma: add support for experemental pragma. Ref #18 (3229f89)
  • ts: migrated to TypeScript (9313ac0)

0.1.3 (2018-12-13)

Bug Fixes

  • nodeModulesRoot: now node modules resolves only once, but not for every file (5a92283)