@nomiclabs/buidler
Version:
Buidler is an extensible developer tool that helps smart contract developers increase productivity by reliably bringing together the tools they want.
67 lines • 2.66 kB
JavaScript
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const downloader_1 = require("./downloader");
class Compiler {
constructor(version, compilersDir, compilerDownloader) {
this._version = version;
this._compilersDir = compilersDir;
this._localSolcVersion = Compiler._getLocalSolcVersion();
if (compilerDownloader !== undefined) {
this._downloader = compilerDownloader;
}
else {
this._downloader = new downloader_1.CompilerDownloader(this._compilersDir, this._localSolcVersion);
}
}
static _getLocalSolcVersion() {
return require("solc/package.json").version;
}
async compile(input) {
const solc = await this.getSolc();
const jsonOutput = solc.compile(JSON.stringify(input));
return JSON.parse(jsonOutput);
}
async getSolc() {
if (this._loadedSolc !== undefined) {
return this._loadedSolc;
}
if (this._isUsingLocalSolcVersion()) {
this._loadedSolc = require("solc");
return this._loadedSolc;
}
const compilerPath = await this._downloader.getDownloadedCompilerPath(this._version);
const { default: solcWrapper } = await Promise.resolve().then(() => __importStar(require("solc/wrapper")));
this._loadedSolc = solcWrapper(this._loadCompilerSources(compilerPath));
return this._loadedSolc;
}
_isUsingLocalSolcVersion() {
return this._version === this._localSolcVersion;
}
/**
* This function loads the compiler sources bypassing any require hook.
*
* The compiler is a huge asm.js file, and using a simple require may trigger
* babel/register and hang the process.
*/
_loadCompilerSources(compilerPath) {
const Module = module.constructor;
const previousHook = Module._extensions[".js"];
Module._extensions[".js"] = function (module, filename) {
const content = fs.readFileSync(filename, "utf8");
Object.getPrototypeOf(module)._compile.call(module, content, filename);
};
const loadedSolc = require(compilerPath);
Module._extensions[".js"] = previousHook;
return loadedSolc;
}
}
exports.Compiler = Compiler;
//# sourceMappingURL=index.js.map
;