Skip to content

Add support for mixed ESM and CJS via mixedModules flag #26

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

Merged
merged 6 commits into from
Jul 24, 2019

Conversation

styfle
Copy link
Member

@styfle styfle commented Jul 22, 2019

This adds support for mixing ESM and CJS so that we have feature parity with ncc, which is basically webpack.

This will fix all the issues that were reported in the latest release:

Update

I spoke with Timer and it sounds like Next.js might want to follow the Node.js behavior. If that's the case maybe we don't want this PR to land. We need to discuss with the whole team.

Perhaps we should enable backwards compatibility through a config flag like mixedModules: true. That way user's who have broken deployments can opt-in to this behavior.

@styfle styfle changed the title Mixded ESM and CJS Mixed ESM and CJS Jul 22, 2019
@styfle styfle changed the title Mixed ESM and CJS Add support for Mixed ESM and CJS Jul 22, 2019
@styfle styfle changed the title Add support for Mixed ESM and CJS Add support for mixed ESM and CJS Jul 22, 2019
@styfle styfle added the enhancement New feature or request label Jul 22, 2019
@@ -1,3 +0,0 @@
[
"test/unit/require-esm/input.js"
]
Copy link
Member Author

@styfle styfle Jul 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted this test because it was contrary to the new behavior. Instead I created a new test.

@styfle styfle marked this pull request as ready for review July 22, 2019 22:31
@styfle styfle requested a review from guybedford as a code owner July 22, 2019 22:31
@styfle styfle requested review from lucleray and leo July 22, 2019 22:32
@styfle styfle requested review from guybedford and Timer July 22, 2019 22:47
@codecov-io
Copy link

codecov-io commented Jul 23, 2019

Codecov Report

Merging #26 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #26      +/-   ##
==========================================
+ Coverage   89.88%   89.89%   +0.01%     
==========================================
  Files          10       10              
  Lines         979      980       +1     
==========================================
+ Hits          880      881       +1     
  Misses         99       99
Impacted Files Coverage Δ
src/node-file-trace.js 90.64% <100%> (+0.06%) ⬆️
src/analyze.js 96.15% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e707318...5a4b53c. Read the comment docs.

Copy link
Member

@Timer Timer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is completely non-standard behavior and shouldn't be allowed. The provided user code in the referenced links is not valid JavaScript.

Mixing CommonJS isn't allowed by Webpack, Node, or and standards body.

I'm not in favor of allowing this, and I'm sorry that Next.js ever supported this.
This was a side-effect of Next.js fixing a pre-Babel-v7 bug.

Also, I don't believe ncc ever supported this behavior. The only behavior ncc should've supported was a ES Modules file requiring a CommonJS file, and vice-versa. Not mixing the two within a single file.

@styfle
Copy link
Member Author

styfle commented Jul 23, 2019

@Timer I just confirmed that ncc works with the unit test I added.

cd test/unit/mixed-esm-cjs
ncc build input.js
node dist

This change is necessary to maintain backwards compatibility. I would be open to using a flag to opt-in to this non-standard behavior if you prefer.

@Timer
Copy link
Member

Timer commented Jul 23, 2019

Ah, this is because ncc uses webpack's legacy javascript/auto source handling method instead of javascript/esm or javascript/dynamic.

Looks like we might be stuck supporting this behavior until Node 12 is LTS with officially defined behavior.

@styfle styfle changed the title Add support for mixed ESM and CJS Add support for mixed ESM and CJS via mixedModules flag Jul 23, 2019
@styfle
Copy link
Member Author

styfle commented Jul 23, 2019

@Timer I added a flag mixedModules so that we can opt-in to this "legacy" behavior.

@styfle styfle requested a review from Timer July 23, 2019 17:32
@styfle styfle merged commit bee1cd2 into master Jul 24, 2019
@styfle styfle deleted the mixed-esm-cjs branch July 24, 2019 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants