-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Rewrite __dirname
/__filename
to be relative from asset.filePath
#7611
Comments
But process.chdir("/something/else");
fs.readFile(`${__dirname}/foo/bar`) -> process.chdir("/something/else");
fs.readFile(`${"../.."}/foo/bar`) // now reads `/foo/bar` Whereas I would have expected process.chdir("/something/else");
fs.readFile(`${__dirname + "/../.."}/foo/bar`) |
Thanks for the note, I've added an example use case why I added the initial expected behavior as the projectRoot is the cwd there. But I guess this would also work with what you have in mind. |
__dirname
to be relative from cwd
__dirname
to be relative from options.projectRoot
__dirname
to be relative from options.projectRoot
__dirname
/__filename
to be relative from options.projectRoot
I guess this is breaking the new TypeScript configs in Gatsby |
__dirname
/__filename
to be relative from options.projectRoot
__dirname
/__filename
to be relative from asset.filePath
Is there an option to revert this to the old behavior? Now my electron-main references to __dirname have the wrong file path in them (appends .../.../src/ to the __dirname). |
@rihok Could you make a small pre-production of that? 🙏 |
Sure. I compile preload.js separately, so in the main process I do something like this: // main.ts
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 800,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
});
} but that gets compiled into |
So how are we supposed to get the path to the current file after compilation now? |
In the above case, you could possibly do something like this: import {fileURLToPath} from 'url';
fileURLToPath(new URL('preload.js', import.meta.url)); This will also have the effect of creating a dependency on Are there other use cases where that wouldn't work? |
The file that loads the preload script is the Other cases are also when you try to write a file in the directory of the script. fs.writeFile(__dirname + 'whatever/today.log'); |
Doesn't quite work; seeks the wrong directories because of this: parcel-bundler/parcel#7611
Hello, |
🙋 feature request
This is a more detailed report of #5090 and one that I put up after following some private conversions with the Parcel Core Team to discuss the specifics in the open.
This feature (request) addressed one bulletpoint here: #2493
🤔 Expected Behavior
file.ts
:Gets compiled with Parcel to JS into the directory
.cache/compiled
..cache/compiled/file.js
:😯 Current Behavior
file.ts
:.cache/compiled/file.js
:💁 Possible Solution
First step would be to set a default for
node
target. That could/would be:Treat it as relative from
options.projectRoot
(what is shown in the expected behavior example).So for example: If
outputDir
is.cache/compiled
and currentcwd()
/projectRoot is/
the relative path to it would be../../
In a next step options similar to https://webpack.js.org/configuration/node/#node__dirname could be added.
🔦 Context
This might also impact #6925 maybe?
Example Use Case
/usr/project/file.ts
:/usr/project/.cache/compiled/file.js
:/usr/project
is then the projectRoot because the rest of the scripts runs from this root directory and then thefile.js
is accessed.The text was updated successfully, but these errors were encountered: