Skip to content

Commit 6ef57c3

Browse files
committed
Added a functionality to convert "__dirname" to "import.meta.dirname"
1 parent 98a6997 commit 6ef57c3

File tree

3 files changed

+58
-23
lines changed

3 files changed

+58
-23
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ The best way to use this package is to integrate it with your build process by a
6464
}
6565
}
6666
```
67+
68+
**Note:**
69+
All `__dirname` matches in `ESM` will be replaced with `import.meta.dirname`, for example:
70+
71+
```ts
72+
// TypeScript
73+
const dirname: string = __dirname;
74+
```
75+
76+
Will be compiled to:
77+
```js
78+
// CommonJS (CJS)
79+
const dirname = __dirname;
80+
```
81+
82+
```js
83+
// ECMAScript Modules (ESM)
84+
const dirname = import.meta.dirname;
85+
```
6786
___
6887
## License
6988
Please read the license from [here](https://github.com/nasriyasoftware/PostBuild?tab=License-1-ov-file).

main.js

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,6 @@ 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-
this.#_helpers.copy.helpers.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-
},
11899
exclude: ['.ts'],
119100
folderContent: (from, to, exclude) => {
120101
if (!fs.existsSync(to)) { fs.mkdirSync(to, { recursive: true }) }
@@ -144,7 +125,8 @@ class Main {
144125
if (this.#_config.esmDir) {
145126
this.#_helpers.print('Copying files to ESM...');
146127
this.#_helpers.copy.directory(from, path.join(constants.ROOT, constants.ESM_REL_PATH), exclude);
147-
this.#_helpers.copy.helpers.addJsonImportAssertions(this.#_config.esmDir);
128+
this.#_helpers.addJsonImportAssertions(this.#_config.esmDir);
129+
this.#_helpers.convertDirToImport(this.#_config.esmDir);
148130
}
149131

150132
if (this.#_config.cjsDir) {
@@ -247,19 +229,53 @@ class Main {
247229
throw new Error(`The ${constants.PACKAGE_NAME}'s config file is not a valid JSON file.`)
248230
}
249231
}
232+
},
233+
addJsonImportAssertions: (dir) => {
234+
this.#_helpers.print('Add JSON import assertion');
235+
fs.readdirSync(dir).forEach(file => {
236+
const fullPath = path.join(dir, file);
237+
if (fs.lstatSync(fullPath).isDirectory()) {
238+
this.#_helpers.addJsonImportAssertions(fullPath);
239+
} else if (file.endsWith('.js') || file.endsWith('.mjs')) {
240+
let content = fs.readFileSync(fullPath, 'utf8');
241+
content = content.replace(/import\s+(.*?)\s+from\s+(['"])(.*?)\.json\2\s*;/g, (match, imports, quote, modulePath) => {
242+
if (!imports.includes('assert')) {
243+
return `import ${imports} from ${quote}${modulePath}.json${quote} assert { type: "json" };`;
244+
}
245+
return match;
246+
});
247+
fs.writeFileSync(fullPath, content, 'utf8');
248+
}
249+
});
250+
},
251+
/**This method converts `_dirname = __dirname` to `_dirname = import.meta.dirname` */
252+
convertDirToImport: (dir) => {
253+
fs.readdirSync(dir).forEach(file => {
254+
const fullPath = path.join(dir, file);
255+
if (fs.lstatSync(fullPath).isDirectory()) {
256+
this.#_helpers.convertDirToImport(fullPath);
257+
} else if (file.endsWith('.js')) {
258+
let content = fs.readFileSync(fullPath, 'utf8');
259+
260+
// Replace __dirname with import.meta.dirname
261+
content = content.replace(/__dirname/g, 'import.meta.dirname');
262+
263+
fs.writeFileSync(fullPath, content, 'utf8');
264+
}
265+
});
250266
}
251267
}
252268

253269
run() {
254-
const st = performance.now();
270+
const st = Date.now();
255271

256272
this.#_helpers.config.check();
257273
this.#_helpers.read();
258274
this.#_helpers.create.packages();
259275
this.#_helpers.copy.run();
260276
this.#_helpers.extensions.run();
261277

262-
const et = performance.now();
278+
const et = Date.now();
263279
const duration = et - st;
264280
this.#_helpers.print(`PostBuild finishes in ${duration} milliseconds`);
265281
}

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.5",
3+
"version": "1.0.6",
44
"description": "A package that does some tasks after compilation",
55
"main": "main.js",
66
"type": "module",

0 commit comments

Comments
 (0)