Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions docs/adr/0007-restrict-dynamic-import-contexts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 0007 Restrict dynamic import contexts with webpackInclude

Status: Accepted (2026-11-28)

## Context

Dynamic imports with a template literal, like
``import(`moment/locale/${lang}.js`)``, make Webpack create a context module.
Every file which could match becomes its own chunk, and:

- A lookup map of all matching files is put into the chunk of the importing
module. For Patterns this is the eagerly loaded patterns chunk.
- Every chunk gets an entry in the chunk filename map of the Webpack runtime,
which is part of the entry points `bundle.min.js` and `remote.min.js`.
Webpack lists all async chunks reachable from the entry there, no matter how
deeply nested the dynamic import is.

Moving such an import into a lazily loaded `--implementation` module only
moves the lookup map out of the patterns chunk. The runtime chunk filename map
in the entry points stays the same. This was verified with a build.

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

- pat-syntax-highlight: highlight.js languages (193) and themes (258,
including the `base16/` subdirectory).
- pat-calendar: fullcalendar locales (156, including the `*.global.js`
duplicates).
- pat-display-time: moment locales (139).

pat-calendar and pat-display-time cut the language to 2 letters, so locales
like `zh-tw` or `ar-dz` can never be requested.

pat-syntax-highlight restricts the `theme` option to `dark` and `light`, which
map to the `stackoverflow-*` themes. Other themes can only be passed as
options from JavaScript.

## Decision

Restrict the matching files with `webpackInclude`:

- pat-calendar and pat-display-time: Only 2-letter locales.
- pat-syntax-highlight themes: Only top-level themes, no `base16/` themes.
- pat-syntax-highlight languages: Only the languages of the highlight.js
"common" build (`highlight.js/lib/common.js`).

`webpackInclude` is matched against the absolute file path, so the regular
expressions are anchored to the directory name and not to `./`.

## Consequences

Production build:

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

- pat-calendar and pat-display-time behave the same as before.
- pat-syntax-highlight: Languages outside the common set are no longer
available. They fall back to `highlightAuto`, which only knows already
registered languages, so they are mostly not highlighted. Languages can be
added to the `webpackInclude` list when needed.
- pat-syntax-highlight: `base16/` themes can no longer be passed from
JavaScript.
- New template literal dynamic imports should get a `webpackInclude` filter
too.
16 changes: 13 additions & 3 deletions src/pat/calendar/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,19 @@ export default Base.extend({
// we don't support any country-specific language variants, always use first 2 letters
lang = lang.substr(0, 2).toLowerCase();
if (lang !== "en") {
const locale = await import(`@fullcalendar/core/locales/${lang}.js`);
config.locale = locale.default;
log.debug(`Loaded locale for ${lang}`);
try {
// Only 2-letter locales can be requested. Every file which
// could match becomes a chunk listed in the entry point's
// runtime.
const locale = await import(
/* webpackInclude: /locales\/[a-z]{2}\.js$/ */
`@fullcalendar/core/locales/${lang}.js`
);
config.locale = locale.default;
log.debug(`Loaded locale for ${lang}`);
} catch {
log.warn(`No locale for ${lang}, falling back to English.`);
}
}
if (this.options.first.day !== null) {
config.firstDay = this.options.first.day;
Expand Down
11 changes: 11 additions & 0 deletions src/pat/calendar/calendar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,17 @@ describe("1 - Calendar tests", () => {
const title_el = el.querySelector(".cal-title");
expect(title_el.innerHTML).toEqual("März 2020");
});

it("Falls back to English if no locale exists for the language", async () => {
const el = document.querySelector(".pat-calendar");
// There is only zh-cn and zh-tw, but the language is cut to 2 letters.
el.setAttribute("data-pat-calendar", "initial-date: 2020-03-01; lang: zh");
pattern.init(el);
await utils.timeout(1); // wait a tick for async to settle.

const title_el = el.querySelector(".cal-title");
expect(title_el.innerHTML).toEqual("March 2020");
});
});

describe("2 - Calendar tests with calendar controls outside pat-calendar", () => {
Expand Down
7 changes: 6 additions & 1 deletion src/pat/display-time/display-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ export default Base.extend({
// we don't support any country-specific language variants, always use first 2 letters
lang = lang.substr(0, 2).toLowerCase();
try {
await import(`moment/locale/${lang}.js`);
// Only 2-letter locales can be requested. Every file which could
// match becomes a chunk listed in the entry point's runtime.
await import(
/* webpackInclude: /locale\/[a-z]{2}\.js$/ */
`moment/locale/${lang}.js`
);
Moment.locale(lang);
this.lang = lang;
} catch {
Expand Down
9 changes: 8 additions & 1 deletion src/pat/syntax-highlight/syntax-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class Pattern extends BasePattern {

// Map both spellings to one stylesheet so Webpack emits it once.
const theme_name = String(theme).replace(/\.min$/, "");
// Only top-level themes, no `base16/` subdirectory. Every file which
// could match becomes a chunk listed in the entry point's runtime.
import(
/* webpackInclude: /styles\/[^/]+\.css$/ */
/* webpackExclude: /\.min\.css$/ */
`highlight.js/styles/${theme_name}.css`
);
Expand Down Expand Up @@ -75,7 +78,11 @@ class Pattern extends BasePattern {
const lang_file = import_path_mapping[language] || language;
const hljs_language = (
await import(
/* webpackExclude: /\.js\.js$/ */
// Only the languages of highlight.js' "common" build.
// Every file which could match becomes a chunk listed
// in the entry point's runtime. Other languages fall
// back to `highlightAuto`.
/* webpackInclude: /languages\/(xml|bash|c|cpp|csharp|css|markdown|diff|ruby|go|graphql|ini|java|javascript|json|kotlin|less|lua|makefile|perl|objectivec|php|php-template|plaintext|python|python-repl|r|rust|scss|shell|sql|swift|yaml|typescript|vbnet|wasm)\.js$/ */
`highlight.js/lib/languages/${lang_file}`
)
).default;
Expand Down