Open suguanYang opened 4 days ago
the transformed Import Calls should follow the ecma specification, which should be a Rejected Promise.
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) { } };
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') }
Expected behavior
the transformed Import Calls should follow the ecma specification, which should be a Rejected Promise.
current behavior:
the transformed code:
demo: https://esbuild.github.io/try/#YgAwLjI0LjAAIC0tYnVuZGxlAGUAZW50cnkuanMAY29uc3QgbWFpbiA9IGFzeW5jICgpID0+IHsKICAgY29uc3QgbnVtID0gMTsKICAgYXdhaXQgaW1wb3J0KGAuL2ZpbGUke251bSB9YCkuY2F0Y2goZXJyID0+IHsKICAgICAgIGlmIChlcnIuY29kZSA9PT0gIkVSUl9NT0RVTEVfTk9UX0ZPVU5EIikgewogICAgICAgICAvLyBub3Qgd29ya3MKICAgICAgIH0KICAgfSk7CgogICB0cnkgewogICAgICBhd2FpdCBpbXBvcnQoYC4vZmlsZSR7bnVtIH1gKQogICB9IGNhdGNoIChlcnIpIHsKICAgICAvLyB0aGlzIHdvcmtzCiAgIH0KCn0AAGZpbGUuanMAZXhwb3J0IGRlZmF1bHQgMTsAAGZpbGUyLmpzAGV4cG9ydCBkZWZhdWx0IDE7
Possible Workaround:
add a new glob for dynamic import: