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

Invalid configuration object error when using --experimental-modules #61972

Closed
mizan42047 opened this issue May 24, 2024 · 4 comments
Closed
Labels
[Feature] Script Modules API Related to the Script Modules API that adds support for native ES modules and import maps [Type] Bug An existing feature does not function as intended

Comments

@mizan42047
Copy link

mizan42047 commented May 24, 2024

Description

Without --experimental-modules, the configuration below runs fine:

const defaultConfig = require('@wordpress/scripts/config/webpack.config.js');
const path = require('path');

module.exports = {
    ...defaultConfig,
    resolve: {
        alias: {
            '@': path.resolve(__dirname, 'src/'),
        },
    },
};

However, when I added the --experimental-modules flag to my package.json like this:
"start": "wp-scripts start --experimental-modules"
I got the following error:

[webpack-cli] Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
 - configuration has an unknown property '1'. These properties are valid:
   object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, extends?, externals?, externalsPresets?, externalsType?, ignoreWarnings?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
   -> Options object as provided by the user.
   For typos: please correct them.
   For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration.
     Loaders should be updated to allow passing options via loader options in module.rules.
     Until loaders are updated, one can use the LoaderOptionsPlugin to pass these options to the loader:
     plugins: [
       new webpack.LoaderOptionsPlugin({
         // test: /\.xxx$/, // may apply this only for some modules
         options: {
           1: …
         }
       })
     ]

Step-by-step reproduction instructions

  1. Create a plugin with npx @wordpress/create-block@latest my-first-interactive-block --template @wordpress/create-block-interactive-template.
  2. Add a file named webpack.config.js.
  3. Then, try to extend the @wordpress/scripts configuration like this:
const defaultConfig = require('@wordpress/scripts/config/webpack.config.js');
const path = require('path');
module.exports = {
    ...defaultConfig,
    resolve: {
        alias: {
            '@': path.resolve(__dirname, 'src/'),
        },
    },
};

Screenshots, screen recording, code snippet

Screenshot from 2024-05-24 23-55-24

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@mizan42047 mizan42047 added the [Type] Bug An existing feature does not function as intended label May 24, 2024
@rafaucau

This comment was marked as resolved.

@jordesign jordesign added the [Feature] Script Modules API Related to the Script Modules API that adds support for native ES modules and import maps label May 26, 2024
@rafaucau
Copy link

rafaucau commented May 30, 2024

When using --experimental-modules, the config returns an array of two objects:

module.exports = [ scriptConfig, moduleConfig ];

To handle this, you can extend both configs:

const wordpressConfig = require('@wordpress/scripts/config/webpack.config');

function extendSharedConfig(config) {
	return {
		...config,
		// Add shared config extensions here...
	};
}

function extendScriptConfig(config) {
	return {
		...config,
		// Add non-module config extensions here...
	};
}

module.exports = (() => {
	if (Array.isArray(wordpressConfig)) {
		const [scriptConfig, moduleConfig] = wordpressConfig;

		const extendedScriptConfig = extendSharedConfig(extendScriptConfig(scriptConfig));
		const extendedModuleConfig = extendSharedConfig(moduleConfig);

		return [extendedScriptConfig, extendedModuleConfig];
	} else {
		return extendSharedConfig(extendScriptConfig(wordpressConfig));
	}
})();

@mizan42047
Copy link
Author

When using --experimental-modules, the config returns an array of two objects:

module.exports = [ scriptConfig, moduleConfig ];

To handle this, you can extend both configs:

const wordpressConfig = require('@wordpress/scripts/config/webpack.config');

function extendSharedConfig(config) {
	return {
		...config,
		// Add shared config extensions here...
	};
}

function extendScriptConfig(config) {
	return {
		...config,
		// Add non-module config extensions here...
	};
}

module.exports = (() => {
	if (Array.isArray(wordpressConfig)) {
		const [scriptConfig, moduleConfig] = wordpressConfig;

		const extendedScriptConfig = extendSharedConfig(extendScriptConfig(scriptConfig));
		const extendedModuleConfig = extendSharedConfig(moduleConfig);

		return [extendedScriptConfig, extendedModuleConfig];
	} else {
		return extendSharedConfig(extendScriptConfig(wordpressConfig));
	}
})();

Yeah, it works. Thank you so much @rafaucau

@TheAggressive
Copy link

@mizan42047 could you post your final webpack.config.js for reference how it turned out?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Script Modules API Related to the Script Modules API that adds support for native ES modules and import maps [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

4 participants