-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WP Scripts: Build block.json viewModule (#57461)
Add handling of viewModule field in block.json when using wp-scripts build or start. As the Modules API matures, our tooling should support it. We add an option to clearly mark this an an experimental feature: `wp-scripts build --experimental-modules` or `wp-scripts start --experimental-modules`. To support modules with webpack, it was necessary to run a multi-compilation. An array of webpack configurations is used instead of a single webpack configuration. This is because module compilation is an option at the compilation level and cannot be set for specific entrypoints. If the `--experimental-modules` option is found, we use an environment variable to change the webpack.config export to return an array `[ scriptWebpackConfig, moduleWebpackConfig ]`. Without the experimental option, the `webpack.config` export is the same. Consumers should be able to continue extending this config without noticing any differences. Part of #57492.
- Loading branch information
Showing
8 changed files
with
341 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const moduleFields = new Set( [ 'viewModule' ] ); | ||
const scriptFields = new Set( [ 'viewScript', 'script', 'editorScript' ] ); | ||
|
||
/** | ||
* @param {Object} blockJson | ||
* @return {null|Record<string, unknown>} Fields | ||
*/ | ||
function getBlockJsonModuleFields( blockJson ) { | ||
let result = null; | ||
for ( const field of moduleFields ) { | ||
if ( Object.hasOwn( blockJson, field ) ) { | ||
if ( ! result ) { | ||
result = {}; | ||
} | ||
result[ field ] = blockJson[ field ]; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* @param {Object} blockJson | ||
* @return {null|Record<string, unknown>} Fields | ||
*/ | ||
function getBlockJsonScriptFields( blockJson ) { | ||
let result = null; | ||
for ( const field of scriptFields ) { | ||
if ( Object.hasOwn( blockJson, field ) ) { | ||
if ( ! result ) { | ||
result = {}; | ||
} | ||
result[ field ] = blockJson[ field ]; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
module.exports = { | ||
getBlockJsonModuleFields, | ||
getBlockJsonScriptFields, | ||
}; |
Oops, something went wrong.