From 55ab016c0a8ff1be1ccf8d36673e4391918647ba Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Wed, 5 Aug 2020 08:57:23 +0530 Subject: [PATCH] feat: add support for .cjs config (#1727) * feat: support .cjs config file * feat: support .cjs config file * feat: support .cjs config file --- packages/webpack-cli/lib/groups/ConfigGroup.js | 10 ++++++++++ test/config-format/commonjs/commonjs.test.js | 12 ++++++++++++ test/config-format/commonjs/main.js | 1 + test/config-format/commonjs/webpack.config.cjs | 12 ++++++++++++ test/{ => config-format}/typescript/main.ts | 0 test/{ => config-format}/typescript/package.json | 0 .../typescript/typescript.test.js | 2 +- .../{ => config-format}/typescript/webpack.config.ts | 1 + 8 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/config-format/commonjs/commonjs.test.js create mode 100644 test/config-format/commonjs/main.js create mode 100644 test/config-format/commonjs/webpack.config.cjs rename test/{ => config-format}/typescript/main.ts (100%) rename test/{ => config-format}/typescript/package.json (100%) rename test/{ => config-format}/typescript/typescript.test.js (92%) rename test/{ => config-format}/typescript/webpack.config.ts (80%) diff --git a/packages/webpack-cli/lib/groups/ConfigGroup.js b/packages/webpack-cli/lib/groups/ConfigGroup.js index 29f58bb681a..5e8953759be 100644 --- a/packages/webpack-cli/lib/groups/ConfigGroup.js +++ b/packages/webpack-cli/lib/groups/ConfigGroup.js @@ -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) diff --git a/test/config-format/commonjs/commonjs.test.js b/test/config-format/commonjs/commonjs.test.js new file mode 100644 index 00000000000..6da158d4f2b --- /dev/null +++ b/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(); + }); +}); diff --git a/test/config-format/commonjs/main.js b/test/config-format/commonjs/main.js new file mode 100644 index 00000000000..8ed93e41fb2 --- /dev/null +++ b/test/config-format/commonjs/main.js @@ -0,0 +1 @@ +console.log('Hoshiumi'); diff --git a/test/config-format/commonjs/webpack.config.cjs b/test/config-format/commonjs/webpack.config.cjs new file mode 100644 index 00000000000..7bb5d185edf --- /dev/null +++ b/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; diff --git a/test/typescript/main.ts b/test/config-format/typescript/main.ts similarity index 100% rename from test/typescript/main.ts rename to test/config-format/typescript/main.ts diff --git a/test/typescript/package.json b/test/config-format/typescript/package.json similarity index 100% rename from test/typescript/package.json rename to test/config-format/typescript/package.json diff --git a/test/typescript/typescript.test.js b/test/config-format/typescript/typescript.test.js similarity index 92% rename from test/typescript/typescript.test.js rename to test/config-format/typescript/typescript.test.js index 5c097d25b3e..51a48c57131 100644 --- a/test/typescript/typescript.test.js +++ b/test/config-format/typescript/typescript.test.js @@ -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'); diff --git a/test/typescript/webpack.config.ts b/test/config-format/typescript/webpack.config.ts similarity index 80% rename from test/typescript/webpack.config.ts rename to test/config-format/typescript/webpack.config.ts index 3fcf302d52d..4a17fa148c6 100644 --- a/test/typescript/webpack.config.ts +++ b/test/config-format/typescript/webpack.config.ts @@ -1,3 +1,4 @@ +/* eslint-disable node/no-unsupported-features/es-syntax */ /** eslint-disable **/ import * as path from 'path';