Webpack-cli: support `webpack.config.cjs` by default

Created on 9 Jan 2020  路  22Comments  路  Source: webpack/webpack-cli

If a webpack.config.js is present, the webpack command picks it up by default. We use the --config option here only to show that you can pass a config of any name. This will be useful for more complex configurations that need to be split into multiple files.

We should also pick it up when a webpack.config.cjs is present

Feature

Most helpful comment

^
+

Alternative solution: wondering if replacing the extension '.cjs' with '.js' is a better solution?

Nope, as stated above they are interpreted differently, so not a good solution.

All 22 comments

This requires a minimum version of node right?

No, we can just regard cjs as js

Seems like we can't just support it - https://github.com/gulpjs/interpret/issues/65

Actually, you can, as .cjs can be used anywhere a .js file can be used currently, which both the docs and the source code confirm.

Hi. Can I take up this issue? @ematipico

Hi. I have made some changes, how do I test that the changes resolve the issue?
Specifically, I have made changes to the 'packages/webpack-cli/lib/groups/configGroup.js' file to include support for '.cjs' file. Thanks.

how do I test that the changes resolve the issue

@Parikshit-Hooda As this is a new feature you are implementing you have to write your own tests.

Also, you can run the current tests with yarn test

@snitin315 Actually, I want to confirm if the feature addition actually fixes this issue or not? I imagine that the modified repo(fixed webpack-cli) will need to be imported in a dummy project and check. How do I go about checking this?

you can use npm link or yarn link

Haven't done this kind of an activity before. Can you please elaborate or tell me what exactly should I google?

Haven't done this kind of an activity before. Can you please elaborate or tell me what exactly should I google?

You need to run your changes locally, https://docs.npmjs.com/cli/link.html
Link the package and try to run with a .cjs config file then write tests for it.

Question: Should I link the repo after following the contributing.md and making edits or without following the instructions in CONTRIBUTING.md. I am asking because I am not sure if going through yarn install ->bootstrap -> build causes some unrequired changes.

build causes some unrequired changes.

What are the unrequired changes?

Yep please install, bootstrap, build then link the webpack-cli package in the packages folder.

Then you need to npm link webpack-cli in a folder, that folder will then use the local code that you have.

Question: Should I link the repo after following the contributing.md and making edits or without following the instructions in CONTRIBUTING.md.

Yes, after following the steps.

Capture
After linking it, its still requiring me to 'npm install webpack-cli' to run the npm run build. What am I doing wrong?

Invoke using webpack-cli, after you run npm link webpack-cli in your test folder.

Doesn't seem to work.
$ webpack-cli --entry src/index.js --config webpack.config.cjs
bash: webpack-cli: command not found

You didn't symlink properly then.

got it. will raise a PR in a couple of days.

Alternative solution: wondering if replacing the extension '.cjs' with '.js' is a better solution?
Reason: I was going through the file that is to be edited and 'webpack.config.js' is actually used as a primitive default variable and the variable scope is dispersed and large. changing the extension is hence a simpler solution. Also, if anybody has a suggestion which files actually need to be inspected for this feature, do let me know!

.js聽is聽parsed as聽an聽ECMAScript聽module when聽package.json has聽type:聽"module"聽specified.

.cjs聽is聽always聽parsed as聽a聽CommonJS聽module, which聽is the聽only module聽format which聽webpack currently聽supports for聽its聽configuration聽files.

^
+

Alternative solution: wondering if replacing the extension '.cjs' with '.js' is a better solution?

Nope, as stated above they are interpreted differently, so not a good solution.

.js is parsed as an ECMAScript module when package.json has type: "module" specified.

.cjs is always parsed as a CommonJS module, which is the only module format which webpack currently supports for its configuration files.

Hey yall. Is it currently planned to support esm for config files? I guess it would raise the minimum node version way too high right?

Was this page helpful?
0 / 5 - 0 ratings