Skip to content

Correctly resolve dynamic chunk paths relative to the current file at run time #11127

@gkjohnson

Description

@gkjohnson

Feature request

Correctly load a bundled dynamic javascript import file relative to the current script file.

What is the expected behavior?

When dynamic imports (import()) the bundle should correctly load bundle chunks relative to the loading javascript file rather than the loaded root HTML file as the import() natively behaves.

Without this behavior package dependencies aren't able to rely on dynamic import behavior.

If this isn't currently clear I can provide a minimal build of the problematic behavior.

What is motivation or use case for adding/changing the behavior?

Dynamic imports are valuable part of modern javascript for enabling more performant initial download times and support from bundlers is important for enabling their adoption. Right now publicPath must be set with the root html file that will load it in mind which is confusing and not necessarily clear without digging deep into webpacks behavior. It also prevents the same package from being loaded by html files located in different directory tiers relative to the bundled javascript. However it should be possible for this functionality to "just work".

How should this be implemented in your opinion?

document.currentScript could be used to derive the current script path in supported browsers. Some polyfills claim that document._currentScript() is available in older versions of IE. Other polyfills and Parcel.js (which supports this behavior correctly out of the box) rely on looking at the latest script available in an Errors stacktrace to get the current script path and load a chunk relative to the current script.

Are you willing to work on this yourself?

This is not typically my expertise but if no one else is available to address this and someone can point me in the right direction for where to make the change I can try to contribute this feature.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions