-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix hard dependance on ES modules #5832
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
Conversation
Previously, __dirname was redefined to a value derived from import.meta.url. This meant when the adapter was transpiled to commonJS, __dirname would be undefined. Now it only redefined if it does not have a truthy value.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious if there's a reason you can't use ES modules. Is there something else in the ecosystem that needs to be addressed?
@@ -19,7 +19,7 @@ const address_header = env('ADDRESS_HEADER', '').toLowerCase(); | |||
const protocol_header = env('PROTOCOL_HEADER', '').toLowerCase(); | |||
const host_header = env('HOST_HEADER', 'host').toLowerCase(); | |||
|
|||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | |||
const __dirname = __dirname || path.dirname(fileURLToPath(import.meta.url)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is failing lint
. will the following work?
const __dirname = __dirname || path.dirname(fileURLToPath(import.meta.url)); | |
const __dirname = globalThis.__dirname || path.dirname(fileURLToPath(import.meta.url)); |
Thanks, though as noted above this definitely isn't the right fix as it's invalid JS. Can you open an issue with a reproduction please? |
Sorry about leaving this for so long - I had a bit of a busy couple of weeks. So - the story for this. I've been building a SvelteKit app that needs to run on a server with a bit of a 'pet' set up that runs a bunch of other webapps on it. I wanted to run it in a way that I could upload a single JS file to the server for easier updates. Unfortunately, the /client/ and /static/ directories can't be all bundled into one file. I compromised a bit to wanting a small, quick upload to a single folder.
I discovered the second one only after automating copying a package json into the build :(. After much fiddling and trial and error, I came up with this: diff --git a/files/handler-393b2283.js b/files/handler-393b2283.js
index c5588bfa8e4eb54fed732724e95aa6dab677a944..c6a50d6de94641f8e7f71eddaf704b48017d190c 100644
--- a/files/handler-393b2283.js
+++ b/files/handler-393b2283.js
@@ -1052,7 +1052,14 @@ const address_header = env('ADDRESS_HEADER', '').toLowerCase();
const protocol_header = env('PROTOCOL_HEADER', '').toLowerCase();
const host_header = env('HOST_HEADER', 'host').toLowerCase();
-const __dirname = path.dirname(fileURLToPath(import.meta.url));
+let __dirname;
+try {
+ __dirname = path.dirname(fileURLToPath(import.meta.url))
+} catch (e) {
+ try {
+ __dirname = path.dirname(resolve(".", process.argv[1] ))
+ } catch (e) { }
+}
/**
* @param {string} path With the caveat that node must be run with the filename to be executed rather than the folder, defaulting to index,js or the settings in package.json ( Again, apologies for leaving this so long, and for my writing skills. I also did this all via the web interface, so sorry for not running all the scripts as well. |
Your latest proposed patch is even less likely to be merged, so I'm going to go ahead and close this. If you're just trying to bundle dependencies, you can do something like this: #3176 (comment) Otherwise, you should use the custom server option: https://github.com/sveltejs/kit/tree/master/packages/adapter-node#custom-server |
Previously, __dirname was redefined to a value derived from import.meta.url. This meant when the adapter was transpiled to commonJS, __dirname would be undefined, crashing the server. Now it only redefined if it does not have a truthy value.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0