Closed phanect closed 10 months ago
Hi, @phanect, I also meet the same problem. I think this is expected behavior. As you said. It's hard to handle this problem by bundlers
This is my two methods to get expected path:
// src/main.ts
import { finup } from 'find-up'
process.env.ROOT_PATH = findUpSync('package.json')
// src/template/build.ejs // Some Content
// src/other/file/foo.ts const path = resolve(process.env.ROOT_PATH, 'template/build.ejs')
Hi @peterroe, thanks for your reply :)
I understand it is hard to handle this by unbuild (and maybe esbuild). I will try mkdist. Thanks for the workaround.
I'm closing this Issue.
Environment
Environment of repro (Stackblitz)
Environment of my local PC
Reproduction
https://stackblitz.com/edit/stackblitz-starters-hebbrj
Run
npm start
to build and run the compiled mjs.Describe the bug
When you build a path with Node.js's
path.join()
in a different deepness from the build path (e.g. the generated file is $PROJECT_ROOT/build/main.mjs and the source file is in $PROJECT_ROOT/foo/bar/library.ts),path.join()
returns unexpected path when you join__dirname
and relative path likepath.join(__dirname, '../../../..')
.In the repro, I created a TS file path/to/deep/folder/lib.ts and declare a variable
projectRoot
as follows:const projectRoot = join(__dirname, '../../../..');
The project root is /home/projects/stackblitz-starters-hebbrj, soprojectRoot
should be "/home/projects/stackblitz-starters-hebbrj".I call this
projectRoot
variable from main.ts in the project root and build it with unbuild. However, when I build it and run the generated mjs,projectRoot
points "/".This happened because all the source files are compiled to dist/main.mjs and
join(__dirname, '../../../..')
runs on it. In the built mjs,__dirname
is /home/projects/stackblitz-starters-hebbrj/dist while I expected__dirname
is /home/projects/stackblitz-starters-hebbrj/path/to/deep/folder.I understand it is hard to handle this issue (and I'm not sure if it should be considered as a bug), but I believe it should make a better developer experience if this issue is resolved. Sorry if this is not an issue caused by unbuild but by the bundlers (esbuild or mkdist?).
Additional context
No response
Logs