Skip to content

Commit 5dc1dea

Browse files
committed
Add json assertion
1 parent 149af89 commit 5dc1dea

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

main.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ class Main {
9696
}
9797
},
9898
copy: {
99+
helpers: {
100+
addJsonImportAssertions(dir) {
101+
fs.readdirSync(dir).forEach(file => {
102+
const fullPath = path.join(dir, file);
103+
if (fs.lstatSync(fullPath).isDirectory()) {
104+
addJsonImportAssertions(fullPath);
105+
} else if (file.endsWith('.js') || file.endsWith('.mjs')) {
106+
let content = fs.readFileSync(fullPath, 'utf8');
107+
content = content.replace(/import\s+(.*?)\s+from\s+(['"])(.*?)\.json\2\s*;/g, (match, imports, quote, modulePath) => {
108+
if (!imports.includes('assert')) {
109+
return `import ${imports} from ${quote}${modulePath}.json${quote} assert { type: "json" };`;
110+
}
111+
return match;
112+
});
113+
fs.writeFileSync(fullPath, content, 'utf8');
114+
}
115+
});
116+
}
117+
},
99118
exclude: ['.ts'],
100119
folderContent: (from, to, exclude) => {
101120
if (!fs.existsSync(to)) { fs.mkdirSync(to, { recursive: true }) }
@@ -125,6 +144,7 @@ class Main {
125144
if (this.#_config.esmDir) {
126145
this.#_helpers.print('Copying files to ESM...');
127146
this.#_helpers.copy.directory(from, path.join(constants.ROOT, constants.ESM_REL_PATH), exclude);
147+
this.#_helpers.copy.helpers.addJsonImportAssertions(this.#_config.esmDir);
128148
}
129149

130150
if (this.#_config.cjsDir) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nasriya/postbuild",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A package that does some tasks after compilation",
55
"main": "main.js",
66
"type": "module",

0 commit comments

Comments
 (0)