Skip to content
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

ESM dynamic import to glob imports, throw synchronous error #3980

Open
suguanYang opened this issue Nov 18, 2024 · 0 comments
Open

ESM dynamic import to glob imports, throw synchronous error #3980

suguanYang opened this issue Nov 18, 2024 · 0 comments

Comments

@suguanYang
Copy link

Expected behavior

the transformed Import Calls should follow the ecma specification, which should be a Rejected Promise.

current behavior:

const main = async () => {
   const num = 1;
   await import(`./file${num }`).catch(err => {
         // can not capture module not fould error
   });

   try {
      await import(`./file${num }`)
   } catch (err) {
     // this could
   }

}

the transformed code:

  var __glob = (map) => (path) => {
    var fn = map[path];
    if (fn) return fn();
    throw new Error("Module not found in bundle: " + path); // synchronously throws an error.
  };
  // import("./file*") in entry.js
  var globImport_file = __glob({
    "./file.js": () => Promise.resolve().then(() => (init_file(), file_exports))
  });
  var main = async () => {
    const num = 3;
    await globImport_file(`./file${num}`).catch((err) => {
      if (err.code === "ERR_MODULE_NOT_FOUND") {
        console.log(err);
      }
    });
    try {
      await globImport_file(`./file${num}`);
    } catch (err) {
    }
  };

demo: https://esbuild.github.io/try/#YgAwLjI0LjAAIC0tYnVuZGxlAGUAZW50cnkuanMAY29uc3QgbWFpbiA9IGFzeW5jICgpID0+IHsKICAgY29uc3QgbnVtID0gMTsKICAgYXdhaXQgaW1wb3J0KGAuL2ZpbGUke251bSB9YCkuY2F0Y2goZXJyID0+IHsKICAgICAgIGlmIChlcnIuY29kZSA9PT0gIkVSUl9NT0RVTEVfTk9UX0ZPVU5EIikgewogICAgICAgICAvLyBub3Qgd29ya3MKICAgICAgIH0KICAgfSk7CgogICB0cnkgewogICAgICBhd2FpdCBpbXBvcnQoYC4vZmlsZSR7bnVtIH1gKQogICB9IGNhdGNoIChlcnIpIHsKICAgICAvLyB0aGlzIHdvcmtzCiAgIH0KCn0AAGZpbGUuanMAZXhwb3J0IGRlZmF1bHQgMTsAAGZpbGUyLmpzAGV4cG9ydCBkZWZhdWx0IDE7

Possible Workaround:

add a new glob for dynamic import:

var __globImport = map => path => {
	var fn = map[path]
	if (fn) return fn()
	return Promise.reject('Module not found')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant