From ac7af135452806078563d9d096b1764bce4d5cb4 Mon Sep 17 00:00:00 2001 From: luisherranz Date: Thu, 19 Nov 2026 15:42:18 +0100 Subject: [PATCH 01/12] Dependency Extraction: Keep the import kind of each script module external --- .../lib/index.js | 7 +- .../lib/util.js | 22 +- .../test/__snapshots__/build.js.snap | 546 ++++++++++++++++-- .../test/build.js | 170 ++++-- .../wordpress-script-modules/dynamic.js | 9 + .../wordpress-script-modules/static.js | 5 + .../webpack.config.js | 9 + 7 files changed, 659 insertions(+), 109 deletions(-) create mode 100644 packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/dynamic.js create mode 100644 packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/static.js create mode 100644 packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/webpack.config.js diff --git a/packages/dependency-extraction-webpack-plugin/lib/index.js b/packages/dependency-extraction-webpack-plugin/lib/index.js index 91c354b7feb230..ceb78b849dbf50 100644 --- a/packages/dependency-extraction-webpack-plugin/lib/index.js +++ b/packages/dependency-extraction-webpack-plugin/lib/index.js @@ -156,10 +156,15 @@ class DependencyExtractionWebpackPlugin { /** * Offload externalization work to the ExternalsPlugin. + * + * Script modules use the `module-import` type, which keeps the kind of + * each import: a static `import` declaration stays static and an + * `import()` stays dynamic. + * * @type {webpack.ExternalsPlugin} */ this.externalsPlugin = new webpack.ExternalsPlugin( - this.useModules ? 'import' : 'window', + this.useModules ? 'module-import' : 'window', this.externalizeWpDeps.bind( this ) ); diff --git a/packages/dependency-extraction-webpack-plugin/lib/util.js b/packages/dependency-extraction-webpack-plugin/lib/util.js index 6457b1b0457faf..e5f4e876635f8e 100644 --- a/packages/dependency-extraction-webpack-plugin/lib/util.js +++ b/packages/dependency-extraction-webpack-plugin/lib/util.js @@ -76,32 +76,26 @@ function defaultRequestToExternal( request ) { /** * Default request to external module transformation * - * Currently only `@wordpress/interactivity` and `@wordpress/interactivity-router` - * are supported. + * Currently only `@wordpress/interactivity`, `@wordpress/interactivity-router`, + * and `@wordpress/a11y` are supported. Each maps to its own script module ID, + * which every import of it keeps: a static `import` declaration stays static + * and an `import()` stays dynamic. * * Do not use the boolean shorthand here, it's only handled for the * `requestToExternalModule` option. * * @param {string} request Module request (the module name in `import from`) to be transformed - * @return {string|Error|undefined} The resulting external definition. + * @return {string|undefined} The resulting external definition. * - Return `undefined` to ignore the request (do not externalize). * - Return `string` to map the request to an external. - * - Return `Error` to emit an error. + * @throws {Error} When the request is a WordPress script, which script modules can't import. */ function defaultRequestToExternalModule( request ) { - if ( request === '@wordpress/interactivity' ) { - // This is a special case. Interactivity does not support dynamic imports at - // this time. We add the external "module" type to indicate that webpack - // should externalize this as a module (instead of our default `import()` - // external type) which forces @wordpress/interactivity imports to be - // hoisted to static imports. - return `module ${ request }`; - } - switch ( request ) { + case '@wordpress/interactivity': case '@wordpress/interactivity-router': case '@wordpress/a11y': - return `import ${ request }`; + return request; } const isWordPressScript = Boolean( defaultRequestToExternal( request ) ); diff --git a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap index 0eb447bb20d81c..01e53b13bc3431 100644 --- a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap +++ b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap @@ -7,14 +7,14 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`combine-assets\` '@wordpress/blob', 'lodash' ), - 'version' => '270cb9e2d17f391e9270', + 'version' => '7eff34617328511782d2', 'type' => 'module' ), 'fileB.mjs' => array( 'dependencies' => array( '@wordpress/token-list' ), - 'version' => '4cf36c37afb98cbb1f86', + 'version' => '973c1e1bcd51b7984157', 'type' => 'module' ) ); @@ -24,17 +24,17 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`combine-assets\` exports[`DependencyExtractionWebpackPlugin modules > Webpack \`combine-assets\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/blob", "userRequest": "@wordpress/blob", }, { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/token-list", "userRequest": "@wordpress/token-list", }, { - "externalType": "import", + "externalType": "module-import", "request": "lodash", "userRequest": "lodash", }, @@ -55,7 +55,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`cyclic-dependency exports[`DependencyExtractionWebpackPlugin modules > Webpack \`cyclic-dependency-graph\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "module", + "externalType": "module-import", "request": "@wordpress/interactivity", "userRequest": "@wordpress/interactivity", }, @@ -79,7 +79,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`cyclic-dynamic-de exports[`DependencyExtractionWebpackPlugin modules > Webpack \`cyclic-dynamic-dependency-graph\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "module", + "externalType": "module-import", "request": "@wordpress/interactivity", "userRequest": "@wordpress/interactivity", }, @@ -103,7 +103,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`cyclic-external-d exports[`DependencyExtractionWebpackPlugin modules > Webpack \`cyclic-external-deps\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "module", + "externalType": "module-import", "request": "@wordpress/interactivity", "userRequest": "@wordpress/interactivity", }, @@ -118,7 +118,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`dynamic-import\` 'import' => 'dynamic' ) ), - 'version' => '7d8b118ac6242e7f308e', + 'version' => 'f2e6640468a84cdc4998', 'type' => 'module' ); " @@ -127,7 +127,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`dynamic-import\` exports[`DependencyExtractionWebpackPlugin modules > Webpack \`dynamic-import\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/blob", "userRequest": "@wordpress/blob", }, @@ -149,7 +149,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`function-output-f '@wordpress/blob', 'lodash' ), - 'version' => 'b68363f122fe0a26183e', + 'version' => 'e1af539043c7b15bb0d6', 'type' => 'module' ); " @@ -158,12 +158,12 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`function-output-f exports[`DependencyExtractionWebpackPlugin modules > Webpack \`function-output-filename\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/blob", "userRequest": "@wordpress/blob", }, { - "externalType": "import", + "externalType": "module-import", "request": "lodash", "userRequest": "lodash", }, @@ -176,7 +176,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`has-extension-suf '@wordpress/blob', 'lodash' ), - 'version' => 'fd4b8dcc8741c1523219', + 'version' => '409ace9b7131261ac392', 'type' => 'module' ); " @@ -185,12 +185,12 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`has-extension-suf exports[`DependencyExtractionWebpackPlugin modules > Webpack \`has-extension-suffix\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/blob", "userRequest": "@wordpress/blob", }, { - "externalType": "import", + "externalType": "module-import", "request": "lodash", "userRequest": "lodash", }, @@ -203,7 +203,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`module-renames\` 'renamed--@my/module', 'renamed--other-module' ), - 'version' => '79d3c39076be7f6df4ea', + 'version' => '373c8b2e6dde089cd84a', 'type' => 'module' ); " @@ -212,12 +212,12 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`module-renames\` exports[`DependencyExtractionWebpackPlugin modules > Webpack \`module-renames\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "renamed--@my/module", "userRequest": "@my/module", }, { - "externalType": "import", + "externalType": "module-import", "request": "renamed--other-module", "userRequest": "other-module", }, @@ -232,7 +232,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`nested-dynamic-im 'import' => 'dynamic' ) ), - 'version' => '67bfe5b9e28926bb7d9f', + 'version' => '3fcb9b25d95448d36ac5', 'type' => 'module' ); " @@ -241,7 +241,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`nested-dynamic-im exports[`DependencyExtractionWebpackPlugin modules > Webpack \`nested-dynamic-import\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "module", + "externalType": "module-import", "request": "@wordpress/interactivity", "userRequest": "@wordpress/interactivity", }, @@ -280,7 +280,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`option-function-o '@wordpress/blob', 'lodash' ), - 'version' => 'b68363f122fe0a26183e', + 'version' => 'e1af539043c7b15bb0d6', 'type' => 'module' ); " @@ -289,12 +289,12 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`option-function-o exports[`DependencyExtractionWebpackPlugin modules > Webpack \`option-function-output-filename\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/blob", "userRequest": "@wordpress/blob", }, { - "externalType": "import", + "externalType": "module-import", "request": "lodash", "userRequest": "lodash", }, @@ -307,7 +307,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`option-output-fil '@wordpress/blob', 'lodash' ), - 'version' => 'b68363f122fe0a26183e', + 'version' => 'e1af539043c7b15bb0d6', 'type' => 'module' ); " @@ -316,12 +316,12 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`option-output-fil exports[`DependencyExtractionWebpackPlugin modules > Webpack \`option-output-filename\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/blob", "userRequest": "@wordpress/blob", }, { - "externalType": "import", + "externalType": "module-import", "request": "lodash", "userRequest": "lodash", }, @@ -333,7 +333,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`output-format-jso "dependencies": [ "lodash" ], - "version": "11156aba3b369605e939", + "version": "b88f38b51529fefd78ea", "type": "module" }" `; @@ -341,7 +341,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`output-format-jso exports[`DependencyExtractionWebpackPlugin modules > Webpack \`output-format-json\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "lodash", "userRequest": "lodash", }, @@ -356,7 +356,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`overrides\` > sho 'rxjs', 'rxjs/operators' ), - 'version' => 'af74b6f7d8a3f114b5b3', + 'version' => '7b784cc8ae9ba073abe6', 'type' => 'module' ); " @@ -365,22 +365,22 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`overrides\` > sho exports[`DependencyExtractionWebpackPlugin modules > Webpack \`overrides\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/blob", "userRequest": "@wordpress/blob", }, { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/url", "userRequest": "@wordpress/url", }, { - "externalType": "import", + "externalType": "module-import", "request": "rxjs", "userRequest": "rxjs", }, { - "externalType": "import", + "externalType": "module-import", "request": "rxjs/operators", "userRequest": "rxjs/operators", }, @@ -418,7 +418,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`runtime-chunk-sin 'dependencies' => array( '@wordpress/blob' ), - 'version' => '0b4be29a9a428b9e8608', + 'version' => '547479763ef3cc9c5b3f', 'type' => 'module', 'handle' => 'runtime-chunk-single-modules-a' ); @@ -431,7 +431,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`runtime-chunk-sin '@wordpress/blob', 'lodash' ), - 'version' => '95c637f4ad0885697262', + 'version' => '123d6a6e989037833f6c', 'type' => 'module', 'handle' => 'runtime-chunk-single-modules-b' ); @@ -443,7 +443,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`runtime-chunk-sin 'dependencies' => array( ), - 'version' => '9c63db42c1fe56ddaf1e', + 'version' => '06eb23ece3c1ab5f957a', 'type' => 'module', 'handle' => 'runtime-chunk-single-modules-runtime' ); @@ -453,12 +453,12 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`runtime-chunk-sin exports[`DependencyExtractionWebpackPlugin modules > Webpack \`runtime-chunk-single\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/blob", "userRequest": "@wordpress/blob", }, { - "externalType": "import", + "externalType": "module-import", "request": "lodash", "userRequest": "lodash", }, @@ -470,7 +470,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`style-cache-group 'dependencies' => array( '@wordpress/blob' ), - 'version' => '8d9f598be79e102d0648', + 'version' => 'c66c1495b04909af8337', 'type' => 'module' ); " @@ -479,7 +479,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`style-cache-group exports[`DependencyExtractionWebpackPlugin modules > Webpack \`style-cache-group\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/blob", "userRequest": "@wordpress/blob", }, @@ -492,7 +492,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`style-imports\` > '@wordpress/blob', 'lodash' ), - 'version' => '825b69f70b86760c73a3', + 'version' => 'fc6bbfd4f4f314fc5b91', 'type' => 'module' ); " @@ -501,12 +501,12 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`style-imports\` > exports[`DependencyExtractionWebpackPlugin modules > Webpack \`style-imports\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/blob", "userRequest": "@wordpress/blob", }, { - "externalType": "import", + "externalType": "module-import", "request": "lodash", "userRequest": "lodash", }, @@ -519,7 +519,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress\` > sho '@wordpress/blob', 'lodash' ), - 'version' => 'b68363f122fe0a26183e', + 'version' => 'e1af539043c7b15bb0d6', 'type' => 'module' ); " @@ -528,12 +528,12 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress\` > sho exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/blob", "userRequest": "@wordpress/blob", }, { - "externalType": "import", + "externalType": "module-import", "request": "lodash", "userRequest": "lodash", }, @@ -549,7 +549,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-interac 'import' => 'dynamic' ) ), - 'version' => '995805497b878a0197d9', + 'version' => '06e112f232d0d7385044', 'type' => 'module' ); " @@ -558,15 +558,15 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-interac exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-interactivity\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", - "request": "lodash", - "userRequest": "lodash", - }, - { - "externalType": "module", + "externalType": "module-import", "request": "@wordpress/interactivity", "userRequest": "@wordpress/interactivity", }, + { + "externalType": "module-import", + "request": "lodash", + "userRequest": "lodash", + }, ] `; @@ -576,7 +576,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-require '@wordpress/blob', 'lodash' ), - 'version' => '9dcb7bb56f3fd06c5087', + 'version' => '7c3ee7ef53a2ce355708', 'type' => 'module' ); " @@ -585,18 +585,397 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-require exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-require\` > should produce expected output > External modules should match snapshot 1`] = ` [ { - "externalType": "import", + "externalType": "module-import", "request": "@wordpress/blob", "userRequest": "@wordpress/blob", }, { - "externalType": "import", + "externalType": "module-import", "request": "lodash", "userRequest": "lodash", }, ] `; +exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-script-modules\` > should produce expected output > Asset file 'dynamic.asset.php' should match snapshot 1`] = ` +" array( + array( + 'id' => '@wordpress/a11y', + 'import' => 'dynamic' + ), + array( + 'id' => '@wordpress/interactivity', + 'import' => 'dynamic' + ), + array( + 'id' => '@wordpress/interactivity-router', + 'import' => 'dynamic' + ) + ), + 'version' => 'd341ba7ca1a44a5cf85a', + 'type' => 'module' +); +" +`; + +exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-script-modules\` > should produce expected output > Asset file 'static.asset.php' should match snapshot 1`] = ` +" array( + '@wordpress/a11y', + '@wordpress/interactivity', + '@wordpress/interactivity-router' + ), + 'version' => '5025370194ea97be933a', + 'type' => 'module' +); +" +`; + +exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-script-modules\` > should produce expected output > External modules should match snapshot 1`] = ` +[ + { + "externalType": "module-import", + "request": "@wordpress/a11y", + "userRequest": "@wordpress/a11y", + }, + { + "externalType": "module-import", + "request": "@wordpress/interactivity", + "userRequest": "@wordpress/interactivity", + }, + { + "externalType": "module-import", + "request": "@wordpress/interactivity-router", + "userRequest": "@wordpress/interactivity-router", + }, + { + "externalType": "module-import", + "request": "@wordpress/a11y", + "userRequest": "@wordpress/a11y", + }, + { + "externalType": "module-import", + "request": "@wordpress/interactivity", + "userRequest": "@wordpress/interactivity", + }, + { + "externalType": "module-import", + "request": "@wordpress/interactivity-router", + "userRequest": "@wordpress/interactivity-router", + }, +] +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`combine-assets\` > should keep the kind of each import of an external 1`] = ` +{ + "fileA.mjs": { + "dynamic": [], + "static": [ + "@wordpress/blob", + "lodash", + ], + }, + "fileB.mjs": { + "dynamic": [], + "static": [ + "@wordpress/token-list", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`cyclic-dependency-graph\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [ + "@wordpress/interactivity", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`cyclic-dynamic-dependency-graph\` > should keep the kind of each import of an external 1`] = ` +{ + "a_js.mjs": { + "dynamic": [], + "static": [ + "@wordpress/interactivity", + ], + }, + "main.mjs": { + "dynamic": [ + "./a_js.mjs", + ], + "static": [], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`cyclic-external-deps\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [ + "@wordpress/interactivity", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`dynamic-import\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [ + "./util_js.mjs", + ], + "static": [], + }, + "util_js.mjs": { + "dynamic": [], + "static": [ + "@wordpress/blob", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`function-output-filename\` > should keep the kind of each import of an external 1`] = ` +{ + "chunk--main--main.js": { + "dynamic": [], + "static": [ + "@wordpress/blob", + "lodash", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`has-extension-suffix\` > should keep the kind of each import of an external 1`] = ` +{ + "index.min.js": { + "dynamic": [], + "static": [ + "@wordpress/blob", + "lodash", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`module-renames\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [ + "renamed--@my/module", + "renamed--other-module", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`nested-dynamic-import\` > should keep the kind of each import of an external 1`] = ` +{ + "level-one_js.mjs": { + "dynamic": [], + "static": [], + }, + "main.mjs": { + "dynamic": [ + "./level-one_js.mjs", + "@wordpress/interactivity", + ], + "static": [], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`no-default\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`no-deps\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`option-function-output-filename\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [ + "@wordpress/blob", + "lodash", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`option-output-filename\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [ + "@wordpress/blob", + "lodash", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`output-format-json\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [ + "lodash", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`overrides\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [ + "@wordpress/blob", + "@wordpress/url", + "rxjs", + "rxjs/operators", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`polyfill-magic-comment\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`polyfill-magic-comment-minified\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`runtime-chunk-single\` > should keep the kind of each import of an external 1`] = ` +{ + "a.mjs": { + "dynamic": [], + "static": [], + }, + "b.mjs": { + "dynamic": [], + "static": [], + }, + "runtime.mjs": { + "dynamic": [], + "static": [], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`style-cache-group\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [ + "@wordpress/blob", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`style-imports\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [ + "@wordpress/blob", + "lodash", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`wordpress\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [ + "@wordpress/blob", + "lodash", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`wordpress-interactivity\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [ + "@wordpress/interactivity", + ], + "static": [ + "lodash", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`wordpress-require\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [], + "static": [ + "@wordpress/blob", + "lodash", + ], + }, +} +`; + +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`wordpress-script-modules\` > should keep the kind of each import of an external 1`] = ` +{ + "dynamic.mjs": { + "dynamic": [ + "@wordpress/a11y", + "@wordpress/interactivity", + "@wordpress/interactivity-router", + ], + "static": [], + }, + "static.mjs": { + "dynamic": [], + "static": [ + "@wordpress/a11y", + "@wordpress/interactivity", + "@wordpress/interactivity-router", + ], + }, +} +`; + exports[`DependencyExtractionWebpackPlugin scripts > Webpack \`combine-assets\` > should produce expected output > Asset file 'assets.php' should match snapshot 1`] = ` " array( @@ -1214,3 +1593,56 @@ exports[`DependencyExtractionWebpackPlugin scripts > Webpack \`wordpress-require }, ] `; + +exports[`DependencyExtractionWebpackPlugin scripts > Webpack \`wordpress-script-modules\` > should produce expected output > Asset file 'dynamic.asset.php' should match snapshot 1`] = ` +" array( + 'wp-a11y', + 'wp-interactivity', + 'wp-interactivity-router' + ), + 'version' => 'a32f1abc0cf670a4d0f5' +); +" +`; + +exports[`DependencyExtractionWebpackPlugin scripts > Webpack \`wordpress-script-modules\` > should produce expected output > Asset file 'static.asset.php' should match snapshot 1`] = ` +" array( + 'wp-a11y', + 'wp-interactivity', + 'wp-interactivity-router' + ), + 'version' => 'eab49dbefc633236009b' +); +" +`; + +exports[`DependencyExtractionWebpackPlugin scripts > Webpack \`wordpress-script-modules\` > should produce expected output > External modules should match snapshot 1`] = ` +[ + { + "externalType": "window", + "request": [ + "wp", + "a11y", + ], + "userRequest": "@wordpress/a11y", + }, + { + "externalType": "window", + "request": [ + "wp", + "interactivity", + ], + "userRequest": "@wordpress/interactivity", + }, + { + "externalType": "window", + "request": [ + "wp", + "interactivityRouter", + ], + "userRequest": "@wordpress/interactivity-router", + }, +] +`; diff --git a/packages/dependency-extraction-webpack-plugin/test/build.js b/packages/dependency-extraction-webpack-plugin/test/build.js index 469c30295936a9..41ae74db4fc6be 100644 --- a/packages/dependency-extraction-webpack-plugin/test/build.js +++ b/packages/dependency-extraction-webpack-plugin/test/build.js @@ -27,6 +27,49 @@ function expectCompilationError( stats ) { expect( stats.toString( { errors: true, all: false } ) ).toMatchSnapshot(); } +/** + * Lists, for each JavaScript file emitted to a folder, the specifiers its + * static `import` declarations and its `import()` calls name. + * + * @param {string} outputDirectory Folder of an unminified module build. + * @return {Record} Specifiers by file and import kind. + */ +function getEmittedImports( outputDirectory ) { + const specifiers = ( content, pattern ) => + [ + ...new Set( + Array.from( + content.matchAll( pattern ), + ( match ) => match[ 1 ] + ) + ), + ].sort(); + + return Object.fromEntries( + globSync( '**/*.@(js|mjs)', { cwd: outputDirectory } ) + .sort() + .map( ( file ) => { + const content = fs.readFileSync( + path.join( outputDirectory, file ), + 'utf-8' + ); + return [ + file, + { + static: specifiers( + content, + /^import\s(?:[^;]*?\sfrom\s)?"([^"]+)";$/gm + ), + dynamic: specifiers( + content, + /\bimport\("([^"]+)"\)/g + ), + }, + ]; + } ) + ); +} + function loadWebpackConfig( configPath, defaults ) { const config = require( configPath ); @@ -38,11 +81,58 @@ function loadWebpackConfig( configPath, defaults ) { }; } +/** + * Compiles a fixture as classic scripts or as script modules. + * + * @param {string} configCase Name of the fixture folder. + * @param {'scripts' | 'modules'} moduleMode Kind of output. + * @param {string} outputDirectory Folder to emit the build to. + * @return {Promise} Stats of the compilation. + */ +function compileFixture( configCase, moduleMode, outputDirectory ) { + const testDirectory = path.join( fixturesPath, configCase ); + const options = loadWebpackConfig( + path.join( testDirectory, 'webpack.config.js' ), + { + name: `${ configCase }-${ moduleMode }`, + target: 'web', + context: testDirectory, + entry: './index.js', + mode: 'production', + optimization: { + minimize: false, + chunkIds: 'named', + moduleIds: 'named', + }, + output: {}, + experiments: {}, + } + ); + options.output.path = outputDirectory; + + if ( moduleMode === 'modules' ) { + options.target = 'es2024'; + options.output.module = true; + options.output.chunkFormat = 'module'; + options.output.library = options.output.library || {}; + options.output.library.type = 'module'; + options.experiments.outputModule = true; + } + + return new Promise( ( resolve, reject ) => + webpack( options, ( err, stats ) => { + if ( err ) { + return reject( err ); + } + resolve( stats ); + } ) + ); +} + describe.each( /** @type {const} */ ( [ 'scripts', 'modules' ] ) )( 'DependencyExtractionWebpackPlugin %s', ( moduleMode ) => { describe.each( configFixtures )( 'Webpack `%s`', ( configCase ) => { - const testDirectory = path.join( fixturesPath, configCase ); const outputDirectory = path.join( currentDirectory, 'build', @@ -59,42 +149,10 @@ describe.each( /** @type {const} */ ( [ 'scripts', 'modules' ] ) )( afterEach( () => rimrafSync( outputDirectory ) ); test( 'should produce expected output', async () => { - const options = loadWebpackConfig( - path.join( testDirectory, 'webpack.config.js' ), - { - name: `${ configCase }-${ moduleMode }`, - target: 'web', - context: testDirectory, - entry: './index.js', - mode: 'production', - optimization: { - minimize: false, - chunkIds: 'named', - moduleIds: 'named', - }, - output: {}, - experiments: {}, - } - ); - options.output.path = outputDirectory; - - if ( moduleMode === 'modules' ) { - options.target = 'es2024'; - options.output.module = true; - options.output.chunkFormat = 'module'; - options.output.library = options.output.library || {}; - options.output.library.type = 'module'; - options.experiments.outputModule = true; - } - - /** @type {webpack.Stats} */ - const stats = await new Promise( ( resolve, reject ) => - webpack( options, ( err, _stats ) => { - if ( err ) { - return reject( err ); - } - resolve( _stats ); - } ) + const stats = await compileFixture( + configCase, + moduleMode, + outputDirectory ); if ( configCase.includes( 'error' ) ) { @@ -155,6 +213,44 @@ describe.each( /** @type {const} */ ( [ 'scripts', 'modules' ] ) )( } ); +describe( 'DependencyExtractionWebpackPlugin modules emitted imports', () => { + describe.each( + configFixtures.filter( + ( configCase ) => ! configCase.includes( 'error' ) + ) + )( 'Webpack `%s`', ( configCase ) => { + const outputDirectory = path.join( + currentDirectory, + 'build', + 'modules-emitted-imports', + configCase + ); + + beforeEach( () => { + rimrafSync( outputDirectory ); + mkdirpSync( outputDirectory ); + } ); + + afterEach( () => rimrafSync( outputDirectory ) ); + + test( 'should keep the kind of each import of an external', async () => { + const stats = await compileFixture( + configCase, + 'modules', + outputDirectory + ); + + if ( stats.hasErrors() ) { + throw new Error( + stats.toString( { errors: true, all: false } ) + ); + } + + expect( getEmittedImports( outputDirectory ) ).toMatchSnapshot(); + } ); + } ); +} ); + describe.each( /** @type {const} */ ( [ 'scripts', 'modules' ] ) )( 'DependencyExtractionWebpackPlugin %s asset version', ( moduleMode ) => { diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/dynamic.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/dynamic.js new file mode 100644 index 00000000000000..7ba1ec3fa42b3b --- /dev/null +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/dynamic.js @@ -0,0 +1,9 @@ +export async function navigate( url ) { + const { store } = await import( '@wordpress/interactivity' ); + const { actions } = await import( '@wordpress/interactivity-router' ); + const { speak } = await import( '@wordpress/a11y' ); + + store( 'dynamic', {} ); + await actions.navigate( url ); + speak( 'Navigated' ); +} diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/static.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/static.js new file mode 100644 index 00000000000000..29a76dd44d6063 --- /dev/null +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/static.js @@ -0,0 +1,5 @@ +import { store } from '@wordpress/interactivity'; +import '@wordpress/interactivity-router'; +import { speak } from '@wordpress/a11y'; + +store( 'static', { actions: { announce: () => speak( 'Hello' ) } } ); diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/webpack.config.js new file mode 100644 index 00000000000000..12c0421a59f72c --- /dev/null +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/webpack.config.js @@ -0,0 +1,9 @@ +const DependencyExtractionWebpackPlugin = require( '../../..' ); + +module.exports = { + entry: { + static: './static.js', + dynamic: './dynamic.js', + }, + plugins: [ new DependencyExtractionWebpackPlugin() ], +}; From 40b1d201870f3f94829c4f3743ad93914bc12649 Mon Sep 17 00:00:00 2001 From: luisherranz Date: Thu, 19 Nov 2026 15:42:26 +0100 Subject: [PATCH 02/12] Dependency Extraction: Fix Markdown lint errors in the README --- packages/dependency-extraction-webpack-plugin/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/dependency-extraction-webpack-plugin/README.md b/packages/dependency-extraction-webpack-plugin/README.md index 10369dbb6661b5..3494494185f6f9 100644 --- a/packages/dependency-extraction-webpack-plugin/README.md +++ b/packages/dependency-extraction-webpack-plugin/README.md @@ -68,7 +68,7 @@ The asset file name mirrors the emitted JavaScript file name, replacing the file For example, for an entry named `entrypoint`: -``` +```text // Entry `entrypoint` import { Component } from 'react'; @@ -138,7 +138,7 @@ Each entry point in the webpack bundle will include an asset file that declares For example, for an entry named `entrypoint`: -``` +```text // Entry `entrypoint` import { store, getContext } from '@wordpress/interactivity'; @@ -228,7 +228,7 @@ You can provide a filename, or set it to `true` to report to a default `external ##### `requestToExternal` -**Note**: This option is not available with script modules. See [`requestToExternalModule`](#requestToExternalModule) for module usage. +**Note**: This option is not available with script modules. See [`requestToExternalModule`](#requesttoexternalmodule) for module usage. - Type: function @@ -259,7 +259,7 @@ module.exports = { ##### `requestToExternalModule` -**Note**: This option is only available with script modules. See [`requestToExternal`](#requestToExternal) for script usage. +**Note**: This option is only available with script modules. See [`requestToExternal`](#requesttoexternal) for script usage. - Type: function From 172e3835e743a64fc4f47ebe9a4e581e5bcb77d6 Mon Sep 17 00:00:00 2001 From: luisherranz Date: Thu, 19 Nov 2026 15:42:27 +0100 Subject: [PATCH 03/12] Dependency Extraction: Require webpack 5.95.0 --- package-lock.json | 2 +- packages/dependency-extraction-webpack-plugin/README.md | 2 +- packages/dependency-extraction-webpack-plugin/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0404bbc79d20bb..28cb4f58abe4b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -45578,7 +45578,7 @@ "node": ">=22.13.0" }, "peerDependencies": { - "webpack": "^5.0.0" + "webpack": "^5.95.0" } }, "packages/dependency-extraction-webpack-plugin/node_modules/balanced-match": { diff --git a/packages/dependency-extraction-webpack-plugin/README.md b/packages/dependency-extraction-webpack-plugin/README.md index 3494494185f6f9..22d17b64c2f0e4 100644 --- a/packages/dependency-extraction-webpack-plugin/README.md +++ b/packages/dependency-extraction-webpack-plugin/README.md @@ -21,7 +21,7 @@ Install the module npm install @wordpress/dependency-extraction-webpack-plugin --save-dev ``` -**Note**: This package requires Node.js version with long-term support status (check [Active LTS or Maintenance LTS releases](https://nodejs.org/en/about/previous-releases)). It also requires webpack 5.0.0 or newer. It is not compatible with older versions. +**Note**: This package requires Node.js version with long-term support status (check [Active LTS or Maintenance LTS releases](https://nodejs.org/en/about/previous-releases)). It also requires webpack 5.95.0 or newer. It is not compatible with older versions. ## Usage diff --git a/packages/dependency-extraction-webpack-plugin/package.json b/packages/dependency-extraction-webpack-plugin/package.json index 05d1f7bd12cf0a..9e231e41f30ed1 100644 --- a/packages/dependency-extraction-webpack-plugin/package.json +++ b/packages/dependency-extraction-webpack-plugin/package.json @@ -51,7 +51,7 @@ "webpack": "^5.111.0" }, "peerDependencies": { - "webpack": "^5.0.0" + "webpack": "^5.95.0" }, "publishConfig": { "access": "public" From f545aad74c4d1919cf420f59ef3ed675f2684611 Mon Sep 17 00:00:00 2001 From: luisherranz Date: Thu, 19 Nov 2026 15:42:31 +0100 Subject: [PATCH 04/12] Dependency Extraction: Document the import kinds of script module externals --- .../dependency-extraction-webpack-plugin/CHANGELOG.md | 9 +++++++++ .../dependency-extraction-webpack-plugin/README.md | 10 ++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/dependency-extraction-webpack-plugin/CHANGELOG.md b/packages/dependency-extraction-webpack-plugin/CHANGELOG.md index d3443ac7f62169..6662e8b6f844fe 100644 --- a/packages/dependency-extraction-webpack-plugin/CHANGELOG.md +++ b/packages/dependency-extraction-webpack-plugin/CHANGELOG.md @@ -2,6 +2,15 @@ ## Unreleased +### Breaking Changes + +- Require webpack 5.95.0 or newer ([#00000](https://github.com/WordPress/gutenberg/pull/00000)). +- `defaultRequestToExternalModule`, exported from `lib/util`, returns script module IDs without an externals type prefix ([#00000](https://github.com/WordPress/gutenberg/pull/00000)). + +### Enhancements + +- In script module builds, a static `import` of an externalized script module stays a static import, and an `import()` of it stays an `import()` unless the same file also imports it statically, `@wordpress/interactivity` included ([#00000](https://github.com/WordPress/gutenberg/pull/00000)). + ## 7.2.0 (2026-11-18) ### Enhancements diff --git a/packages/dependency-extraction-webpack-plugin/README.md b/packages/dependency-extraction-webpack-plugin/README.md index 22d17b64c2f0e4..5794e75d273a3f 100644 --- a/packages/dependency-extraction-webpack-plugin/README.md +++ b/packages/dependency-extraction-webpack-plugin/README.md @@ -152,11 +152,13 @@ import { store, getContext } from '@wordpress/interactivity'; By default, the following script module requests are handled: -| Request | -| ---------------------------- | -| `@wordpress/interactivity ` | +| Request | +| --------------------------------- | +| `@wordpress/a11y` | +| `@wordpress/interactivity` | +| `@wordpress/interactivity-router` | -(`@wordpress/interactivity` is currently the only available WordPress script module.) +Imports of an externalized script module keep their kind in the bundle: a static `import` declaration stays a static import, and an `import()` stays an `import()` of the script module, which loads it when that code runs. When one file both imports a script module statically and calls `import()` with it, both use the static import. **Note:** This plugin overlaps with the functionality provided by [webpack `externals`](https://webpack.js.org/configuration/externals). This plugin is intended to extract script module identifiers from bundle compilation so that a list of script module dependencies does not need to be manually maintained. If you don't need to extract a list of script module dependencies, use the `externals` option directly. From 6ae307c5b2b10aa93127432acc5fd9bc9b4a8a5f Mon Sep 17 00:00:00 2001 From: luisherranz Date: Thu, 19 Nov 2026 15:42:33 +0100 Subject: [PATCH 05/12] Dependency Extraction: Describe how one file's imports share a kind --- packages/dependency-extraction-webpack-plugin/CHANGELOG.md | 2 +- packages/dependency-extraction-webpack-plugin/lib/index.js | 7 ++++--- packages/dependency-extraction-webpack-plugin/lib/util.js | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/dependency-extraction-webpack-plugin/CHANGELOG.md b/packages/dependency-extraction-webpack-plugin/CHANGELOG.md index 6662e8b6f844fe..e84e75ba2dba1a 100644 --- a/packages/dependency-extraction-webpack-plugin/CHANGELOG.md +++ b/packages/dependency-extraction-webpack-plugin/CHANGELOG.md @@ -9,7 +9,7 @@ ### Enhancements -- In script module builds, a static `import` of an externalized script module stays a static import, and an `import()` of it stays an `import()` unless the same file also imports it statically, `@wordpress/interactivity` included ([#00000](https://github.com/WordPress/gutenberg/pull/00000)). +- Keep the kind of each import of an externalized script module in script module builds: a static `import` stays static and an `import()` stays dynamic, `@wordpress/interactivity` included. In a file that also imports the script module statically, an `import()` of it uses the static import ([#00000](https://github.com/WordPress/gutenberg/pull/00000)). ## 7.2.0 (2026-11-18) diff --git a/packages/dependency-extraction-webpack-plugin/lib/index.js b/packages/dependency-extraction-webpack-plugin/lib/index.js index ceb78b849dbf50..4eb567ff2cf0d0 100644 --- a/packages/dependency-extraction-webpack-plugin/lib/index.js +++ b/packages/dependency-extraction-webpack-plugin/lib/index.js @@ -157,9 +157,10 @@ class DependencyExtractionWebpackPlugin { /** * Offload externalization work to the ExternalsPlugin. * - * Script modules use the `module-import` type, which keeps the kind of - * each import: a static `import` declaration stays static and an - * `import()` stays dynamic. + * Script modules use the `module-import` type, so a static `import` + * declaration of an external stays static and an `import()` of it stays + * dynamic. Webpack externalizes the imports of one request in one file + * together, so a file that has both gets the static import for both. * * @type {webpack.ExternalsPlugin} */ diff --git a/packages/dependency-extraction-webpack-plugin/lib/util.js b/packages/dependency-extraction-webpack-plugin/lib/util.js index e5f4e876635f8e..bee590050b5891 100644 --- a/packages/dependency-extraction-webpack-plugin/lib/util.js +++ b/packages/dependency-extraction-webpack-plugin/lib/util.js @@ -78,8 +78,8 @@ function defaultRequestToExternal( request ) { * * Currently only `@wordpress/interactivity`, `@wordpress/interactivity-router`, * and `@wordpress/a11y` are supported. Each maps to its own script module ID, - * which every import of it keeps: a static `import` declaration stays static - * and an `import()` stays dynamic. + * without an externals type, so each import of it takes its kind from the + * plugin's externals type. * * Do not use the boolean shorthand here, it's only handled for the * `requestToExternalModule` option. From 305249ba24c0c9bd57bc2a9b01aba33da226bd27 Mon Sep 17 00:00:00 2001 From: luisherranz Date: Thu, 19 Nov 2026 15:42:39 +0100 Subject: [PATCH 06/12] Dependency Extraction: Keep both import kinds of a script module in one file --- .../lib/index.js | 132 ++++++++++++++---- .../test/__snapshots__/build.js.snap | 66 +++++++++ .../test/build.js | 74 +++++++++- .../wordpress-script-modules/announce.js | 4 + .../both-kinds-split.js | 6 + .../wordpress-script-modules/both-kinds.js | 8 ++ .../webpack.config.js | 2 + 7 files changed, 258 insertions(+), 34 deletions(-) create mode 100644 packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/announce.js create mode 100644 packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/both-kinds-split.js create mode 100644 packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/both-kinds.js diff --git a/packages/dependency-extraction-webpack-plugin/lib/index.js b/packages/dependency-extraction-webpack-plugin/lib/index.js index 4eb567ff2cf0d0..30db3ecacc7dcc 100644 --- a/packages/dependency-extraction-webpack-plugin/lib/index.js +++ b/packages/dependency-extraction-webpack-plugin/lib/index.js @@ -56,42 +56,56 @@ class DependencyExtractionWebpackPlugin { } /** - * @param {webpack.ExternalItemFunctionData} data - * @param { ( err?: null | Error, result?: string | string[] ) => void } callback + * Map a request to the external that the plugin externalizes it to. + * + * @param {string} request Requested module. + * @return {string|string[]|undefined} The external, or `undefined` when the + * plugin does not externalize the request. + * @throws {Error} When the request can't be used, like a WordPress script in a script module. */ - externalizeWpDeps( { request }, callback ) { + mapRequestToExternal( request ) { let externalRequest; - try { - // Handle via options.requestToExternal(Module) first. - if ( this.useModules ) { - if ( - typeof this.options.requestToExternalModule === 'function' - ) { - externalRequest = - this.options.requestToExternalModule( request ); + // Handle via options.requestToExternal(Module) first. + if ( this.useModules ) { + if ( typeof this.options.requestToExternalModule === 'function' ) { + externalRequest = + this.options.requestToExternalModule( request ); - // requestToExternalModule allows a boolean shorthand - if ( externalRequest === false ) { - externalRequest = undefined; - } - if ( externalRequest === true ) { - externalRequest = request; - } + // requestToExternalModule allows a boolean shorthand + if ( externalRequest === false ) { + externalRequest = undefined; + } + if ( externalRequest === true ) { + externalRequest = request; } - } else if ( typeof this.options.requestToExternal === 'function' ) { - externalRequest = this.options.requestToExternal( request ); } + } else if ( typeof this.options.requestToExternal === 'function' ) { + externalRequest = this.options.requestToExternal( request ); + } - // Cascade to default if unhandled and enabled. - if ( - typeof externalRequest === 'undefined' && - this.options.useDefaults - ) { - externalRequest = this.useModules - ? defaultRequestToExternalModule( request ) - : defaultRequestToExternal( request ); - } + // Cascade to default if unhandled and enabled. + if ( + typeof externalRequest === 'undefined' && + this.options.useDefaults + ) { + externalRequest = this.useModules + ? defaultRequestToExternalModule( request ) + : defaultRequestToExternal( request ); + } + + return externalRequest; + } + + /** + * @param {webpack.ExternalItemFunctionData} data + * @param { ( err?: null | Error, result?: string | string[] ) => void } callback + */ + externalizeWpDeps( { request }, callback ) { + let externalRequest; + + try { + externalRequest = this.mapRequestToExternal( request ); } catch ( err ) { return callback( err ); } @@ -105,6 +119,54 @@ class DependencyExtractionWebpackPlugin { return callback(); } + /** + * Keep each `import()` of an externalized request in a module apart from + * the module's static imports of the same request. + * + * Webpack externalizes the dependencies of one module that share a + * resource identifier together, with the kind of the first one, so a + * static import and an `import()` of one request would both come out + * static. Giving the `import()` its own resource identifier makes webpack + * externalize it on its own, with the dynamic kind. The dependency keeps + * that identifier when a rebuild reuses the module from the memory cache. + * + * @param {webpack.Module} module Module whose dependencies webpack is about to resolve. + */ + separateDynamicImports( module ) { + const blocks = [ ...module.blocks ]; + while ( blocks.length ) { + const block = blocks.pop(); + blocks.push( ...block.blocks ); + + for ( const dependency of block.dependencies ) { + // Webpack types the dependency of an `import()` of a string as `import()`. + if ( + dependency.type !== 'import()' || + ! this.isExternalized( dependency.request ) + ) { + continue; + } + + const resourceIdentifier = dependency.getResourceIdentifier(); + dependency.getResourceIdentifier = () => + `${ resourceIdentifier }|import()`; + } + } + } + + /** + * @param {string} request Requested module. + * @return {boolean} Whether the plugin externalizes the request. + */ + isExternalized( request ) { + try { + return Boolean( this.mapRequestToExternal( request ) ); + } catch { + // Webpack reports the error when it resolves the request. + return false; + } + } + /** * @param {string} request * @return {string} Mapped dependency name @@ -159,8 +221,7 @@ class DependencyExtractionWebpackPlugin { * * Script modules use the `module-import` type, so a static `import` * declaration of an external stays static and an `import()` of it stays - * dynamic. Webpack externalizes the imports of one request in one file - * together, so a file that has both gets the static import for both. + * dynamic (see `separateDynamicImports` for a file that has both). * * @type {webpack.ExternalsPlugin} */ @@ -174,6 +235,15 @@ class DependencyExtractionWebpackPlugin { compiler.hooks.thisCompilation.tap( this.constructor.name, ( compilation ) => { + if ( this.useModules ) { + // Runs after webpack parses a module and before it resolves + // the module's dependencies. + compilation.hooks.succeedModule.tap( + this.constructor.name, + ( module ) => this.separateDynamicImports( module ) + ); + } + compilation.hooks.processAssets.tap( { name: this.constructor.name, diff --git a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap index 01e53b13bc3431..32263235d78e79 100644 --- a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap +++ b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap @@ -597,6 +597,36 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-require ] `; +exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-script-modules\` > should produce expected output > Asset file 'both-kinds.asset.php' should match snapshot 1`] = ` +" array( + '@wordpress/a11y', + array( + 'id' => '@wordpress/a11y', + 'import' => 'dynamic' + ) + ), + 'version' => '5aec48e840a9ed83ef2f', + 'type' => 'module' +); +" +`; + +exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-script-modules\` > should produce expected output > Asset file 'both-kinds-split.asset.php' should match snapshot 1`] = ` +" array( + '@wordpress/a11y', + array( + 'id' => '@wordpress/a11y', + 'import' => 'dynamic' + ) + ), + 'version' => '459f95ddfe8ec010458f', + 'type' => 'module' +); +" +`; + exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-script-modules\` > should produce expected output > Asset file 'dynamic.asset.php' should match snapshot 1`] = ` " array( @@ -957,6 +987,22 @@ exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`w exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`wordpress-script-modules\` > should keep the kind of each import of an external 1`] = ` { + "both-kinds-split.mjs": { + "dynamic": [ + "@wordpress/a11y", + ], + "static": [ + "@wordpress/a11y", + ], + }, + "both-kinds.mjs": { + "dynamic": [ + "@wordpress/a11y", + ], + "static": [ + "@wordpress/a11y", + ], + }, "dynamic.mjs": { "dynamic": [ "@wordpress/a11y", @@ -1594,6 +1640,26 @@ exports[`DependencyExtractionWebpackPlugin scripts > Webpack \`wordpress-require ] `; +exports[`DependencyExtractionWebpackPlugin scripts > Webpack \`wordpress-script-modules\` > should produce expected output > Asset file 'both-kinds.asset.php' should match snapshot 1`] = ` +" array( + 'wp-a11y' + ), + 'version' => 'eec39a5174acfc781251' +); +" +`; + +exports[`DependencyExtractionWebpackPlugin scripts > Webpack \`wordpress-script-modules\` > should produce expected output > Asset file 'both-kinds-split.asset.php' should match snapshot 1`] = ` +" array( + 'wp-a11y' + ), + 'version' => 'fdbb65633273fe53f799' +); +" +`; + exports[`DependencyExtractionWebpackPlugin scripts > Webpack \`wordpress-script-modules\` > should produce expected output > Asset file 'dynamic.asset.php' should match snapshot 1`] = ` " array( diff --git a/packages/dependency-extraction-webpack-plugin/test/build.js b/packages/dependency-extraction-webpack-plugin/test/build.js index 41ae74db4fc6be..5e3b887f4babec 100644 --- a/packages/dependency-extraction-webpack-plugin/test/build.js +++ b/packages/dependency-extraction-webpack-plugin/test/build.js @@ -82,14 +82,15 @@ function loadWebpackConfig( configPath, defaults ) { } /** - * Compiles a fixture as classic scripts or as script modules. + * Returns the webpack options that build a fixture as classic scripts or as + * script modules. * * @param {string} configCase Name of the fixture folder. * @param {'scripts' | 'modules'} moduleMode Kind of output. * @param {string} outputDirectory Folder to emit the build to. - * @return {Promise} Stats of the compilation. + * @return {webpack.Configuration} Webpack options. */ -function compileFixture( configCase, moduleMode, outputDirectory ) { +function getFixtureOptions( configCase, moduleMode, outputDirectory ) { const testDirectory = path.join( fixturesPath, configCase ); const options = loadWebpackConfig( path.join( testDirectory, 'webpack.config.js' ), @@ -119,6 +120,24 @@ function compileFixture( configCase, moduleMode, outputDirectory ) { options.experiments.outputModule = true; } + return options; +} + +/** + * Compiles a fixture as classic scripts or as script modules. + * + * @param {string} configCase Name of the fixture folder. + * @param {'scripts' | 'modules'} moduleMode Kind of output. + * @param {string} outputDirectory Folder to emit the build to. + * @return {Promise} Stats of the compilation. + */ +function compileFixture( configCase, moduleMode, outputDirectory ) { + const options = getFixtureOptions( + configCase, + moduleMode, + outputDirectory + ); + return new Promise( ( resolve, reject ) => webpack( options, ( err, stats ) => { if ( err ) { @@ -251,6 +270,55 @@ describe( 'DependencyExtractionWebpackPlugin modules emitted imports', () => { } ); } ); +describe( 'DependencyExtractionWebpackPlugin modules rebuild', () => { + const configCase = 'wordpress-script-modules'; + const outputDirectory = path.join( + currentDirectory, + 'build', + 'modules-rebuild', + configCase + ); + + beforeEach( () => { + rimrafSync( outputDirectory ); + mkdirpSync( outputDirectory ); + } ); + + afterEach( () => rimrafSync( outputDirectory ) ); + + test( 'should keep both import kinds of an unchanged file', async () => { + // Keep the modules in memory between compilations, as watch rebuilds do. + const compiler = webpack( { + ...getFixtureOptions( configCase, 'modules', outputDirectory ), + cache: true, + } ); + const run = () => + new Promise( ( resolve, reject ) => + compiler.run( ( err, stats ) => + err ? reject( err ) : resolve( stats ) + ) + ); + + await run(); + const stats = await run(); + await new Promise( ( resolve ) => compiler.close( resolve ) ); + + const builtFiles = Array.from( + stats.compilation.builtModules, + ( { resource } ) => resource + ); + expect( builtFiles ).not.toContain( + path.join( fixturesPath, configCase, 'both-kinds.js' ) + ); + expect( + getEmittedImports( outputDirectory )[ 'both-kinds.mjs' ] + ).toEqual( { + static: [ '@wordpress/a11y' ], + dynamic: [ '@wordpress/a11y' ], + } ); + } ); +} ); + describe.each( /** @type {const} */ ( [ 'scripts', 'modules' ] ) )( 'DependencyExtractionWebpackPlugin %s asset version', ( moduleMode ) => { diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/announce.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/announce.js new file mode 100644 index 00000000000000..5b42efe64c0d3d --- /dev/null +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/announce.js @@ -0,0 +1,4 @@ +export async function announce( message ) { + const { speak } = await import( '@wordpress/a11y' ); + speak( message ); +} diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/both-kinds-split.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/both-kinds-split.js new file mode 100644 index 00000000000000..b383a1f6dbfcd6 --- /dev/null +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/both-kinds-split.js @@ -0,0 +1,6 @@ +import { speak } from '@wordpress/a11y'; +import { announce } from './announce.js'; + +speak( 'Loaded' ); + +export { announce }; diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/both-kinds.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/both-kinds.js new file mode 100644 index 00000000000000..5578d6250c380e --- /dev/null +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/both-kinds.js @@ -0,0 +1,8 @@ +import { speak } from '@wordpress/a11y'; + +speak( 'Loaded' ); + +export async function announce( message ) { + const a11y = await import( '@wordpress/a11y' ); + a11y.speak( message ); +} diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/webpack.config.js index 12c0421a59f72c..1d303dd6a4152b 100644 --- a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/webpack.config.js +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules/webpack.config.js @@ -4,6 +4,8 @@ module.exports = { entry: { static: './static.js', dynamic: './dynamic.js', + 'both-kinds': './both-kinds.js', + 'both-kinds-split': './both-kinds-split.js', }, plugins: [ new DependencyExtractionWebpackPlugin() ], }; From 887a892cdb26d34d2197bd92a2d1d9e8f44107ff Mon Sep 17 00:00:00 2001 From: luisherranz Date: Thu, 19 Nov 2026 15:42:46 +0100 Subject: [PATCH 07/12] Dependency Extraction: Document that one file keeps both import kinds --- packages/dependency-extraction-webpack-plugin/CHANGELOG.md | 2 +- packages/dependency-extraction-webpack-plugin/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/dependency-extraction-webpack-plugin/CHANGELOG.md b/packages/dependency-extraction-webpack-plugin/CHANGELOG.md index e84e75ba2dba1a..5286bd43a5fb01 100644 --- a/packages/dependency-extraction-webpack-plugin/CHANGELOG.md +++ b/packages/dependency-extraction-webpack-plugin/CHANGELOG.md @@ -9,7 +9,7 @@ ### Enhancements -- Keep the kind of each import of an externalized script module in script module builds: a static `import` stays static and an `import()` stays dynamic, `@wordpress/interactivity` included. In a file that also imports the script module statically, an `import()` of it uses the static import ([#00000](https://github.com/WordPress/gutenberg/pull/00000)). +- Keep the kind of each import of an externalized script module in script module builds: a static `import` stays static and an `import()` stays dynamic, `@wordpress/interactivity` included ([#00000](https://github.com/WordPress/gutenberg/pull/00000)). ## 7.2.0 (2026-11-18) diff --git a/packages/dependency-extraction-webpack-plugin/README.md b/packages/dependency-extraction-webpack-plugin/README.md index 5794e75d273a3f..f844d58deb8798 100644 --- a/packages/dependency-extraction-webpack-plugin/README.md +++ b/packages/dependency-extraction-webpack-plugin/README.md @@ -158,7 +158,7 @@ By default, the following script module requests are handled: | `@wordpress/interactivity` | | `@wordpress/interactivity-router` | -Imports of an externalized script module keep their kind in the bundle: a static `import` declaration stays a static import, and an `import()` stays an `import()` of the script module, which loads it when that code runs. When one file both imports a script module statically and calls `import()` with it, both use the static import. +Imports of an externalized script module keep their kind in the bundle: a static `import` declaration stays a static import, and an `import()` stays an `import()` of the script module, which loads it when that code runs. **Note:** This plugin overlaps with the functionality provided by [webpack `externals`](https://webpack.js.org/configuration/externals). This plugin is intended to extract script module identifiers from bundle compilation so that a list of script module dependencies does not need to be manually maintained. If you don't need to extract a list of script module dependencies, use the `externals` option directly. From 7f1985b5662502aa74da0d37ac86a81b98eca33a Mon Sep 17 00:00:00 2001 From: luisherranz Date: Thu, 19 Nov 2026 15:42:47 +0100 Subject: [PATCH 08/12] Dependency Extraction: Explain the cache check of the rebuild test --- packages/dependency-extraction-webpack-plugin/test/build.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/dependency-extraction-webpack-plugin/test/build.js b/packages/dependency-extraction-webpack-plugin/test/build.js index 5e3b887f4babec..07082ec0ebca83 100644 --- a/packages/dependency-extraction-webpack-plugin/test/build.js +++ b/packages/dependency-extraction-webpack-plugin/test/build.js @@ -303,6 +303,7 @@ describe( 'DependencyExtractionWebpackPlugin modules rebuild', () => { const stats = await run(); await new Promise( ( resolve ) => compiler.close( resolve ) ); + // The second compilation reuses the file's module from the cache. const builtFiles = Array.from( stats.compilation.builtModules, ( { resource } ) => resource From 529406d04ac2d369f012203db73938eca0f67a26 Mon Sep 17 00:00:00 2001 From: luisherranz Date: Thu, 19 Nov 2026 15:42:54 +0100 Subject: [PATCH 09/12] Dependency Extraction: Preserve static imports alongside eager dynamic imports --- .../lib/index.js | 18 ++++-- .../test/__snapshots__/build.js.snap | 62 +++++++++++++++++++ .../wordpress-script-modules-eager/index.js | 8 +++ .../webpack.config.js | 5 ++ 4 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules-eager/index.js create mode 100644 packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules-eager/webpack.config.js diff --git a/packages/dependency-extraction-webpack-plugin/lib/index.js b/packages/dependency-extraction-webpack-plugin/lib/index.js index 30db3ecacc7dcc..9243e6044b6b9c 100644 --- a/packages/dependency-extraction-webpack-plugin/lib/index.js +++ b/packages/dependency-extraction-webpack-plugin/lib/index.js @@ -125,23 +125,31 @@ class DependencyExtractionWebpackPlugin { * * Webpack externalizes the dependencies of one module that share a * resource identifier together, with the kind of the first one, so a - * static import and an `import()` of one request would both come out - * static. Giving the `import()` its own resource identifier makes webpack + * static import and an `import()` of one request would get one kind. + * Giving the `import()` its own resource identifier makes webpack * externalize it on its own, with the dynamic kind. The dependency keeps * that identifier when a rebuild reuses the module from the memory cache. * + * A `webpackMode: "weak"` `import()` stays with the static imports: on its + * own, it would point to an external that no chunk loads, so it would + * always reject. + * * @param {webpack.Module} module Module whose dependencies webpack is about to resolve. */ separateDynamicImports( module ) { - const blocks = [ ...module.blocks ]; + const blocks = [ module ]; while ( blocks.length ) { const block = blocks.pop(); blocks.push( ...block.blocks ); for ( const dependency of block.dependencies ) { - // Webpack types the dependency of an `import()` of a string as `import()`. + // Webpack types the dependency of an `import()` of a string as + // `import()`, or as `import() eager` in eager mode, where the + // module holds it instead of an async block. if ( - dependency.type !== 'import()' || + ! [ 'import()', 'import() eager' ].includes( + dependency.type + ) || ! this.isExternalized( dependency.request ) ) { continue; diff --git a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap index 32263235d78e79..74049538a01073 100644 --- a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap +++ b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap @@ -697,6 +697,32 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-script- ] `; +exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-script-modules-eager\` > should produce expected output > Asset file 'main.asset.php' should match snapshot 1`] = ` +" array( + '@wordpress/a11y' + ), + 'version' => 'a61fcddbcf32f703c0f3', + 'type' => 'module' +); +" +`; + +exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-script-modules-eager\` > should produce expected output > External modules should match snapshot 1`] = ` +[ + { + "externalType": "module-import", + "request": "@wordpress/a11y", + "userRequest": "@wordpress/a11y", + }, + { + "externalType": "module-import", + "request": "@wordpress/a11y", + "userRequest": "@wordpress/a11y", + }, +] +`; + exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`combine-assets\` > should keep the kind of each import of an external 1`] = ` { "fileA.mjs": { @@ -1022,6 +1048,19 @@ exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`w } `; +exports[`DependencyExtractionWebpackPlugin modules emitted imports > Webpack \`wordpress-script-modules-eager\` > should keep the kind of each import of an external 1`] = ` +{ + "main.mjs": { + "dynamic": [ + "@wordpress/a11y", + ], + "static": [ + "@wordpress/a11y", + ], + }, +} +`; + exports[`DependencyExtractionWebpackPlugin scripts > Webpack \`combine-assets\` > should produce expected output > Asset file 'assets.php' should match snapshot 1`] = ` " array( @@ -1712,3 +1751,26 @@ exports[`DependencyExtractionWebpackPlugin scripts > Webpack \`wordpress-script- }, ] `; + +exports[`DependencyExtractionWebpackPlugin scripts > Webpack \`wordpress-script-modules-eager\` > should produce expected output > Asset file 'main.asset.php' should match snapshot 1`] = ` +" array( + 'wp-a11y' + ), + 'version' => '73fdfd5f16ac27ad0c7c' +); +" +`; + +exports[`DependencyExtractionWebpackPlugin scripts > Webpack \`wordpress-script-modules-eager\` > should produce expected output > External modules should match snapshot 1`] = ` +[ + { + "externalType": "window", + "request": [ + "wp", + "a11y", + ], + "userRequest": "@wordpress/a11y", + }, +] +`; diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules-eager/index.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules-eager/index.js new file mode 100644 index 00000000000000..d3276d6eb8de17 --- /dev/null +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules-eager/index.js @@ -0,0 +1,8 @@ +import { speak } from '@wordpress/a11y'; + +speak( 'Loaded' ); + +export async function announce( message ) { + const a11y = await import( /* webpackMode: "eager" */ '@wordpress/a11y' ); + a11y.speak( message ); +} diff --git a/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules-eager/webpack.config.js b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules-eager/webpack.config.js new file mode 100644 index 00000000000000..ca6a3e9e85a75d --- /dev/null +++ b/packages/dependency-extraction-webpack-plugin/test/fixtures/wordpress-script-modules-eager/webpack.config.js @@ -0,0 +1,5 @@ +const DependencyExtractionWebpackPlugin = require( '../../..' ); + +module.exports = { + plugins: [ new DependencyExtractionWebpackPlugin() ], +}; From 1dcbf6f2e57ccc32d19923f5906f88d94699fd64 Mon Sep 17 00:00:00 2001 From: luisherranz Date: Thu, 19 Nov 2026 15:43:00 +0100 Subject: [PATCH 10/12] Dependency Extraction: Check that the rebuild test reuses the cached module --- .../test/build.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/dependency-extraction-webpack-plugin/test/build.js b/packages/dependency-extraction-webpack-plugin/test/build.js index 07082ec0ebca83..1cd3bdfa8d6244 100644 --- a/packages/dependency-extraction-webpack-plugin/test/build.js +++ b/packages/dependency-extraction-webpack-plugin/test/build.js @@ -304,13 +304,16 @@ describe( 'DependencyExtractionWebpackPlugin modules rebuild', () => { await new Promise( ( resolve ) => compiler.close( resolve ) ); // The second compilation reuses the file's module from the cache. - const builtFiles = Array.from( - stats.compilation.builtModules, - ( { resource } ) => resource - ); - expect( builtFiles ).not.toContain( - path.join( fixturesPath, configCase, 'both-kinds.js' ) - ); + const { modules } = stats.toJson( { + all: false, + modules: true, + nestedModules: true, + cachedModules: true, + } ); + const bothKindsModule = modules + .flatMap( ( module ) => [ module, ...( module.modules ?? [] ) ] ) + .find( ( { name } ) => name === './both-kinds.js' ); + expect( bothKindsModule ).toMatchObject( { built: false } ); expect( getEmittedImports( outputDirectory )[ 'both-kinds.mjs' ] ).toEqual( { From 7e13706c582ae7d69df81a14a81b1a77fc8a9f76 Mon Sep 17 00:00:00 2001 From: luisherranz Date: Thu, 19 Nov 2026 15:43:07 +0100 Subject: [PATCH 11/12] Dependency Extraction: List each dependency once in its asset file --- .../lib/index.js | 12 ++++-------- .../test/__snapshots__/build.js.snap | 12 ++---------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/packages/dependency-extraction-webpack-plugin/lib/index.js b/packages/dependency-extraction-webpack-plugin/lib/index.js index 9243e6044b6b9c..3d6cc327202b81 100644 --- a/packages/dependency-extraction-webpack-plugin/lib/index.js +++ b/packages/dependency-extraction-webpack-plugin/lib/index.js @@ -132,7 +132,8 @@ class DependencyExtractionWebpackPlugin { * * A `webpackMode: "weak"` `import()` stays with the static imports: on its * own, it would point to an external that no chunk loads, so it would - * always reject. + * always reject. Grouped, it comes first, so the group, the file's static + * imports of the request included, gets the dynamic kind. * * @param {webpack.Module} module Module whose dependencies webpack is about to resolve. */ @@ -421,13 +422,6 @@ class DependencyExtractionWebpackPlugin { const { userRequest } = m; if ( this.externalizedDeps.has( userRequest ) ) { if ( this.useModules ) { - // A static occurrence (in the entry chunk) wins. - if ( - fromAsyncChunk && - chunkStaticDeps.has( m.request ) - ) { - return; - } // Externals in an async chunk are reached via a dynamic // import by definition. const isStatic = @@ -550,6 +544,8 @@ class DependencyExtractionWebpackPlugin { // Sort these so we can produce a stable, stringified representation. ...Array.from( chunkStaticDeps ).sort(), ...Array.from( chunkDynamicDeps ) + // A static occurrence of a script module wins. + .filter( ( id ) => ! chunkStaticDeps.has( id ) ) .sort() .map( ( id ) => ( { id, import: 'dynamic' } ) ), ], diff --git a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap index 74049538a01073..7dfd7dd687b4ed 100644 --- a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap +++ b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap @@ -600,11 +600,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-require exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-script-modules\` > should produce expected output > Asset file 'both-kinds.asset.php' should match snapshot 1`] = ` " array( - '@wordpress/a11y', - array( - 'id' => '@wordpress/a11y', - 'import' => 'dynamic' - ) + '@wordpress/a11y' ), 'version' => '5aec48e840a9ed83ef2f', 'type' => 'module' @@ -615,11 +611,7 @@ exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-script- exports[`DependencyExtractionWebpackPlugin modules > Webpack \`wordpress-script-modules\` > should produce expected output > Asset file 'both-kinds-split.asset.php' should match snapshot 1`] = ` " array( - '@wordpress/a11y', - array( - 'id' => '@wordpress/a11y', - 'import' => 'dynamic' - ) + '@wordpress/a11y' ), 'version' => '459f95ddfe8ec010458f', 'type' => 'module' From 414e4a6400a2e3c72cd50e048b91928abdeb1f7b Mon Sep 17 00:00:00 2001 From: luisherranz Date: Thu, 19 Nov 2026 15:44:22 +0100 Subject: [PATCH 12/12] Dependency Extraction: Link the changelog to the pull request --- packages/dependency-extraction-webpack-plugin/CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/dependency-extraction-webpack-plugin/CHANGELOG.md b/packages/dependency-extraction-webpack-plugin/CHANGELOG.md index 5286bd43a5fb01..a9755558664659 100644 --- a/packages/dependency-extraction-webpack-plugin/CHANGELOG.md +++ b/packages/dependency-extraction-webpack-plugin/CHANGELOG.md @@ -4,12 +4,12 @@ ### Breaking Changes -- Require webpack 5.95.0 or newer ([#00000](https://github.com/WordPress/gutenberg/pull/00000)). -- `defaultRequestToExternalModule`, exported from `lib/util`, returns script module IDs without an externals type prefix ([#00000](https://github.com/WordPress/gutenberg/pull/00000)). +- Require webpack 5.95.0 or newer ([#87098](https://github.com/WordPress/gutenberg/pull/87098)). +- `defaultRequestToExternalModule`, exported from `lib/util`, returns script module IDs without an externals type prefix ([#87098](https://github.com/WordPress/gutenberg/pull/87098)). ### Enhancements -- Keep the kind of each import of an externalized script module in script module builds: a static `import` stays static and an `import()` stays dynamic, `@wordpress/interactivity` included ([#00000](https://github.com/WordPress/gutenberg/pull/00000)). +- Keep the kind of each import of an externalized script module in script module builds: a static `import` stays static and an `import()` stays dynamic, `@wordpress/interactivity` included ([#87098](https://github.com/WordPress/gutenberg/pull/87098)). ## 7.2.0 (2026-11-18)