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

[feat] auto shim import.meta.url for cjs output #3099

Open
JounQin opened this issue May 2, 2023 · 2 comments
Open

[feat] auto shim import.meta.url for cjs output #3099

JounQin opened this issue May 2, 2023 · 2 comments

Comments

@JounQin
Copy link

JounQin commented May 2, 2023

Like rollup, it shims import.meta.url with the following codes:

(typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('test.js', document.baseURI).href))

Additionally, there is an official rollup plugin @rollup/plugin-esm-shim which adds __filename, __dirname, require support for ESM automatically, maybe it can be adapted as an esbuild plugin also.

@so1ve
Copy link

so1ve commented May 19, 2023

Like rollup, it shims import.meta.url with the following codes:

(typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('test.js', document.baseURI).href))

Additionally, there is an official rollup plugin @rollup/plugin-esm-shim which adds __filename, __dirname, require support for ESM automatically, maybe it can be adapted as an esbuild plugin also.

Out of scope, just curious: Why rollup uses require('u' + 'rl') instead of a direct require?

@hyrious
Copy link

hyrious commented May 19, 2023

Why rollup uses require('u' + 'rl') instead of a direct require?

Because certain bundlers (including Rollup itself) won't try to bundle 'url' when it is not a trivial string.

$ echo 'require("url")' > a.js
$ npx rollup a.js -p commonjs

a.js  stdout...
import 'url';

var a = {};

export { a as default };
(!) Unresolved dependencies
https://rollupjs.org/troubleshooting/#warning-treating-module-as-external-dependency
url (imported by "url?commonjs-external")
$ echo 'require("u" + "rl")' > a.js
$ npx rollup a.js -p commonjs

a.js  stdout...
function commonjsRequire(path) {
	throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');
}

var a = {};

commonjsRequire("u" + "rl");

export { a as default };

However, this technique is not applied to esbuild -- https://esbuild.github.io/api/#non-analyzable-imports, esbuild can bundle 'url' in this form. 🤷

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

3 participants