Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

JadedBlueEyes
Copy link

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:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
    • (I'm not sure adding esbuild for just one test is a great idea)

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

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.
@changeset-bot
Copy link

changeset-bot bot commented Aug 5, 2022

⚠️ No Changeset found

Latest commit: 0d05ab6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Member

@benmccann benmccann left a 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));
Copy link
Member

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?

Suggested change
const __dirname = __dirname || path.dirname(fileURLToPath(import.meta.url));
const __dirname = globalThis.__dirname || path.dirname(fileURLToPath(import.meta.url));

@Rich-Harris
Copy link
Member

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?

@JadedBlueEyes
Copy link
Author

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.
Directly copying the build folder did not work, for two reasons:

  • NodeJS does not allow using ES modules by default without "type": "module" in a package.json
  • The dependencies are not bundled
    • node_modules are huge, and include all dev dependences, with no way to separate them out without deleting and reinstalling production only.

I discovered the second one only after automating copying a package json into the build :(.
Of course, the obvious solution - bundle, transpile and treeshake it all!I used ESBuild, and it worked great first try, except for the one line of code here - it couldn't deal with import.meta.url, and when running the code with node, it errored out.

After much fiddling and trial and error, I came up with this:
@[email protected]

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 ("main" I think?). There's probably a way to account for this, or a better way of doing it in general, but this worked for me.

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.

@benmccann
Copy link
Member

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

@benmccann benmccann closed this Aug 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants