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

Add .cjs as list of potential JS files #75

Merged
merged 9 commits into from
Apr 7, 2022
Merged

Add .cjs as list of potential JS files #75

merged 9 commits into from
Apr 7, 2022

Conversation

crutchcorn
Copy link
Contributor

When using type="module", there's still the ability to use commonjs style require and module.exports in .cjs files (as opposed to .js files).

This PR adds it as a candidate for files that can be handled using Liftoff

I've tested this within our close-to-release plopjs@3 release, which will support ESM

@phated
Copy link
Member

phated commented Nov 29, 2021

I think this makes sense. However, in #68, we identified that falling back to node's loader will load the cjs file anyway. Can you explain a little more how plopjs loads things so I can understand why falling back doesn't work?

@crutchcorn
Copy link
Contributor Author

Whoops! I missed #68, my bad.

For context, I'm using an await import here:

https://github.com/plopjs/node-plop/blob/master/src/node-plop.js#L198

Where plop then passes the configfile directly from Liftoff

https://github.com/plopjs/plop/blob/master/src/plop.js#L18-L24

https://github.com/plopjs/plop/blob/master/src/plop.js#L56-L59

However, when I was working on CLI E2E tests, I ran into issues with CJS files loading properly

https://github.com/plopjs/plop/blob/master/tests/esm.spec.js#L30-L39

This test currently passes, but if I remove the cjs config extension from here:

https://github.com/plopjs/plop/blob/master/src/plop.js#L22

This test then fails

@phated
Copy link
Member

phated commented Nov 29, 2021

Well, I'm convinced! I'm happy to have a real-world use case to prove that it is needed. I'll get this merged and released today.

@phated phated linked an issue Nov 29, 2021 that may be closed by this pull request
@phated
Copy link
Member

phated commented Nov 29, 2021

@crutchcorn I'm trying to figure out how to write a test for this right now.

@crutchcorn
Copy link
Contributor Author

@phated gotcha! I can see about adding them if that'd be helpful

Far from any rush though, we've already published v3 and it's working rather well with the extension changes :)

@phated
Copy link
Member

phated commented Nov 29, 2021

That'd be a ton of help! I've not done much with "type": "module" stuff. It seems the issue is that the .cjs extension doesn't exist in the commonjs context, so we might need to stub it the same way we stub mjs.

@phated
Copy link
Member

phated commented Dec 29, 2021

@crutchcorn do you still have some availability to get tests written for this?

@crutchcorn
Copy link
Contributor Author

crutchcorn commented Dec 29, 2021

I took a quick stab and I'll admit that I don't understand the testing methodologies at play well enough to move forward.

Admittedly, the tests should look similar to mjs I think. The gist is basically:

  1. CJS can be imported from anything (require in non-ESM files, import in ESM), regardless of type field values in package.json
  2. CJS can use module.export (and other CJS) in type="module" projects
  3. CJS should throw an error when using ESM exports and such, regardless of type field values in package.json

@phated
Copy link
Member

phated commented Dec 29, 2021

@crutchcorn the issue, if I remember, is that our test harness is running under commonjs, so we can never test the cjs extension. I think that you'll need to exec some script in a different directory that enables the module context

@phated
Copy link
Member

phated commented Apr 7, 2022

@crutchcorn I rebased this and added some tests. Can you sanity check them for me to make sure this is what you expect?

@phated
Copy link
Member

phated commented Apr 7, 2022

Tests are passing. Just waiting on @crutchcorn's feedback 😄

@crutchcorn
Copy link
Contributor Author

Tests LGTM!

@phated phated merged commit 7989161 into gulpjs:master Apr 7, 2022
@phated
Copy link
Member

phated commented Apr 7, 2022

Thanks for getting this started and checking out the tests! Sorry it has taken so long to get released (it'll ship with the 3.0 changes)

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.

Investigate if .cjs extension support is needed.
3 participants