-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add --compat flag to provide built-in Node modules (#12293)
This commit adds "--compat" flag. When the flag is passed a set of mappings for built-in Node modules is injected into the import map. If user doesn't explicitly provide an import map (using "--import-map" flag) then a map is created on the fly. If there are already existing mappings in import map that would clash with built-in Node modules a set of diagnostics is printed to the terminal with suggestions how to proceed.
- Loading branch information
1 parent
64a7187
commit f1d3a17
Showing
14 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
use std::collections::HashMap; | ||
|
||
static SUPPORTED_MODULES: &[&str] = &[ | ||
"assert", | ||
"assert/strict", | ||
"async_hooks", | ||
"buffer", | ||
"child_process", | ||
"cluster", | ||
"console", | ||
"constants", | ||
"crypto", | ||
"dgram", | ||
"dns", | ||
"domain", | ||
"events", | ||
"fs", | ||
"fs/promises", | ||
"http", | ||
"https", | ||
"module", | ||
"net", | ||
"os", | ||
"path", | ||
"path/posix", | ||
"path/win32", | ||
"perf_hooks", | ||
"process", | ||
"querystring", | ||
"readline", | ||
"stream", | ||
"stream/promises", | ||
"stream/web", | ||
"string_decoder", | ||
"sys", | ||
"timers", | ||
"timers/promises", | ||
"tls", | ||
"tty", | ||
"url", | ||
"util", | ||
"util/types", | ||
"v8", | ||
"vm", | ||
"zlib", | ||
]; | ||
|
||
pub fn get_mapped_node_builtins() -> HashMap<String, String> { | ||
let mut mappings = HashMap::new(); | ||
|
||
for module in SUPPORTED_MODULES { | ||
// TODO(bartlomieju): this is unversioned, and should be fixed to use latest stable? | ||
let module_url = format!("https://deno.land/std/node/{}.ts", module); | ||
mappings.insert(module.to_string(), module_url); | ||
} | ||
|
||
mappings | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
mod ast; | ||
mod auth_tokens; | ||
mod checksum; | ||
mod compat; | ||
mod config_file; | ||
mod deno_dir; | ||
mod diagnostics; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
use crate::itest; | ||
|
||
itest!(fs_promises { | ||
args: "run --compat --unstable -A compat/fs_promises.js", | ||
output: "compat/fs_promises.out", | ||
}); | ||
|
||
itest!(existing_import_map { | ||
args: "run --compat --import-map compat/existing_import_map.json compat/fs_promises.js", | ||
output: "compat/existing_import_map.out", | ||
exit_code: 1, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"imports": { | ||
"fs/promises": "./non_existent_file.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[WILDCARD] | ||
Some Node built-ins were not added to the import map: | ||
- "fs/promises" already exists and is mapped to "[WILDCARD]non_existent_file.js" | ||
If you want to use Node built-ins provided by Deno remove listed specifiers from "imports" mapping in the import map file. | ||
error: Cannot resolve module [WILDCARD] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import fs from "fs/promises"; | ||
const data = await fs.readFile("compat/test.txt", "utf-8"); | ||
console.log(data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[WILDCARD] | ||
This is some example text that will be read using compatiblity mode. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is some example text that will be read using compatiblity mode. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters