Skip to content

Dependency Extraction Webpack Plugin: improved script module externalization#78886

Open
joshualip-plaudit wants to merge 3 commits into
WordPress:fix/dep-extraction-script-module-externalsfrom
joshualip-plaudit:patch-2
Open

Dependency Extraction Webpack Plugin: improved script module externalization#78886
joshualip-plaudit wants to merge 3 commits into
WordPress:fix/dep-extraction-script-module-externalsfrom
joshualip-plaudit:patch-2

Conversation

@joshualip-plaudit

Copy link
Copy Markdown
Contributor

What?

Enhances PR #76397

This fixes the handling modules that are imported both statically and dynamically in the same project and adds detection of dynamic imports that are inside functions.

Why?

Script module externalization would be incomplete without this

How?

I modified the index.js file based on the work done by @jorgefilipecosta and a bunch of brute-force testing of how webpack records dynamic dependencies of my own

Testing Instructions

Build a project with the following files:

File 1:

import * as test from "@wordpress/interactivity";
async function testFunc() {
	console.log(test);
}

setTimeout(() => testFunc(), 10_000);

File 2:

import * as a11y from "@wordpress/a11y";

export async function testFunc() {
	const test = await import("@wordpress/interactivity");
	console.log(test, a11y);
}
setTimeout(() => testFunc(), 10_000);

File 3:

import * as t from "@wordpress/a11y";
import * as b from "@wordpress/blocks";

import * as test from "@wordpress/interactivity";

async function testFunc() {
	console.log(test);
}

setTimeout(() => testFunc(), 10_000);
console.log(b, t);

File 4:

import * as t from "@wordpress/a11y";
import * as b from "@wordpress/blocks";

async function testFunc() {
	const test = await import("@wordpress/interactivity");
	console.log(test);
}

setTimeout(() => testFunc(), 10_000);
console.log(b, t);

The files should have the following dependencies:

  • File 1: {"dependencies": ["@wordpress/interactivity"]}
  • File 2: {"dependencies": ["@wordpress/a11y", {"id": "@wordpress/interactivity", "import": "dynamic"}]}
  • File 3: {"dependencies": ["wp-blocks"], "module_dependencies": ["@wordpress/a11y", "@wordpress/interactivity"]}
  • File 4: {"dependencies": ["wp-blocks"], "module_dependencies": ["@wordpress/a11y", {"id": "@wordpress/interactivity", "import": "dynamic"}]}

Use of AI Tools

I don't use AI tools

jorgefilipecosta and others added 2 commits June 2, 2026 14:22
…mport() and emit module_dependencies

Packages like @wordpress/abilities and @wordpress/core-abilities are script modules
(registered via wpScriptModuleExports) that don't expose window globals. The plugin
currently treats all @wordpress/* packages as window-global scripts, which breaks
at runtime.

This adds these packages to the existing defaultRequestToExternalModule switch and
makes non-module (IIFE) builds also consult that function via a try-catch bridge.
Script module externals are detected in processModule via m.externalType === 'import'
and emitted as module_dependencies in the asset file, matching the format WordPress
PHP expects.
This adds handling of mixed usage of module imports + detection of dynamic imports in functions
@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: joshualip-plaudit <joshualipstone@git.wordpress.org>
Co-authored-by: simison <simison@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@simison

simison commented Jun 2, 2026

Copy link
Copy Markdown
Member

Can you add a changelog entry? Thanks!

@simison simison requested review from jsnajdr and youknowriad June 2, 2026 17:53
@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Required label: Any label starting with [Type].
  • Labels found: .

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

@joshualip-plaudit

Copy link
Copy Markdown
Contributor Author

@simison I added a changelog entry, but this is a PR on a PR, so it doesn't actually do much.

@jorgefilipecosta jorgefilipecosta force-pushed the fix/dep-extraction-script-module-externals branch 2 times, most recently from ca5c600 to 48af2e1 Compare June 5, 2026 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants