-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathremappings-helper.js
More file actions
73 lines (70 loc) · 2.42 KB
/
remappings-helper.js
File metadata and controls
73 lines (70 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require("hardhat-preprocessor");
const fs = require("fs");
/**
* Loads Solidity import remappings from a `remappings.txt` file.
*
* Each line in `remappings.txt` should be in the format:
* <alias>=<path>
* For example:
* @openzeppelin/=node_modules/@openzeppelin/
*
* This function:
* 1. Reads all lines from `./remappings.txt`.
* 2. Splits each line into an alias (`from`) and its target path (`to`).
* 3. Stores the mappings in an object for quick lookup.
*
* @returns {Object} An object mapping import aliases to their resolved paths.
*/
function loadRemappings() {
const lines = fs.readFileSync("./remappings.txt", "utf-8").split(/\r?\n|\r/);
const map = {};
for (const line of lines) {
const [from, to] = line.split("=");
map[from] = to;
}
return map;
}
/**
* Configures a Hardhat preprocessor to apply Solidity import remappings.
*
* This function:
* 1. Loads alias-to-path mappings from `remappings.txt` via `loadRemappings()`.
* 2. Returns a configuration object compatible with the `hardhat-preprocessor` plugin.
* 3. For each line of a Solidity file, if the line is an `import` statement, it checks
* whether any alias (`from`) appears in the line. If so, the alias is replaced with
* its corresponding path (`to`).
* 4. Provides the full remappings in `settings` for reference or debugging.
*
* Example:
* If `remappings.txt` contains:
* @openzeppelin/=node_modules/@openzeppelin/
*
* Then this import:
* import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
*
* Will be rewritten to:
* import "node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
*
* @returns {Object} A Hardhat preprocessor configuration with an `eachLine` hook
* that rewrites import paths according to defined remappings.
*/
function remapImportPaths(){
const remap = loadRemappings();
return {
eachLine: () => ({
transform: (line) => {
const text = line.trim();
if (text.startsWith("import ")) {
for (const [from, to] of Object.entries(remap)) {
if (text.includes(from)) {
return line.replace(from, to);
}
}
}
return line;
},
settings: { remappings: remap },
}),
};
}
module.exports={remapImportPaths}