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

[bug] dynamic imports doesn't work with extracted WordPress dependencies #2545

Closed
3 tasks done
cawa-93 opened this issue Jan 12, 2024 · 4 comments
Closed
3 tasks done
Labels
bug Something isn't working stale

Comments

@cawa-93
Copy link
Contributor

cawa-93 commented Jan 12, 2024

Agreement

Describe the issue

If I dynamically import a module that includes import of WP dependencies, this module is loaded without WP dependencies.

Expected Behavior

The dependencies of the WP are dynamically loaded and initialize global variables before the main module is executed.

Actual Behavior

Wordpress dependencies are extracted but not dynamically loaded when the module is loaded.

Steps To Reproduce

// index.js
const {getText} = await import('./translator.js')
console.log( getText )
// translator.js
import { __ } from '@wordpress/i18n';
export const getText = __
  1. Create there two files.
  2. Enqueue index.js into page.
  3. Check console logs.

version

6.20.0

Logs

No response

Configuration

No response

Relevant .budfiles

No response

@cawa-93 cawa-93 added the bug Something isn't working label Jan 12, 2024
@retlehs
Copy link
Member

retlehs commented Jan 12, 2024

Enqueue index.js into page.

How exactly are you doing this? Are you using the bundle function from Acorn?

@cawa-93
Copy link
Contributor Author

cawa-93 commented Jan 12, 2024

We use the bedrock and sage theme.
In theme's setup.php

add_action('wp_enqueue_scripts', function () {
    bundle('category')->enqueue();
}, 100);

The problem occurs in development mode. The development mode is launched with a bud dev. On page I got error Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'i18n')

зображення

@kellymears
Copy link
Contributor

kellymears commented Jan 18, 2024

I haven't looked at this super closely yet but at first glance:

you can't dynamically import WordPress dependencies as they are included via window variables.

As in, your source:

import { __ } from '@wordpress/i18n'

compiles to something like:

const __ = wp.i18n.__

window.wp is set by WordPress core via acorn.

I've never tried to dynamically import WP dependencies because of this reason and while it would be nice if it worked, it seems that it doesn't. This might be an improvement that we can make to @roots/wordpress-dependencies-webpack-plugin and/or @roots/wordpress-externals-webpack-plugin but it might also be an upstream limitation in webpack itself.

As a workaround I would add any import statements to the entrypoint so that they are properly picked up on by the plugins.

Example for an entrypoint called app.js which uses @wordpress/i18n somewhere via a dynamic import:

import '@wordpress/i18n' // flag external pkg
// ... your source

Copy link

Message to comment on stale issues. If none provided, will not mark issues stale

@github-actions github-actions bot added the stale label Mar 19, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale
Projects
None yet
Development

No branches or pull requests

3 participants