Skip to content

Commit

Permalink
feat: add support for .cjs config (#1727)
Browse files Browse the repository at this point in the history
* feat: support .cjs config file

* feat: support .cjs config file

* feat: support .cjs config file
  • Loading branch information
anshumanv committed Aug 5, 2020
1 parent c8e1dfe commit 55ab016
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/webpack-cli/lib/groups/ConfigGroup.js
Expand Up @@ -42,6 +42,16 @@ const getDefaultConfigFiles = () => {

const getConfigInfoFromFileName = (filename) => {
const fileMetaData = parse(filename);
// .cjs is not available on interpret side, handle it manually for now
if (filename.endsWith('.cjs')) {
return [
{
path: resolve(filename),
ext: '.cjs',
module: null,
},
];
}
return Object.keys(extensions)
.filter((ext) => ext.includes(fileMetaData.ext))
.filter((ext) => fileMetaData.base.substr(fileMetaData.base.length - ext.length) === ext)
Expand Down
12 changes: 12 additions & 0 deletions test/config-format/commonjs/commonjs.test.js
@@ -0,0 +1,12 @@
const { run } = require('../../utils/test-utils');
const { existsSync } = require('fs');
const { resolve } = require('path');

describe('webpack cli', () => {
it('should support CommonJS file', () => {
const { stderr, stdout } = run(__dirname, ['-c', 'webpack.config.cjs'], false);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(existsSync(resolve(__dirname, 'dist/foo.bundle.js'))).toBeTruthy();
});
});
1 change: 1 addition & 0 deletions test/config-format/commonjs/main.js
@@ -0,0 +1 @@
console.log('Hoshiumi');
12 changes: 12 additions & 0 deletions test/config-format/commonjs/webpack.config.cjs
@@ -0,0 +1,12 @@
const path = require('path');

const config = {
mode: 'production',
entry: './main.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'foo.bundle.js',
},
};

module.exports = config;
File renamed without changes.
File renamed without changes.
@@ -1,6 +1,6 @@
/* eslint-disable node/no-missing-require */
/* eslint-disable node/no-unpublished-require */
const { run, runInstall } = require('../utils/test-utils');
const { run, runInstall } = require('../../utils/test-utils');
const { stat } = require('fs');
const { resolve } = require('path');

Expand Down
@@ -1,3 +1,4 @@
/* eslint-disable node/no-unsupported-features/es-syntax */
/** eslint-disable **/
import * as path from 'path';

Expand Down

0 comments on commit 55ab016

Please sign in to comment.