-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add --compat flag to provide built-in Node modules #12293
Changes from all commits
4f530c4
ac5bc75
2ff2fd5
60e6949
2e55a29
d1ccdf3
5e2c2ad
5c7a075
947fc4e
154f949
24923bc
71ca413
17668f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 | ||
} |
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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ use deno_tls::rustls_native_certs::load_native_certs; | |
use deno_tls::webpki_roots::TLS_SERVER_ROOTS; | ||
use import_map::ImportMap; | ||
use log::debug; | ||
use log::info; | ||
use log::warn; | ||
use std::collections::HashMap; | ||
use std::collections::HashSet; | ||
|
@@ -182,7 +183,7 @@ impl ProcState { | |
None | ||
}; | ||
|
||
let maybe_import_map: Option<ImportMap> = | ||
let mut maybe_import_map: Option<ImportMap> = | ||
match flags.import_map_path.as_ref() { | ||
None => None, | ||
Some(import_map_url) => { | ||
|
@@ -204,6 +205,32 @@ impl ProcState { | |
} | ||
}; | ||
|
||
if flags.compat { | ||
let mut import_map = match maybe_import_map { | ||
Some(import_map) => import_map, | ||
None => { | ||
// INFO: we're creating an empty import map, with its specifier pointing | ||
// to `CWD/node_import_map.json` to make sure the map still works as expected. | ||
let import_map_specifier = | ||
std::env::current_dir()?.join("node_import_map.json"); | ||
ImportMap::from_json(import_map_specifier.to_str().unwrap(), "{}") | ||
.unwrap() | ||
} | ||
}; | ||
let node_builtins = crate::compat::get_mapped_node_builtins(); | ||
let diagnostics = import_map.update_imports(node_builtins)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if some already has entries for some/all of them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A diagnostic is printed out to the terminal, like so:
I just noticed that the output is broken and will fix it before landing the PR, should be
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a test: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Finally shows up as:
Suggestions welcome There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it is still unclear to the user what the state is or what action they should take. Is it really a problem? If it is a problem, what should a user do about it to resolve it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I clarified it even further:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool |
||
|
||
if !diagnostics.is_empty() { | ||
info!("Some Node built-ins were not added to the import map:"); | ||
for diagnostic in diagnostics { | ||
info!(" - {}", diagnostic); | ||
} | ||
info!("If you want to use Node built-ins provided by Deno remove listed specifiers from \"imports\" mapping in the import map file."); | ||
} | ||
|
||
maybe_import_map = Some(import_map); | ||
} | ||
|
||
let maybe_inspect_host = flags.inspect.or(flags.inspect_brk); | ||
let maybe_inspector_server = maybe_inspect_host.map(|host| { | ||
Arc::new(InspectorServer::new(host, version::get_user_agent())) | ||
|
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, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"imports": { | ||
"fs/promises": "./non_existent_file.js" | ||
} | ||
} |
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] |
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); |
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. |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this not going to print the
X-Deno-Warning
text every time the user caches it for the first time, with no clue as how to fix it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep - we can worry about this later.