Skip to content
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

Dependencies on node APIs #1126

Open
pulsejet opened this issue Nov 24, 2023 · 9 comments
Open

Dependencies on node APIs #1126

pulsejet opened this issue Nov 24, 2023 · 9 comments

Comments

@pulsejet
Copy link

Can we please not depend on path? I would really like to get rid of node polyfills.

@susnux
Copy link
Contributor

susnux commented Nov 27, 2023

What is the issue you face? Because bundling those polyfills does not make much sense.
You will always need a bundler, so you can inject the polyfills your self, which will allow treeshaking and smaller bundle size as otherwise the same functions might get included multiple times just because they are bundled with each library.

@pulsejet
Copy link
Author

Actually none of the other nextcloud packages (at least the ones I used) depend on node polyfills, which allowed me to get rid of NodePolyfillPlugin altogether. But then I had to add it back just for this package. If we use something like path-browserify instead that actually targets the browser then there's no need to change the downstream application's webpack config.

@pulsejet
Copy link
Author

pulsejet commented Nov 27, 2023

Also noteworthy: path depends on process so this also adds completely redundant code to the output bundle. The impact may be negligible though.

@pulsejet
Copy link
Author

pulsejet commented Nov 27, 2023

Hmm, the docs of path-browserify claim webpack injects it by default. I'm a bit confused now.

EDIT: I guess this was old documentation. We can still import from path-browserify directly and it seems to work fine

@susnux
Copy link
Contributor

susnux commented Nov 27, 2023

I am still not really convinced this makes sense, as the libraries are build for bundlers and thus you can simply use polyfills there. But especially because there are also some other dependencies that use node modules, and therefor if we bundle the polyfills you have to polyfill the dependencies by your self and thus get the same polyfill injected twice.

Meaning for this library if we include the path polyfills you still need the polyfills in your app config because this library depends on e.g. @nextcloud/files which makes heavy use of the path module.

Directly importing from browserify-path seems to be ok, but you still get that error from the dependencies of this library (and maybe others).

So I would prefer if we can agree on a common practice across @nextcloud/ libraries for handling node modules.

@pulsejet
Copy link
Author

Meaning for this library if we include the path polyfills you still need the polyfills in your app config because this library depends on e.g. @nextcloud/files which makes heavy use of the path module.

Gotcha, didn't know we use it in files too.

So I would prefer if we can agree on a common practice across @nextcloud/ libraries for handling node modules.

Yeah this makes total sense. From my side, the reasons I think we should move away from node polyfilling:

  1. This code is only ever supposed run in the browser, so polyfilling node functionailty is semantically not right. This code is never supposed to run in node, so it makes no sense to use node APIs.
  2. It adds transitive dependencies that we don't need (e.g. process)
  3. (most important) this causes the library to depend on the webpack configuration of the downstream plugin. Further, no idea what happens if the downstream app wants to switch to something other than webpack. The libraries should be as independent as possible.

Need input from more people on this. @skjnldsv any comments?

BTW the webdav client used to depend on node polyfills earlier, but that dependency too is gone in the latest version (they split node and browser modules).

@skjnldsv
Copy link
Contributor

skjnldsv commented Dec 5, 2023

Need input from more people on this. @skjnldsv any comments?

Always up for cleaner approaches. Thought what library do you have in mind for the few path imports we're using (usually normalize, basename, extname, dirname and join)

@pulsejet
Copy link
Author

pulsejet commented Dec 5, 2023

browserify-path should work here, I believe. This is essentially the POSIX part of the node path module, which is the only thing needed in the browser. But the improvement is that it doesn't depend on process, plus no need for the extra polyfill plugin in the downstream webpack config.

@susnux
Copy link
Contributor

susnux commented Dec 5, 2023

Then how about https://www.npmjs.com/package/@nextcloud/paths ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants