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

Investigate if .cjs extension support is needed. #68

Closed
phated opened this issue Jun 3, 2020 · 8 comments · Fixed by #75
Closed

Investigate if .cjs extension support is needed. #68

phated opened this issue Jun 3, 2020 · 8 comments · Fixed by #75

Comments

@phated
Copy link
Member

phated commented Jun 3, 2020

From #59, we believed that .cjs extension support was needed, but I think that was due to not properly supporting the ESM syntax. With the work done in gulp-cli by @snoak, we can import ESM modules.

Given that, I only added support for .mjs in #65 and I wanted to open this to investigate if we actually need to support the .cjs extension.

If we end up needing to support it, I've put in place the architecture to support & test the extension on all versions of node.

@snoack
Copy link

snoack commented Jun 3, 2020

Node.js considers a script an ES module if it either has the file extension .mjs, or if it has the file extension .js and is located in a package that declares "type": "module" in its package.json.

Node.js considers a script a CommonJS module if it either has the file extension .cjs, or if it has the file extension .js and is not located in a package that declares "type": "module" in its package.json.

So if a package uses "type": "module" but for whatever reason sticks to CommonJS for their Gulpfile that file would have to be named gulpfile.cjs.

@phated
Copy link
Member Author

phated commented Jun 3, 2020

Thanks for the additional details! That should help us make a decision.

but for whatever reason sticks to CommonJS for their Gulpfile

My assumption was that we didn't support ESM with "type": "module", so they needed to keep their gulpfile in CommonJS. If that's the only reason, I don't see a need to support the .cjs extension. However, I'll keep this open to see if someone has a use-case for it with the new ESM support.

@snoack
Copy link

snoack commented Jun 3, 2020

I don't think this is true. We call require() first and if it fails with ERR_REQUIRE_ESM we fallback to import().

So if there is a gulpfile.js in a package that declares "type": "module", require() will fail because it considers it an ES module which cannot be loaded that way, but import import() should succeed if the script uses valid ESM semantics.

However, if there is a gulpfile.js using CommonJS semantics in a package that declares "type": "module", require() will fail because it considers it an ES module which cannot be loaded that way, then import() will fail when the script attempts to access require, exports or module.exports which don't exist in ES modules.

@anshumanv
Copy link

Hey 👋

Thanks for the support for .mjs config, over at webpack-cli we use interpret to use various formats of webpack config files as per the extensions supplied by interpret, is it possible to add .cjs to the extension list?

@phated
Copy link
Member Author

phated commented Aug 2, 2020

@anshumanv Can you explain a little more? node.js supports "fall through" when requiring files that don't have an extension registered, so wouldn't .cjs just be loaded normally as a .js file?

@anshumanv
Copy link

Hey @phated yes, so we use interpret to get webpack configs with valid extension whether .ts, .js etc so whatever configs are supplied via --config flag to the cli, we check if they have a valid extension from interpret - https://github.com/webpack/webpack-cli/blob/a380a785c296208af7017f547cd34cf72517f9da/packages/webpack-cli/lib/groups/ConfigGroup.js#L46 and filter out the ones which are not, so since .cjs is not listed in the extension list, it's not recognized as a valid config. Is there any other approach that I can take? To me simple solution seems to list it in the extensions list.

@phated
Copy link
Member Author

phated commented Aug 5, 2020

Oh, that's much different than the way gulp uses list library. We use the rechoir library to prepare the environment with the loaders and I believe it allows any unknown extension to fallthrough to the default node loader.

@anshumanv
Copy link

Ah okay, I think it makes a lot of sense to fallback to default node loader. I've opted for the same approach 👍 Thanks @phated 💯

nicolo-ribaudo added a commit to nicolo-ribaudo/vscode-icons that referenced this issue Dec 19, 2020
Gulp supports the `.mjs` file extension for config files (tracked at gulpjs/interpret#65).
It looks like `.cjs` is not supported yet (gulpjs/interpret#68).
robertohuertasm pushed a commit to vscode-icons/vscode-icons that referenced this issue Jan 1, 2021
Gulp supports the `.mjs` file extension for config files (tracked at gulpjs/interpret#65).
It looks like `.cjs` is not supported yet (gulpjs/interpret#68).
@phated phated linked a pull request Nov 29, 2021 that will close this issue
@phated phated closed this as completed in #75 Apr 7, 2022
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 a pull request may close this issue.

3 participants