0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00

feat: import.meta.filename and import.meta.dirname (#22061)

This commit adds `import.meta.filename` and `import.meta.dirname` APIs.

These APIs return string representation of filename and containing
dirname.
They are only defined for local modules (modules that have `file:///`
scheme).

Example:

```ts
console.log(import.meta.filename, import.meta.dirname)
```

Unix:
```
$ deno run /dev/my_module.ts
/dev/my_module.ts /dev/
```

Windows:
```
$ deno run C:\dev\my_module.ts
C:\dev\my_module.ts C:\
```
This commit is contained in:
Bartek Iwańczuk 2024-01-23 23:51:07 +01:00 committed by GitHub
parent 13d5c6420e
commit 60688c563e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 50 additions and 6 deletions

View file

@ -1493,6 +1493,7 @@ itest!(if_main {
itest!(import_meta {
args: "run --quiet --reload --import-map=run/import_meta/importmap.json run/import_meta/main.ts",
output: "run/import_meta/main.out",
http_server: true,
});
itest!(main_module {

View file

@ -1,5 +1,6 @@
other [WILDCARD]other.ts false
main [WILDCARD]main.ts true
other remote [WILDCARD]other.ts false undefined undefined
other [WILDCARD]other.ts false [WILDCARD]other.ts [WILDCARD]
main [WILDCARD]main.ts true [WILDCARD]main.ts [WILDCARD]
Resolving ./foo.js file:///[WILDCARD]/foo.js
Resolving bare from import map https://example.com/
Resolving https://example.com/rewrite from import map https://example.com/rewritten

View file

@ -1,9 +1,15 @@
import { assertThrows } from "../../../../../test_util/std/assert/mod.ts";
console.log("main", import.meta.url, import.meta.main);
import "http://localhost:4545/run/import_meta/other.ts";
import "./other.ts";
console.log(
"main",
import.meta.url,
import.meta.main,
import.meta.filename,
import.meta.dirname,
);
console.log("Resolving ./foo.js", import.meta.resolve("./foo.js"));
console.log("Resolving bare from import map", import.meta.resolve("bare"));
console.log(

View file

@ -1 +1,7 @@
console.log("other", import.meta.url, import.meta.main);
console.log(
import.meta.url.startsWith("http") ? "other remote" : "other",
import.meta.url,
import.meta.main,
import.meta.filename,
import.meta.dirname,
);

View file

@ -28,6 +28,36 @@ declare interface ImportMeta {
*/
url: string;
/** The absolute path of the current module.
*
* This property is only provided for local modules (ie. using `file://` URLs).
*
* Example:
* ```
* // Unix
* console.log(import.meta.filename); // /home/alice/my_module.ts
*
* // Windows
* console.log(import.meta.filename); // C:\alice\my_module.ts
* ```
*/
filename?: string;
/** The absolute path of the dirrectory containing the current module.
*
* This property is only provided for local modules (ie. using `file://` URLs).
*
* * Example:
* ```
* // Unix
* console.log(import.meta.dirname); // /home/alice/
*
* // Windows
* console.log(import.meta.dirname); // C:\alice\
* ```
*/
dirname?: string;
/** A flag that indicates if the current module is the main module that was
* called when starting the program under Deno.
*