-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
import.meta.url support #1492
Comments
A quick plugin to replace {
name: 'import.meta.url',
setup({ onLoad }) {
let fs = require('fs')
let url = require('url')
// TODO: change /()/ to smaller range
onLoad({ filter: /()/, namespace: 'file' }, args => {
let code = fs.readFileSync(args.path, 'utf8')
code = code.replace(
/\bimport\.meta\.url\b/g,
JSON.stringify(url.pathToFileURL(args.path))
)
return { contents: code }
})
}
} |
Thanks for the advice) |
@GenrikhFetischev |
Tried to apply @hyrious plugin, but I got another error:
Solved case by using Yargs package has two different entry points for |
No, it seems a bug in yargs itself. Try this vanilla esm code: import yargs from "yargs"
console.log(yargs, yargs.command)
// save as `a.mjs`
// run with `node a.mjs` It gives [Function (anonymous)] undefined maybe you should track this issue: yargs/yargs#1844 |
You can replace things like this with arbitrary run-time values using the inject and define features. Here's an example:
This defines |
thanks, @evanw. What you've laid out in your last comment seems to have fixed it for me. Out of curiosity: Will |
Wasn't support for Is this issue specific to `--bundle? My config is: void esbuild.build({
entryPoints: [
'source/background.ts',
'source/options.tsx',
'source/resolve-conflicts.ts',
'source/refined-github.ts',
],
bundle: true,
watch: process.argv[2] === '--watch',
outdir: 'distribution/build',
external: ['chrome:*'],
plugins: [readmeLoader],
}); |
@fregante You got empty Since your context is browser extension, maybe the solution above does not work. We should look in webpack to see what it does. Update: I looked into the original output, and find that it is just replaced to __filename (the absolute path to the source file). So maybe my first comment just did this work for you. |
Gotcha, but I hope it will match other bundlers' behavior though. I already tried your plugin but it broke TypeScript support (i.e. esbuild complained about TS syntax) |
@fregante This is because the default loader is |
I have encontered that error too. And thank your guys the |
@xkn1ght The which file to be bundled is determined by how code import it, esbuild is somewhat align to Node.js's native es modules' behavior. Since you write Yargs does have a bug that the esm export is not sharing the same interface as cjs one. I'd recommend you to write a simple plugin to force redirect imports to yargs to its cjs version if you already has some code depends on that. |
thanks~ |
if in ts project: const excludeImportMetaUrl = (): Plugin => {
return {
name: 'exclude-import-meta-url',
setup(build) {
build.onLoad({ filter: /node-runner.ts$/ }, async (args) => {
const contents = await fs.readFile(args.path, 'utf8');
const transformedContents = contents.replace(
/import\.meta\.url/g,
JSON.stringify(url.pathToFileURL(args.path)),
);
console.log(contents);
return { contents: transformedContents, loader: 'ts' };
});
},
};
}; |
import.meta.url is not supported in prominent build tools yet, so we use a different approach: vitejs/vite#8427 evanw/esbuild#1492
The new
Running with $ npx tsx dirname-filename.ts
undefined undefined Unconventional Workaround (for
|
It's a critical bug imho |
This issue is practically resolved via the solution explained above using @karlhorky This issue is unrelated to why it's not working for you with tsx. The reason why it's not working in your code is because you're running the code in CommonJS mode. You have to add |
Hi!
I have a problem with the bundling yargs package. When I try to run bundled code I get the following error:
Looks like it happens cause of the following code (github):
Which transpiled into:
import_meta.url
- obviouslyundefined
there.I try to make a bundle for node.js with the flag
--format=cjs
I have checked out this issue - #208, but looks like my isn't duplicate because I try to bundle ESM -> CJS;
Is it possible to add support
import.meta.url
property for ESM -> CJS bundle?The text was updated successfully, but these errors were encountered: