Rewrite __dirname relative to output directory when targeting node
See original GitHub issueAs is, __dirname
isn’t useful if we want to refer to static resources within a node package. One simple solution is to set __outputdir
to be __dirname
as is, and just refer to static resources relative to the output directory.
I also experimented with recovering the original __dirname semantic by calculating it relative to the output directory (assuming that the output directory is nested within the context).
Here’s what I mean in terms of a plugin:
function DirnamePlugin() {
}
DirnamePlugin.prototype.apply = function(compiler) {
compiler.parser.plugin("expression __outputdir", function() {
this.state.current.addVariable("__outputdir", "__dirname");
});
compiler.parser.plugin("expression __moduledir", function() {
const moduleRelativePathFromContext = path.relative(compiler.context, this.state.module.context);
const outputDir = "__dirname";
// TODO: Figoure out the package dir relative to outputdir. Now just assumes it to be the parent of outputdir.
const packageDir = `${outputDir} + '/../'`;
this.state.current.addVariable("__moduledir", `${packageDir} + ${JSON.stringify(moduleRelativePathFromContext)}`);
return true;
});
}
Right now I use __moduledir
to mean __dirname
. Would you consider something like this to be merged into NodeStuffPlugin?
Issue Analytics
- State:
- Created 8 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
path.join vs path.resolve with __dirname - node.js
Is there a difference when using both path. join and path. resolve with __dirname for resolving absolute path in Node. js?
Read more >path.normalize(p) : nodejs API
path.dirname(p) #. Return the directory name of a path. Similar to the Unix dirname command. Example:.
Read more >How To Use __dirname in Node.js
__dirname is an environment variable that tells you the absolute path of the directory containing the currently executing file.
Read more >How to avoid relative path hell in JavaScript / TypeScript projects
There's a need to know where both the current and target module are in the folder structure. This is a big hurdle to...
Read more >file-loader - webpack - JS.ORG
Import (or require ) the target file(s) in one of the bundle's files: ... To get relative path you can use // const...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I solve this issue by using the DefinePlugin plugin
After adding this definition, webpack will not create VAR INJECTION for
__dirname
PS: webpack@2.2.1
Issue was closed because of inactivity.
If you think this is still a valid issue, please file a new issue with additional information.