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

Easy ES5 ES6 Distinguishability via file extension only #5623

Merged

Conversation

kungfooman
Copy link
Collaborator

This fixes an issue we encounter in #5555 and makes sure that we can use a simple conditional which always works:

ENGINE_PATH.includes('.mjs')

The only workaround otherwise would be to peek into the file via some kind of /...import.../magic regexp, which just increases technical debt while not doing anything more than... figuring out if a file is ES5 or ES6 - and that's exactly the job of the *.mjs file extension 🌈

I confirm I have read the contributing guidelines and signed the Contributor License Agreement.

@mvaligursky
Copy link
Contributor

If you did git add -A . it would be marked as rename, instead of this large diff.

@mvaligursky
Copy link
Contributor

@slimbuck @willeastcott do you foresee any issue with this change?

@kungfooman
Copy link
Collaborator Author

If you did git add -A . it would be marked as rename, instead of this large diff.

Unfortunately not, I just tried it:

image

I could finalize that PR, but it would be the same. Only solution is to break it down in 2 PR's:

  1. Rename src/index.js -> src/index.mjs
  2. Add deprecated src/index.js

@mvaligursky
Copy link
Contributor

have you tried new branch, and first commit doing that? It usually works for me even with small changes to the file.
but not a big deal anyways

@kungfooman
Copy link
Collaborator Author

have you tried new branch, and first commit doing that?

Yep, did a new branch directly from main, only one commit:

https://github.com/kungfooman/playcanvas-engine/commits/easy-es5-es6-distinguishability-git-A

but not a big deal anyways

👍

Copy link
Contributor

@willeastcott willeastcott 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 fine with this. Also wondering whether it makes sense to migrate engine source to .mjs wholesale.

@mvaligursky mvaligursky merged commit d98f6da into playcanvas:main Sep 12, 2023
7 checks passed
@mvaligursky mvaligursky self-assigned this Sep 12, 2023
@epreston
Copy link
Contributor

epreston commented Sep 14, 2023

Sorry to be late to the conversation.

There's no need to do any of this. Need to add "type": "module", to the package.json

This covers node, browsers, tools, etc.

Basic idea is when the source (not the build products) are es6 modules, you mark it in the package.json and all tools work on that assumption.

At first I thought the odd juggling with "mjs" extensions was because half the source was in ES5 but that is not the case. It's just a matter of not marking the project as es6.

It's important to note what the source code is in the package.json regardless, when you do that, none of this is required.

@kungfooman
Copy link
Collaborator Author

There's no need to do any of this. Need to add "type": "module", to the package.json

What you are describing is the ESM algorithm: https://nodejs.org/api/esm.html

image

For whatever reason you dislike part (2) of it - it's there for a reason, if you like it or not.

@epreston
Copy link
Contributor

epreston commented Sep 15, 2023

Nothing to dislike.

Leave some room buy reserving file extensions to get a further, meaningful distinction. Allow for some plain javascript files to be interpreted in multiple ways instead locking it down (so you can force cjs require on plain javascript). Tools are smart enough to look this up once. Making node tools happy in a browser project is not my primary concern.

@kungfooman kungfooman deleted the easy-es5-es6-distinguishability branch May 13, 2024 14:40
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.

4 participants