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

Scripts: Convert file extension to js in block.json during build #41068

Merged
merged 3 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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)).

## 23.0.0 (2022-05-04)

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(
gziolo marked this conversation as resolved.
Show resolved Hide resolved
( 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