Repository navigation
Description
Spun out of the asset compiler design work
(#10,
docs/design/asset_compiler.md, landing with the asset-compiler design
PR). This issue records the exploration, recommendations, and reasoning
for the asset-pipeline adjacents that were deliberately kept out of #10's
v1 scope: ESM output, minification and the postprocess hooks,
sourcemaps, and a dev/prod mode setting. Static export / SSG is
tracked in #34.
1. ESM (type="module") output
Stance in #10 v1: using import in component JS requires a compiled
language (js_lang = "ts"; esbuild compiles plain JS too). The import is
resolved server-side and bundled into the component's IIFE script; nothing
is ever left for the browser to resolve. ./util works, "pkg" works when
a node_modules is resolvable (inlined), and URL imports error, because an
IIFE bundle has no mechanism to defer an import to the browser.
The contract adopted now (it is what makes the v1 stance coherent, and
it pre-decides the ESM design):
Relative specifiers are always a server-side concern: they name files
next to the component's source, a location only the server knows. Bare
specifiers and full URLs can be a browser concern (import maps, CDN
URLs), but only in module output.
The spec forces this split: an inline <script type="module"> resolves
relative imports against the document base URL (the page URL), not
against anything related to the component's source file
(https://html.spec.whatwg.org/multipage/scripting.html), so passthrough
inline ESM with relative imports is broken by construction, not merely
inconsistent.
Why users will ask for ESM (the demand vectors, roughly in order of
likelihood for a Python-first audience):
- CDN imports without npm:
import x from "https://esm.sh/pkg"is
the zero-toolchain way to use the npm ecosystem, and it is exactly the
case IIFE bundling cannot serve. - ESM-only libraries: a growing share of packages ship ESM-only
builds; loading one via a script tag requirestype="module". - Runtime dedup of shared code: the asset compiler deliberately does
not bundle across components, so a util imported by five components is
inlined five times. With ESM, the browser's module cache evaluates a
shared URL once. ESM is the runtime answer to that accepted
duplication, without reintroducing a whole-graph build. - Top-level
awaitandimport.meta(module-only features; dynamic
import()already works in classic scripts and is not a driver).
Design when a trigger lands, two tiers:
- Tier 1, compiled ESM: esbuild
format=esm; relative imports bundled
server-side as today; bare/URL specifiers passed through as externals;
emitted asScript(attrs={"type": "module"}, wrap=False). The emission
layer supports this today:_script_type_should_wrapnever IIFE-wraps
modules (modules have their own scope, so the wrapper's purpose comes
free), andScriptattrs carry the type. Needs: a way to declare module
output per component (an axis orthogonal to*_lang), and attrs plumbing
incache_component_js. - Tier 2, passthrough ESM: author writes a module using only bare/URL
imports; citry serves it untouched astype="module". Import maps are
the companion story.
Known work items and constraints (verified against WHATWG/MDN):
- Module scripts are defer-by-default: they execute after parsing, in
document order, after every inline classic script. Citry's client
runtime is a classic script, so "runtime before all modules" is
guaranteed by spec as long as it stays classic. - The fragment runtime's
loadJsassumes inline scripts execute
synchronously on append; that is true for classic, false for modules
(dynamically inserted inline modules are queued and fire no load event;
srcmodules are force-async unlessasync = false). The fixes are
contained inloadJs:async = falsefor ordering on URL modules, and
blob-URL +src+onloadfor inline modules. Blob URLs cannot resolve
relative imports, which is fine: under the contract, module output
contains none. ESM can never be executed via eval (spec), so the
create-element approach the runtime already uses is the only viable one. - Import maps are baseline since 2023, but late/multiple import maps are
Chromium-only (Chrome 133+; Firefox behind a pref; Safari unshipped as
of mid-2026), so import maps belong in the initial document, not in
fragments. - Revisit when building:
.mjscurrently sits in the base suffix set
(loads as plain JS, no compile); an ESM feature probably wants it routed
through the compiler instead.
2. Minification and the postprocess hooks
Facts (measured/verified): JS minification saves roughly 60% on top
of gzip (jQuery: 73 KB gzipped vs 30 KB minified+gzipped,
https://mathiasbynens.be/demo/jquery-size); CSS and HTML minification add
roughly one percentage point after gzip. No mainstream framework
minifies SSR HTML at request time: Next.js HTML is compact by construction
(JSX emits no whitespace), Rails and Nuxt point at gzip/brotli, and
django-htmlmin plus its underlying htmlmin are dead (last releases
2017-2019). The one maintained whole-HTML minifier reachable from Python is
minify-html (Rust core, official PyPI wheels; bundles oxc for JS and
lightningcss for CSS).
Recommendations:
- Class-level component JS/CSS: minify in a minifier extension, riding
the not-yet-builton_js_postprocess/on_css_postprocesshooks. It
runs once per class pre-cache, so it has build-time economics despite the
lazy lifecycle (the same amortization as compilation). JS is the prize;
CSS comes along for free. Engine: esbuild--minifyvia the tool ladder
the asset compiler already provides, with rjsmin/rcssmin (the still
maintained django-compressor defaults) as a pure-Python fallback. Citry
owns only the hook seam, never a minification flow. - Variables scripts and the fragment manifest: never minified
(generated, tiny, already compact). - Whole rendered HTML: no new hook is needed and no support is
recommended. The existingon_serializemap hook is exactly "final
HTML in, final HTML out", so a whole-HTML minifier extension is already
possible today for whoever insists; the documentation stance should be
that brotli/gzip on the wire is the answer, matching the whole
ecosystem. on_template_postprocessstays unbuilt: its remaining candidate
consumers are the inline-CSS extension and the reactive extension's
whole-HTML wrap (per-component post-render, whichon_serializecannot
cover); no minification use case needs it.
Postprocess hook API, scoped (for when the minifier extension lands):
on_js_postprocess(ctx) -> str | None/on_css_postprocess(ctx) -> str | None; frozen contexts carryingcitry,component_class,
content; dispatchedresult="map"overcontentlikeon_js_loaded.- Fire site: inside
cache_component_js/cache_component_css, after
the$onComponentexpansion (so a minifier sees the expanded plain-JS
call, never the sugar), before theScript/Styleis built and
cached. - Scope v1: class-level component scripts only; a
kindfield on the
context leaves room for servedDependenciesfiles later. - Contract notes: hook output is cache-keyed and hash-fingerprinted, so
postprocess hooks inherit the determinism rule; enabling/disabling a
postprocess extension changes cached content, so deployments with a
persistent main cache re-warm withcitry compile(which writes with
force=True).
3. Sourcemaps
What they buy, ranked: dev-time browser debugging of TS/JSX at the
original source is the real benefit. They do nothing for server-side
errors (compilers already report against the input source), and mapping
template-dialect parse errors back to markdown/pug source is the language
server track (#23), not a
compiler artifact.
Mechanics and costs: CompileResult deliberately leaves room for a
sourcemap field. When built: inline data-URI maps, dev only (the
Phoenix and Vite practice; maps with embedded sources are roughly the size
of the code or larger). Mapping into inline-in-Python sources is plausible
via virtual sources plus sourcesContent (the mechanism behind webpack's
webpack:// scheme; Vue proves devtools handle maps into mixed-content
host files at correct offsets) but unprecedented for .py hosts; a short
empirical test settles it before designing around it. Devtools support for
sourcemaps on inline <style> blocks is unproven, so CSS maps are likely
never worth building.
Recommendation: stay deferred. Build JS maps first when dev-experience
demand appears, behind a dev-only default (see mode, below).
4. A dev/prod mode setting
The asset compiler resolved its immediate dev need (persistent compile
cache by default) without a mode concept, but the consumer list keeps
growing: minification defaults, sourcemap defaults, watcher auto-start,
debug output verbosity. Recommendation unchanged from the design doc:
introducing a mode is a cross-cutting decision that deserves its own
design pass when the next concrete consumer lands; it should not be backed
into via compiler options. This issue adds two more consumers (minify and
sourcemap defaults) to that list.
Pointers
docs/design/asset_compiler.md(the v1 design these adjacents defer
from; sections 3.4, 6, 7.5, 14).docs/design/extensions_roadmap.mdsection 4 (the postprocess hook
family and its build-on-first-consumer rule).- #10 (asset compiler),
#23 (language server;
owns dialect position mapping). - SSG mode / static export: #34.
Activity