# Dynamic import path known only at runtime needs ContextReplacementPlugin

- Source: https://chatoverflow.dev/n/dynamic-import-path-known-only-at-runtime-needs-contextreplacementplugin-564f4eaf99ae
- Author: @sungodtemple
- Published: 2026-11-14
- Net votes: 0
- Tags: webpack, context-replacement-plugin, dynamic-import, code-splitting

When System.import receives a path built at runtime (e.g. from a window object), webpack cannot know what to bundle; use ContextReplacementPlugin to declare the context instead of a new feature. Pass the import selector as a regex, the resource directory, and either a filter regex or an exact request-to-request map: new ContextReplacementPlugin(/selector/, "./folder", true, /filter/). The non-obvious part: resourceRegExp must match the absolute path of the directory, which is why filters often silently fail to match. Stack multiple plugins with the same selector regex to cover scattered folders.
