Skip to content

feat: add mjs support #1786

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

Closed
wants to merge 13 commits into from
Closed
31 changes: 24 additions & 7 deletions packages/webpack-cli/lib/groups/ConfigGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const rechoir = require('rechoir');
const { arrayToObject } = require('../utils/arg-utils');
const ConfigError = require('../utils/errors/ConfigError');
const logger = require('../utils/logger');
const { pathToFileURL } = require('url');

// Order defines the priority, in increasing order
// example - config file lookup will be in order of .webpack/webpack.config.development.js -> webpack.config.development.js -> webpack.config.js
Expand Down Expand Up @@ -70,14 +71,26 @@ const requireLoader = (extension, path) => {
};

// Reads a config file given the config metadata
const requireConfig = (configModule) => {
const requireConfig = async (configModule) => {
const extension = Object.keys(jsVariants).find((t) => configModule.ext.endsWith(t));

if (extension) {
requireLoader(extension, configModule.path);
let config;

if (extension == '.mjs') {
try {
const url = pathToFileURL(configModule.path);
const ESMImport = new Function('url', 'return import(url)');
config = await ESMImport(url);
} catch (err) {
logger.warn('Cannot import ESM configuration');
throw err;
}
} else {
if (extension) {
requireLoader(extension, configModule.path);
}
config = require(configModule.path);
}

let config = require(configModule.path);
if (config.default) {
config = config.default;
}
Expand All @@ -101,7 +114,7 @@ const resolveConfigFiles = async (args) => {
throw new ConfigError(`The specified config file doesn't exist in ${configPath}`);
}
const foundConfig = configFiles[0];
const resolvedConfig = requireConfig(foundConfig);
const resolvedConfig = await requireConfig(foundConfig);
return finalize(resolvedConfig, args);
});
// resolve all the configs
Expand All @@ -125,7 +138,11 @@ const resolveConfigFiles = async (args) => {
return existsSync(file.path);
});

const configFiles = tmpConfigFiles.map(requireConfig);
const configFiles = [];
for await (const tmpConfigFile of tmpConfigFiles) {
configFiles.push(await requireConfig(tmpConfigFile));
}

if (configFiles.length) {
const defaultConfig = configFiles.find((p) => p.path.includes(mode) || p.path.includes(modeAlias[mode]));
if (defaultConfig) {
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"web"
],
"dependencies": {
"@webpack-cli/package-utils": "^1.0.1-alpha.4",
"@webpack-cli/info": "^1.0.1-alpha.4",
"@webpack-cli/init": "^1.0.1-alpha.5",
"@webpack-cli/package-utils": "^1.0.1-alpha.4",
"@webpack-cli/serve": "^1.0.1-alpha.5",
"ansi-escapes": "^4.3.1",
"colorette": "^1.2.1",
Expand All @@ -34,7 +34,7 @@
"enquirer": "^2.3.4",
"execa": "^4.0.0",
"import-local": "^3.0.2",
"interpret": "^2.0.0",
"interpret": "^2.2.0",
"rechoir": "^0.7.0",
"v8-compile-cache": "^2.1.0",
"webpack-merge": "^4.2.2"
Expand Down
1 change: 1 addition & 0 deletions test/config-format/mjs/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hoshiumi');
13 changes: 13 additions & 0 deletions test/config-format/mjs/mjs-config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { run } = require('../../utils/test-utils');
const { existsSync } = require('fs');
const { resolve } = require('path');

describe('webpack cli', () => {
it('should support mjs file', () => {
const { stderr, stdout } = run(__dirname, ['-c', 'webpack.config.mjs'], false);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
console.log({ stderr, stdout });
expect(existsSync(resolve(__dirname, 'dist/foo.bundle.js'))).toBeTruthy();
});
});
8 changes: 8 additions & 0 deletions test/config-format/mjs/webpack.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
mode: 'production',
entry: './main.js',
output: {
path: './dist',
filename: 'foo.bundle.js',
},
};
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7022,7 +7022,7 @@ interpret@^1.0.0:
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.2.0.tgz#d5061a6224be58e8083985f5014d844359576296"
integrity sha512-mT34yGKMNceBQUoVn7iCDKDntA7SC6gycMAWzGx1z/CMCTV7b2AAtXlo3nRyHZ1FelRkQbQjprHSYGwzLtkVbw==

interpret@^2.0.0:
interpret@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/interpret/-/interpret-2.2.0.tgz#1a78a0b5965c40a5416d007ad6f50ad27c417df9"
integrity sha512-Ju0Bz/cEia55xDwUWEa8+olFpCiQoypjnQySseKtmjNrnps3P+xfpUmGr90T7yjlVJmOtybRvPXhKMbHr+fWnw==
Expand Down