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

Build CommonJS module along with ESM and IIFE #4512

Merged
merged 1 commit into from Sep 30, 2021

Conversation

hubertlepicki
Copy link
Contributor

@hubertlepicki hubertlepicki commented Sep 28, 2021

I honestly don't know if this is the best approach but I am quite stuck with trying to upgrade to Phoenix 1.6 on a fairly large project that uses "Jest" for testing of their JavaScript code (elixirforum thread here: https://elixirforum.com/t/phoenix-1-6-0-npm-package-cannot-be-imported-in-jest/42670/4)

I tried various hacks and third party tools suggested on Jest thread but I wasted a whole day and fixing one thing breaks another, typical Node.js developer story. So, I decided to build CJS package of phoenix.js that Jest can understand like it used to up to 1.5.9.

Now I suspect there was a good reason for not including CJS file, but maybe it's just an omission - for the later case I provide this PR below. Note: in this project "phoenix" is the only package that doesn't provide CJS fallback, many other packages do ship with CJS and EMS and I tried to follow the pattern here.

esbuild can build CJS modules as well as ESM, and for the people using
build tools / or testing tools (such as Jest) that do not yet support
ESM modules, it is useful to have fallback when ESM is not availalbe.

This PR includes command to build CJS module along with currently
supported types, and adds entry "main" in package.json to point towards
that file.

This has been tested with Jest, which currently has very poor support
for ESM modules and allows for including "phoenix" in tests and also
stubbing it if needed.

@mveytsman
Copy link
Member

This looks good by me, although the two compiled files in priv/static should be taken out of this PR and built as part of the release cycle.

esbuild can build CJS modules as well as ESM, and for the people using
build tools / or testing tools (such as Jest) that do not yet support
ESM modules, it is useful to have fallback when ESM is not availalbe.

This PR includes command to build CJS module along with currently
supported types, and adds entry "main" in package.json to point towards
that file.

This has been tested with Jest, which currently has very poor support
for ESM modules and allows for including "phoenix" in tests and also
stubbing it if needed.
@hubertlepicki
Copy link
Contributor Author

@mveytsman just removed those from the PR.

@hubertlepicki
Copy link
Contributor Author

So @mveytsman I've been thinking of it and I'm not sure if this should be built as part of release cycle or earlier - when this PR is being merged. If we did the later, then the CJS file could be used intermediately by folks pointing to GH repo master branch, and that'd be some extra testing + we have copies of the other versions of phoenix.js already commited into master as well, so it'd be consistent. Either way it's fine by me.

@mcrumm
Copy link
Member

mcrumm commented Sep 29, 2021

@mveytsman does this fix #4506?

@hubertlepicki The assets will be built when the PR is merged. Max just meant that, as a security measure, the build needs to be done by someone on the Phoenix team :)

@hubertlepicki
Copy link
Contributor Author

hubertlepicki commented Sep 29, 2021 via email

@hubertlepicki
Copy link
Contributor Author

@mcrumm I wonder if I (or you if you want to do it yourself) should prepare a branch to run by those folks and see if their issues are resolved when pointing to that branch? This is, if this PR is not going to be merged to master shortly, otherwise they can just point to master to test.

tcitworld added a commit to framasoft/mobilizon that referenced this pull request Sep 29, 2021
@mveytsman
Copy link
Member

@hubertlepicki yeah I agree - the file doesn't only need to be built on releases. I more wanted to emphasize that we should keep changes to built artifacts out of PRs like this and as part of an action taken by a team member.

@mcrumm I have not tested but I believe that should fix that!

@mcrumm mcrumm merged commit 8831f7a into phoenixframework:master Sep 30, 2021
@mcrumm
Copy link
Member

mcrumm commented Sep 30, 2021

Thanks! 💚💚💚

chrismccord pushed a commit that referenced this pull request Oct 8, 2021
esbuild can build CJS modules as well as ESM, and for the people using
build tools / or testing tools (such as Jest) that do not yet support
ESM modules, it is useful to have fallback when ESM is not availalbe.

This PR includes command to build CJS module along with currently
supported types, and adds entry "main" in package.json to point towards
that file.

This has been tested with Jest, which currently has very poor support
for ESM modules and allows for including "phoenix" in tests and also
stubbing it if needed.
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

Successfully merging this pull request may close these issues.

None yet

3 participants