Skip to content

Commit 48137eb

Browse files
authored
Correctly read rawRequest for frontend ESM (#31917)
Fixes: #31909 Fixes: #32050
1 parent e464fe0 commit 48137eb

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

bin/build-plugin-zip.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ build_files=$(
114114
build/block-library/blocks/*.php \
115115
build/block-library/blocks/*/block.json \
116116
build/block-library/blocks/*/*.css \
117+
build/block-library/blocks/*/*.js \
117118
build/edit-widgets/blocks/*/block.json \
118119
)
119120

webpack.config.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,19 @@ module.exports = {
135135
entry: createEntrypoints(),
136136
output: {
137137
devtoolNamespace: 'wp',
138-
filename: ( data ) => {
139-
const { chunk } = data;
138+
filename: ( pathData ) => {
139+
const { chunk } = pathData;
140140
const { entryModule } = chunk;
141-
const { rawRequest } = entryModule;
141+
const { rawRequest, rootModule } = entryModule;
142142

143-
/*
144-
* If the file being built is a Core Block's frontend file,
145-
* we build it in the block's directory.
146-
*/
147-
if ( rawRequest && rawRequest.includes( '/frontend.js' ) ) {
143+
// When processing ESM files, the requested path
144+
// is defined in `entryModule.rootModule.rawRequest`, instead of
145+
// being present in `entryModule.rawRequest`.
146+
// In the context of frontend files, they would be processed
147+
// as ESM if they use `import` or `export` within it.
148+
const request = rootModule?.rawRequest || rawRequest;
149+
150+
if ( request.includes( '/frontend.js' ) ) {
148151
return `./build/block-library/blocks/[name]/frontend.js`;
149152
}
150153

0 commit comments

Comments
 (0)