From 60688c563e6d02813b021ad91132fe1eb3f103b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 23 Jan 2024 23:51:07 +0100 Subject: [PATCH] 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:\ ``` --- cli/tests/integration/run_tests.rs | 1 + cli/tests/testdata/run/import_meta/main.out | 5 ++-- cli/tests/testdata/run/import_meta/main.ts | 12 ++++++--- cli/tests/testdata/run/import_meta/other.ts | 8 +++++- cli/tsc/dts/lib.deno.ns.d.ts | 30 +++++++++++++++++++++ 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index 429ec2a04c..3a2b46133b 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -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 { diff --git a/cli/tests/testdata/run/import_meta/main.out b/cli/tests/testdata/run/import_meta/main.out index 0329dea8a7..5a86d62409 100644 --- a/cli/tests/testdata/run/import_meta/main.out +++ b/cli/tests/testdata/run/import_meta/main.out @@ -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 diff --git a/cli/tests/testdata/run/import_meta/main.ts b/cli/tests/testdata/run/import_meta/main.ts index 96d63ba786..fb859e250e 100644 --- a/cli/tests/testdata/run/import_meta/main.ts +++ b/cli/tests/testdata/run/import_meta/main.ts @@ -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( diff --git a/cli/tests/testdata/run/import_meta/other.ts b/cli/tests/testdata/run/import_meta/other.ts index 47d7527cd7..5da6a49365 100644 --- a/cli/tests/testdata/run/import_meta/other.ts +++ b/cli/tests/testdata/run/import_meta/other.ts @@ -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, +); diff --git a/cli/tsc/dts/lib.deno.ns.d.ts b/cli/tsc/dts/lib.deno.ns.d.ts index 8d1e28d4be..483c5c3d01 100644 --- a/cli/tsc/dts/lib.deno.ns.d.ts +++ b/cli/tsc/dts/lib.deno.ns.d.ts @@ -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. *