mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2025-01-05 19:53:55 +03:00
Add --omit-default-module-path CLI flag (#2519)
Adds a new CLI flag `--omit-default-module-path`, to control the `default_module_path` generated code. The `default_module_path` generated code is enabled by default, users need to explicitly disable it by setting the `--omit-default-module-path` CLI flag.
This commit is contained in:
parent
fda6bb9f24
commit
d4679a0336
@ -640,15 +640,16 @@ impl<'a> Context<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
let default_module_path = match self.config.mode {
|
||||
OutputMode::Web => format!(
|
||||
"\
|
||||
let default_module_path = if !self.config.omit_default_module_path {
|
||||
match self.config.mode {
|
||||
OutputMode::Web => format!(
|
||||
"\
|
||||
if (typeof input === 'undefined') {{
|
||||
input = new URL('{stem}_bg.wasm', import.meta.url);
|
||||
}}",
|
||||
stem = self.config.stem()?
|
||||
),
|
||||
OutputMode::NoModules { .. } => "\
|
||||
stem = self.config.stem()?
|
||||
),
|
||||
OutputMode::NoModules { .. } => "\
|
||||
if (typeof input === 'undefined') {
|
||||
let src;
|
||||
if (typeof document === 'undefined') {
|
||||
@ -658,11 +659,17 @@ impl<'a> Context<'a> {
|
||||
}
|
||||
input = src.replace(/\\.js$/, '_bg.wasm');
|
||||
}"
|
||||
.to_string(),
|
||||
_ => "".to_string(),
|
||||
.to_string(),
|
||||
_ => "".to_string(),
|
||||
}
|
||||
} else {
|
||||
String::from("")
|
||||
};
|
||||
|
||||
let ts = self.ts_for_init_fn(has_memory, !default_module_path.is_empty())?;
|
||||
let ts = self.ts_for_init_fn(
|
||||
has_memory,
|
||||
!self.config.omit_default_module_path && !default_module_path.is_empty(),
|
||||
)?;
|
||||
|
||||
// Initialize the `imports` object for all import definitions that we're
|
||||
// directed to wire up.
|
||||
|
@ -33,6 +33,7 @@ pub struct Bindgen {
|
||||
keep_debug: bool,
|
||||
remove_name_section: bool,
|
||||
remove_producers_section: bool,
|
||||
omit_default_module_path: bool,
|
||||
emit_start: bool,
|
||||
// Experimental support for weakrefs, an upcoming ECMAScript feature.
|
||||
// Currently only enable-able through an env var.
|
||||
@ -115,6 +116,7 @@ impl Bindgen {
|
||||
multi_value: multi_value || wasm_interface_types,
|
||||
wasm_interface_types,
|
||||
encode_into: EncodeInto::Test,
|
||||
omit_default_module_path: true,
|
||||
}
|
||||
}
|
||||
|
||||
@ -282,6 +284,11 @@ impl Bindgen {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn omit_default_module_path(&mut self, omit_default_module_path: bool) -> &mut Bindgen {
|
||||
self.omit_default_module_path = omit_default_module_path;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn generate<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
|
||||
self.generate_output()?.emit(path.as_ref())
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ Options:
|
||||
--keep-debug Keep debug sections in wasm files
|
||||
--remove-name-section Remove the debugging `name` section of the file
|
||||
--remove-producers-section Remove the telemetry `producers` section
|
||||
--omit-default-module-path Don't add WebAssembly fallback imports in generated JavaScript
|
||||
--encode-into MODE Whether or not to use TextEncoder#encodeInto,
|
||||
valid values are [test, always, never]
|
||||
--nodejs Deprecated, use `--target nodejs`
|
||||
@ -66,6 +67,7 @@ struct Args {
|
||||
flag_keep_debug: bool,
|
||||
flag_encode_into: Option<String>,
|
||||
flag_target: Option<String>,
|
||||
flag_omit_default_module_path: bool,
|
||||
arg_input: Option<PathBuf>,
|
||||
}
|
||||
|
||||
@ -117,7 +119,8 @@ fn rmain(args: &Args) -> Result<(), Error> {
|
||||
.remove_name_section(args.flag_remove_name_section)
|
||||
.remove_producers_section(args.flag_remove_producers_section)
|
||||
.typescript(typescript)
|
||||
.omit_imports(args.flag_omit_imports);
|
||||
.omit_imports(args.flag_omit_imports)
|
||||
.omit_default_module_path(args.flag_omit_default_module_path);
|
||||
if let Some(true) = args.flag_weak_refs {
|
||||
b.weak_refs(true);
|
||||
}
|
||||
|
@ -226,6 +226,93 @@ fn bin_crate_works() {
|
||||
.stdout("hello, world\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_module_path_target_web() {
|
||||
let (mut cmd, out_dir) = Project::new("default_module_path_target_web")
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
"#,
|
||||
)
|
||||
.wasm_bindgen("--target web");
|
||||
cmd.assert().success();
|
||||
let contents = fs::read_to_string(out_dir.join("default_module_path_target_web.js")).unwrap();
|
||||
assert!(contents.contains(
|
||||
"\
|
||||
async function init(input) {
|
||||
if (typeof input === 'undefined') {
|
||||
input = new URL('default_module_path_target_web_bg.wasm', import.meta.url);
|
||||
}",
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_module_path_target_no_modules() {
|
||||
let (mut cmd, out_dir) = Project::new("default_module_path_target_no_modules")
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
"#,
|
||||
)
|
||||
.wasm_bindgen("--target no-modules");
|
||||
cmd.assert().success();
|
||||
let contents =
|
||||
fs::read_to_string(out_dir.join("default_module_path_target_no_modules.js")).unwrap();
|
||||
assert!(contents.contains(
|
||||
"\
|
||||
async function init(input) {
|
||||
if (typeof input === 'undefined') {
|
||||
let src;
|
||||
if (typeof document === 'undefined') {
|
||||
src = location.href;
|
||||
} else {
|
||||
src = document.currentScript.src;
|
||||
}
|
||||
input = src.replace(/\\.js$/, '_bg.wasm');
|
||||
}",
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn omit_default_module_path_target_web() {
|
||||
let (mut cmd, out_dir) = Project::new("omit_default_module_path_target_web")
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
"#,
|
||||
)
|
||||
.wasm_bindgen("--target web --omit-default-module-path");
|
||||
cmd.assert().success();
|
||||
let contents =
|
||||
fs::read_to_string(out_dir.join("omit_default_module_path_target_web.js")).unwrap();
|
||||
assert!(contents.contains(
|
||||
"\
|
||||
async function init(input) {
|
||||
|
||||
const imports = {};",
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn omit_default_module_path_target_no_modules() {
|
||||
let (mut cmd, out_dir) = Project::new("omit_default_module_path_target_no_modules")
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
"#,
|
||||
)
|
||||
.wasm_bindgen("--target no-modules --omit-default-module-path");
|
||||
cmd.assert().success();
|
||||
let contents =
|
||||
fs::read_to_string(out_dir.join("omit_default_module_path_target_no_modules.js")).unwrap();
|
||||
assert!(contents.contains(
|
||||
"\
|
||||
async function init(input) {
|
||||
|
||||
const imports = {};",
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_interface_types() {
|
||||
let (mut cmd, _out_dir) = Project::new("empty_interface_types")
|
||||
|
@ -118,3 +118,7 @@ proposal](https://github.com/webassembly/reference-types) proposal, meaning that
|
||||
the WebAssembly binary will use `externref` when importing and exporting
|
||||
functions that work with `JsValue`. For more information see the [documentation
|
||||
about reference types](./reference-types.md).
|
||||
|
||||
### `--omit-default-module-path`
|
||||
|
||||
Don't add WebAssembly fallback imports in generated JavaScript.
|
Loading…
Reference in New Issue
Block a user