Skip to content

Restrict dynamic import contexts with webpackInclude - #1310

Merged
thet merged 5 commits into
masterfrom
thet/webpack-include-filter
Nov 28, 2026
Merged

thet merged 5 commits into
masterfrom
thet/webpack-include-filter

Conversation

@thet

@thet thet commented Nov 28, 2026

Copy link
Copy Markdown
Member

Problem

Dynamic imports with a template literal, such as
import(`moment/locale/${lang}.js`), make Webpack create a separate chunk
for every file that could match. Every one of those chunks gets an entry in
the chunk filename map of the Webpack runtime. That runtime is part of the
entry points bundle.min.js and remote.min.js. The lookup map of all
matching files also ends up in the eagerly loaded patterns chunk.

Four imports caused 766 of the 832 entries in the runtime chunk map:

Pattern Import Chunks
pat-syntax-highlight highlight.js/lib/languages/${lang_file} 193
pat-syntax-highlight highlight.js/styles/${theme_name}.css 258
pat-calendar @fullcalendar/core/locales/${lang}.js 156
pat-display-time moment/locale/${lang}.js 139

Moving such an import into a lazily loaded --implementation module does
not help. Webpack lists every async chunk reachable from the entry in the
runtime, however deeply the import is nested. A test build confirmed that
the entry points stayed the same size.

Changes

  • pat-calendar, pat-display-time: Only bundle 2-letter locales. Both
    patterns cut the language to 2 letters, so locales like zh-tw or the
    fullcalendar *.global.js duplicates could never be requested. No
    behaviour change.
  • pat-syntax-highlight: Only bundle the languages of the highlight.js
    "common" build and the top-level themes, without base16/.
  • pat-calendar (fix): Fall back to English if no locale exists for the
    language. Before, e.g. lang: zh made the locale import fail and the
    calendar never rendered. A test was added.
  • ADR 0007 documents the decision and the measurements.

webpackInclude is matched against the absolute file path, so the regular
expressions are anchored to the directory name (e.g.
/locales\/[a-z]{2}\.js$/), not to ./.

Results (production build)

before after
bundle.min.js 126,967 B 112,662 B
remote.min.js 126,087 B 111,784 B
entries in the runtime chunk map 832 352
eager patterns chunk 223,222 B 201,108 B
chunk files 1660 700

Behaviour changes in pat-syntax-highlight

  • Languages outside the common set (e.g. dockerfile, nginx, django)
    fall back to highlightAuto. That only knows the languages already loaded
    on the page, so these blocks are mostly not highlighted. Languages can be
    added to the webpackInclude list in syntax-highlight.js when needed.
  • base16/ themes can no longer be passed as options from JavaScript. The
    documented theme option only accepts dark and light, so this has no
    effect for markup-based usage.

Testing

  • npx jest src/pat/calendar src/pat/display-time src/pat/syntax-highlight:
    43 tests pass.
  • Production build checked: the lookup maps contain only the intended files,
    including xml, python, javascript and the stackoverflow-dark and
    stackoverflow-light themes.

🤖 Generated with Claude Code

thet added 5 commits November 28, 2026 06:01
… webpackInclude.

Assisted-by: Claude Opus 5.5
…anguage.

The language is cut to 2 letters, so e.g. "zh" was requested, for which
fullcalendar has no locale. The failing import prevented the calendar from
rendering.

Assisted-by: Claude Opus 5.5
Only 2-letter locales can be requested. Every other file in the dynamic
import context, including the *.global.js duplicates, was a chunk listed in
the runtime of the entry points.

Ref: ADR 0007

Assisted-by: Claude Opus 5.5
Only 2-letter locales can be requested. Every other file in the dynamic
import context was a chunk listed in the runtime of the entry points.

Ref: ADR 0007

Assisted-by: Claude Opus 5.5
…el themes.

Restrict the languages to those of the highlight.js "common" build and the
themes to the top-level ones, without the base16 themes. Every file in the
dynamic import contexts was a chunk listed in the runtime of the entry
points.

Languages outside the common set fall back to highlightAuto.

Ref: ADR 0007

Assisted-by: Claude Opus 5.5
@thet
thet merged commit 94bc664 into master Nov 28, 2026
1 check passed
@thet
thet deleted the thet/webpack-include-filter branch November 28, 2026 10:35
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.

1 participant