Skip to content

Commit

Permalink
Correctly read rawRequest for frontend ESM (#31917)
Browse files Browse the repository at this point in the history
Fixes: #31909
Fixes: #32050
  • Loading branch information
vcanales committed May 20, 2021
1 parent e464fe0 commit 48137eb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions bin/build-plugin-zip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ build_files=$(
build/block-library/blocks/*.php \
build/block-library/blocks/*/block.json \
build/block-library/blocks/*/*.css \
build/block-library/blocks/*/*.js \
build/edit-widgets/blocks/*/block.json \
)

Expand Down
19 changes: 11 additions & 8 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,19 @@ module.exports = {
entry: createEntrypoints(),
output: {
devtoolNamespace: 'wp',
filename: ( data ) => {
const { chunk } = data;
filename: ( pathData ) => {
const { chunk } = pathData;
const { entryModule } = chunk;
const { rawRequest } = entryModule;
const { rawRequest, rootModule } = entryModule;

/*
* If the file being built is a Core Block's frontend file,
* we build it in the block's directory.
*/
if ( rawRequest && rawRequest.includes( '/frontend.js' ) ) {
// When processing ESM files, the requested path
// is defined in `entryModule.rootModule.rawRequest`, instead of
// being present in `entryModule.rawRequest`.
// In the context of frontend files, they would be processed
// as ESM if they use `import` or `export` within it.
const request = rootModule?.rawRequest || rawRequest;

if ( request.includes( '/frontend.js' ) ) {
return `./build/block-library/blocks/[name]/frontend.js`;
}

Expand Down

0 comments on commit 48137eb

Please sign in to comment.