Builder
Usage
# Build application
<pm> run build
# Build application for production
<pm> run build:prod
# Type-check only (TypeScript projects), without building
<pm> run typecheck
typecheck runs the steps tsc needs and nothing else: copy the files into dist-build, merge the HTML templates, then tsc --noEmit with the data-event handler references (see "Compile TypeScript" below). dist/ is left as it is, and dist-build is removed when the check passes (a failed check leaves it, like a failed build; the next run clears it). It exits with tsc's errors and a non-zero code otherwise. Use it in CI rather than a plain tsc --noEmit, which doesn't see the merged templates and reports every handler used only in a template as unused (noUnusedLocals).
Steps
Setup
- Copy files in temporary
dist-builddirectory: the sources, every file inpublic/(served from the site's root) and every file undersrc/assets/, whatever their type..DS_Store,Thumbs.dband.gitkeepfiles are left out. Don't useapp/,core/,config/or (in an extension)extension/as top-level folders inpublic/: those names hold the copied sources, which production builds and extension packages leave out. - with the markdown feature: compile the
markdownSourcesfolders into/generated/*.jsonmanifests
Base steps
- Add tags to
index.html(script, stylesheet) and validate it. It must contain anid="app"element when the app uses the router (the router mounts views into it) orgenerateSEOFiles(pages are prerendered into it). - Validate application routes - fail if duplicates are found. Skipped when no app code imports
routes.tsandgenerateSEOFilesis off: an app that mounts its components without the router needs no routes file. - Merge HTML templates: each
.htmltemplate goes into its component or view, in place of__TEMPLATE_PLACEHOLDER__ - Compile TypeScript (type-checking only in production). It runs on the merged files, so the expressions in
.htmltemplates are type-checked too; errors are reported at their.htmlfile and line.tscruns ondist-buildthrough a generateddist-build/tsconfig.jsonthat extends yours (removed at the end of the build) - Minify HTML templates
- Compile styles (including Tailwind CSS with
tailwindoption enabled) - with i18n option enabled: Merge locales into a single
.jsonfile - Run esbuild: bundles the app into
bundle.js(minified in production). Each dynamicimport()becomes a separate chunk inchunks/, only downloaded the first time it runs; that's how lazy routes work. Code shared by several chunks goes into its own chunk too.
Production-only steps
- Hash
bundle.jsandmain.css, the two filesindex.htmlreferences (chunks/names are already hashed by esbuild; files copied frompublic/andsrc/assets/keep their names) - with generateSEOFiles option enabled: Generate static
.htmlfiles for routes configured inconfig/seo.json - Compress files in Gzip (
.gz) and Brotli (.br) formats, last, so prerendered pages,index.html,sitemap.xmlandrobots.txtare compressed too (the copied sources aren't) - Remove unused folders and
nutin.config.js.
Browser extension projects
- with
--extension: after esbuild (and hashing, in production), write one unpacked extension per browser inextension.browsersintodist/<browser>: the built page and assets (without the copied sources),background.js(bundled fromsrc/extension/background.tswithbuilder.esbuild's settings) andmanifest.json(version frompackage.json, Firefox's derived from Chrome's). Nothing is compressed. See How do I create a browser extension?
Final step
- Rename
dist-buildtodist
Notes
- You can configure SASS paths to be compiled in
nutin.config.js. - You can configure ESBuild options in
nutin.config.js. - The browser bundle only gets the runtime part of
nutin.config.js: the tooling-only keys (builder,testinNutin,dockerPorts,markdownSources) are left out. - If you need new asset formats to be included in the built output, add them in
tools/builder/app/binary-extensions.js.