Merge pull request #1424 from fitzgen/fix-imports

Don't put anything before ES module imports
This commit is contained in:
Alex Crichton 2019-04-04 20:27:37 -05:00 committed by GitHub
commit aa0aff674c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -354,7 +354,8 @@ impl<'a> Context<'a> {
| OutputMode::Node {
experimental_modules: true,
} => {
js.push_str(&format!("import * as wasm from './{}_bg';\n", module_name));
self.imports
.push_str(&format!("import * as wasm from './{}_bg';\n", module_name));
if needs_manual_start {
self.footer.push_str("wasm.__wbindgen_start();\n");
}
@ -365,7 +366,7 @@ impl<'a> Context<'a> {
// expose the same initialization function as `--target no-modules`
// as the default export of the module.
OutputMode::Web => {
js.push_str("const __exports = {};\n");
self.imports_post.push_str("const __exports = {};\n");
self.imports_post.push_str("let wasm;\n");
init = self.gen_init(&module_name, needs_manual_start);
self.footer.push_str("export default init;\n");
@ -377,6 +378,10 @@ impl<'a> Context<'a> {
ts.push_str(&init_ts);
// Emit all the JS for importing all our functionality
assert!(
!self.config.mode.uses_es_modules() || js.is_empty(),
"ES modules require imports to be at the start of the file"
);
js.push_str(&self.imports);
js.push_str("\n");
js.push_str(&self.imports_post);

View File

@ -42,6 +42,19 @@ enum OutputMode {
Node { experimental_modules: bool },
}
impl OutputMode {
fn uses_es_modules(&self) -> bool {
match self {
OutputMode::Bundler { .. }
| OutputMode::Web
| OutputMode::Node {
experimental_modules: true,
} => true,
_ => false,
}
}
}
enum Input {
Path(PathBuf),
Module(Module, String),