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

Suggetion: allow to specify external's type (esm or commonjs) #1915

Closed
hronro opened this issue Jan 6, 2022 · 2 comments
Closed

Suggetion: allow to specify external's type (esm or commonjs) #1915

hronro opened this issue Jan 6, 2022 · 2 comments

Comments

@hronro
Copy link

hronro commented Jan 6, 2022

Say we are trying bundle a module write in commonjs:

echo 'module.exports = require("bar").default + 100;' > foo.js
esbuild foo.js --bundle --format=esm --external:bar --platform=browser

The format of bar module is ESM and eventually we will made the bar module available in the browser using import-maps like this:

<script type="importmap">
{
	"imports": {
		"bar": "https://example.com/bar.js"
	}
}
</script>

However, currently esbuild will output something like __require("bar").default, which will cause a runtime error.

I guess esbuild is treating bar as a commonjs module, so using __require make sense.

So I wonder if we can specify the external's type, so esbuild will finally generate proper imports for the ESM externals.

If it's in a config file, it should be something like this:

{
    externals: ["foo", { name: "bar", type: "esm" }]
}

But I'm not sure how it could be done with a CLI argument.

@hronro
Copy link
Author

hronro commented Jan 11, 2022

After some thought, I think what I mentioned in #1927 might be better.

@watsonsong
Copy link

Is this suggestion has any solution?
I want to use esm but import some external with commonjs.

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

2 participants