Skip to content

Commit

Permalink
Reuse block-json utils in webpack config
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Jan 4, 2024
1 parent d9c06ab commit faa495a
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions packages/scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const {
getWebpackEntryPoints,
getRenderPropPaths,
getAsBooleanFromENV,
getBlockJsonModuleFields,
getBlockJsonScriptFields,
} = require( '../utils' );

const isProduction = process.env.NODE_ENV === 'production';
Expand Down Expand Up @@ -328,22 +330,22 @@ const scriptConfig = {

if ( basename( absoluteFrom ) === 'block.json' ) {
const blockJson = JSON.parse( content.toString() );
[ 'viewScript', 'script', 'editorScript' ].forEach(
( key ) => {
if ( Array.isArray( blockJson[ key ] ) ) {

const fields =
getBlockJsonScriptFields( blockJson );
if ( fields ) {
for ( const [ key, value ] of Object.entries(
fields
) ) {
if ( Array.isArray( value ) ) {
blockJson[ key ] =
value.map( convertExtension );
} else if ( typeof value === 'string' ) {
blockJson[ key ] =
blockJson[ key ].map(
convertExtension
);
} else if (
typeof blockJson[ key ] === 'string'
) {
blockJson[ key ] = convertExtension(
blockJson[ key ]
);
convertExtension( value );
}
}
);
}

return JSON.stringify( blockJson, null, 2 );
}
Expand Down Expand Up @@ -418,20 +420,25 @@ if ( hasExperimentalModulesFlag ) {
const blockJson = JSON.parse(
content.toString()
);
[ 'viewModule' ].forEach( ( key ) => {
if ( Array.isArray( blockJson[ key ] ) ) {
blockJson[ key ] =
blockJson[ key ].map(
convertExtension
);
} else if (
typeof blockJson[ key ] === 'string'
) {
blockJson[ key ] = convertExtension(
blockJson[ key ]
);

const fields =
getBlockJsonModuleFields( blockJson );
if ( fields ) {
for ( const [
key,
value,
] of Object.entries( fields ) ) {
if ( Array.isArray( value ) ) {
blockJson[ key ] =
value.map( convertExtension );
} else if (
typeof value === 'string'
) {
blockJson[ key ] =
convertExtension( value );
}
}
} );
}

return JSON.stringify( blockJson, null, 2 );
}
Expand Down

0 comments on commit faa495a

Please sign in to comment.