-
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
can add support for __dirname and __filename? #859
Comments
This also applies to |
It's possible to implement this with a plugin. You can write an on-load plugin that injects
There are legitimate use cases for referencing the final location of the bundle instead of the location of the source code. For example, it's a common practice to use |
maybe add native support for this and add option? |
I think my preferred solution for this is going to be to use the This is a highly custom feature request so using a plugin for this instead of having it be built in seems like the way to go. It may seem like this is a simple request. However, replacing |
but __dirname and __filename have a specification:
can implement it if target is a node? |
I think this should be a plugin or an opt-in. I personally wouldn't want a bundler to replace |
I just ran into this myself. As someone who is experimenting in Node.js, it makes sense that For my use case, this solved my problem: @mulfyx makes a good point but I agree with @kzc that this should probably be implemented in userland / as a plugin. The only problem about defining it as a side-effect of Maybe esbuild should have a few ‘official, first-party’ plugins that fills this need so that the community doesn’t have to worry about this in the future? |
This only works as long as the application is launched at the root of the project. |
This is what i did. This plugin replaces __dirname and/or __filename with the correct values
|
Thanks @richarddd
|
The big problem with using an onLoad plugin for doing this or injecting other things is that it alters the source map output. There should be an option to add per-file banner or defines that is ignored generating the source map |
FWIW, es modules in node don't support __dirname or __filename either, with |
|
Someone created a plugin just for this: but it does not work for lambda. |
I will publish the little thing @richarddd posted as it's own NPM module today, will try to get it merged into |
maybe it is possible to transform it into paths related to actual file directory? e.g. esbuild --bundle foo.js --platform=node --outfile=foo.bundle.js // foo.js
require('./bar/baz.js')
console.log(__dirname) // keep using __dirname as it's the same dir of entrypoint
// bar/baz.js
console.log(__dirname) // => transform __dirname to (__dirname+'/bar') And I think that satisfies most situations. (Maybe you are using something like |
Thanks a lot for this! Solved a problem I was having with My import { build, Loader, PluginBuild } from 'esbuild';
import { readFileSync } from 'fs';
import { extname, dirname as _dirname } from 'path';
const nodeModules = new RegExp(
/^(?:.*[\\/])?node_modules(?:\/(?!postgres-migrations).*)?$/
);
const dirnamePlugin = {
name: 'dirname',
setup(build: PluginBuild) {
build.onLoad({ filter: /.*/ }, ({ path: filePath }) => {
if (!filePath.match(nodeModules)) {
let contents = readFileSync(filePath, 'utf8');
const loader = extname(filePath).substring(1) as Loader;
const dirname = _dirname(filePath);
contents = contents
.replace('__dirname', `"${dirname}"`)
.replace('__filename', `"${filePath}"`);
return {
contents,
loader,
};
}
});
},
};
void build({
entryPoints: ['src/index.ts'],
bundle: true,
minify: true,
sourcemap: true,
outfile: 'dist/index.js',
platform: 'node',
format: 'cjs',
external: ['pg-native'],
plugins: [dirnamePlugin],
}); |
@mishabruml Thanks for the snippet, const dirnamePlugin = {
name: "dirname",
setup(build) {
build.onLoad({ filter: /.*/ }, ({ path: filePath }) => {
if (!filePath.match(nodeModules)) {
let contents = fs.readFileSync(filePath, "utf8");
const loader = path.extname(filePath).substring(1);
const dirname = path.dirname(filePath);
contents = contents
.replaceAll("__dirname", `"${dirname}"`)
.replaceAll("__filename", `"${filePath}"`);
return {
contents,
loader,
};
}
});
},
}; |
The plugin approach did not work for me. In this particular case, I must also use thomaschaaf/esbuild-plugin-tsc — because this project uses decorators which esbuild refuses to support. Perhaps there was some interference between the 2 plugins. I ended up ditching the This is of course very annoying, as |
Note that the
|
example:
file1.js
file2.js
build:
and run:
expect output:
actual output:
the file 'build.js' looks like this:
why not replace __filename with the path to the file?
The text was updated successfully, but these errors were encountered: