Skip to content

Commit

Permalink
Scripts: Convert file extension to js in block.json during build (#…
Browse files Browse the repository at this point in the history
…41068)

* convert file extension in block.json

* support for multiple entries.

* add changelog.
  • Loading branch information
torounit authored May 23, 2022
1 parent 6851886 commit 93c84f9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### New Feature

- Enable by default code formatting for JSON files in the `format` command ([#40994](https://github.com/WordPress/gutenberg/pull/40994)). You can opt-out of this behavior by providing a custom file matcher, example: `wp-scripts format src/**/*.js`.
- Support tsx files in `viewScript`, `script`, `editorScript` ([#41068](https://github.com/WordPress/gutenberg/pull/41068)).

### Bug Fixes

Expand Down
28 changes: 28 additions & 0 deletions packages/scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,34 @@ const config = {
from: copyWebpackPatterns,
context: process.env.WP_SRC_DIRECTORY,
noErrorOnMissing: true,
transform( content, absoluteFrom ) {
const convertExtension = ( path ) => {
return path.replace( /\.(j|t)sx?$/, '.js' );
};

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

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

return content;
},
},
],
} ),
Expand Down

0 comments on commit 93c84f9

Please sign in to comment.