Open joeldenning opened 3 years ago
This would be a Webpack semantic in how webpack builds CommonJS.
One of the things to note is that __filename
in a bundle is not well-defined because many files with many different __filename
values bundled together into a final bundle will be in a bundle file that has another __filename
value. So then the question is which one of those __filename
values should be used. I don't think any bundlers have a good answer to this from a definition perspective yet. Browserify used to use /local/path.js
as cwd-relative turning into absolute-url-relative paths, but I'm not sure if Webpack fully adopted that convention (//cc @sokra).
So a question to understand further would be - what is the use case you have in mind here that you're looking to handle? Then we can discuss ways to approach that use case rather.
In my case, I worked around it by replacing __filename
with process.cwd()
. The filename was there because it was part of a generated file, and it was used as part of `path.resolve(filename, 'some-other-file.js')`.
Was the generated file an ES module or a CJS module?
The generated file was a CJS module - it was created by sequelize-cli as part of creating a database migration. For reference, here's the original file generated by sequelize-cli:
So basically, we only rewrite __filename
or __dirname
when we are able to analyze what expression they are used in. If we can't do that analysis we leave them as-is and in ESM files they therefore remain undefined.
When building into CommonJS we do exactly the same thing, but __filename
is then defined of course (just wrong effectively since the relative locations will break).
At the end of the day this is about use cases... I would be surprised if the use case was working with the naive CJS version anyway.
Again, regardless of outcome - this is a Webpack concern not an ncc concern. I've posted https://github.com/webpack/webpack/issues/14072 to track further.
Looks like it also mixes up import and require statements in the bundled mjs:
As such its not possible to build with the current version. The version 0.28.x builds it just fine though (only cjs output).
any news on this?
It looks like https://github.com/webpack/webpack/issues/14072 was finally merged and released in webpack@5.90.0
.
The PR to upgrade webpack (https://github.com/vercel/ncc/pull/1158) is currently failing CI so if anyone wants to fix it, I can take a look, thanks!
It looks like webpack/webpack#14072 was finally merged and released in
webpack@5.90.0
.The PR to upgrade webpack (#1158) is currently failing CI so if anyone wants to fix it, I can take a look, thanks!
transfer to https://github.com/vercel/ncc/pull/1177
In a project with
"type": "module"
in the package.json that also contains.cjs
files, the.cjs
files cannot access the global variables__filename
,__dirname
, etc that should be available to commonjs files.I've created https://github.com/joeldenning/ncc-mixed-modules to demonstrate the problem.