mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05: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:
parent
13d5c6420e
commit
60688c563e
5 changed files with 50 additions and 6 deletions
|
@ -1493,6 +1493,7 @@ itest!(if_main {
|
||||||
itest!(import_meta {
|
itest!(import_meta {
|
||||||
args: "run --quiet --reload --import-map=run/import_meta/importmap.json run/import_meta/main.ts",
|
args: "run --quiet --reload --import-map=run/import_meta/importmap.json run/import_meta/main.ts",
|
||||||
output: "run/import_meta/main.out",
|
output: "run/import_meta/main.out",
|
||||||
|
http_server: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
itest!(main_module {
|
itest!(main_module {
|
||||||
|
|
5
cli/tests/testdata/run/import_meta/main.out
vendored
5
cli/tests/testdata/run/import_meta/main.out
vendored
|
@ -1,5 +1,6 @@
|
||||||
other [WILDCARD]other.ts false
|
other remote [WILDCARD]other.ts false undefined undefined
|
||||||
main [WILDCARD]main.ts true
|
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 ./foo.js file:///[WILDCARD]/foo.js
|
||||||
Resolving bare from import map https://example.com/
|
Resolving bare from import map https://example.com/
|
||||||
Resolving https://example.com/rewrite from import map https://example.com/rewritten
|
Resolving https://example.com/rewrite from import map https://example.com/rewritten
|
||||||
|
|
12
cli/tests/testdata/run/import_meta/main.ts
vendored
12
cli/tests/testdata/run/import_meta/main.ts
vendored
|
@ -1,9 +1,15 @@
|
||||||
import { assertThrows } from "../../../../../test_util/std/assert/mod.ts";
|
import { assertThrows } from "../../../../../test_util/std/assert/mod.ts";
|
||||||
|
import "http://localhost:4545/run/import_meta/other.ts";
|
||||||
console.log("main", import.meta.url, import.meta.main);
|
|
||||||
|
|
||||||
import "./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 ./foo.js", import.meta.resolve("./foo.js"));
|
||||||
console.log("Resolving bare from import map", import.meta.resolve("bare"));
|
console.log("Resolving bare from import map", import.meta.resolve("bare"));
|
||||||
console.log(
|
console.log(
|
||||||
|
|
8
cli/tests/testdata/run/import_meta/other.ts
vendored
8
cli/tests/testdata/run/import_meta/other.ts
vendored
|
@ -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,
|
||||||
|
);
|
||||||
|
|
30
cli/tsc/dts/lib.deno.ns.d.ts
vendored
30
cli/tsc/dts/lib.deno.ns.d.ts
vendored
|
@ -28,6 +28,36 @@ declare interface ImportMeta {
|
||||||
*/
|
*/
|
||||||
url: string;
|
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
|
/** A flag that indicates if the current module is the main module that was
|
||||||
* called when starting the program under Deno.
|
* called when starting the program under Deno.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue