37
loading...
This website collects cookies to deliver better user experience
a.html
:<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>A part of our website</title>
<link rel="shortcut icon" href="#" />
<div id="view"></div>
<script type="module" src="./dist/a.js"></script>
</head>
<body>
<h1>Page A</h1>
<p id="core"></p>
<p id="a">(a placeholder)</p>
<p id="b">(b placeholder)</p>
<p><a href="b.html">Go elsewhere</a></p>
</body>
</html>
b.html
:<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>B part of our website</title>
<link rel="shortcut icon" href="#" />
<div id="view"></div>
<script type="module" src="./dist/b.js"></script>
</head>
<body>
<h1>Page B</h1>
<p id="core"></p>
<p id="a">(a placeholder)</p>
<p id="b">(b placeholder)</p>
<p><a href="a.html">Go elsewhere</a></p>
</body>
</html>
<script type="module" ...
. All paragraphs <p id="...
are meant to be updated by our JS & to let us see if all works as expected../src/core.js
:import $ from "jquery";
$("#core").html("Core module");
./src/a.js
:import $ from "jquery";
import "./core";
$("#a").html("A file, file A");
./src/b.js
:import $ from "jquery";
import "./core";
$("#b").html("B yourself");
$ npm init -y
Wrote to /home/marcin/workspace/github/esbuild-multipage/package.json:
{
"name": "esbuild-multipage",
"version": "1.0.0",
"description": "Example repo for an article about multipage with esbuild",
"main": "index.js",
...
$ npm install --save jquery esbuild
added 2 packages, and audited 3 packages in 826ms
found 0 vulnerabilities
npm run build
, add to package.json
:{
...
"scripts": {
...
"build": "esbuild src/a.js src/b.js --bundle --outdir=dist --splitting --format=esm"
}
...
src/a.js
& src/b.s
- the entry points of the application--bundle
- bundle mode of esbuild
--outdir=dist
- the folder where all the built code goes--splitting
- we turn on the experimental splitting behavior--format=esm
- another requirement of splitting to work - as of now, it's only working with es-modules output--splitting
from the build command.$ npm run build
> [email protected] build
> esbuild src/a.js src/b.js --bundle --outdir=dist --splitting --format=esm
dist/chunk-TTCANJWN.js 226.2kb
dist/a.js 190b
dist/b.js 186b
⚡ Done in 23ms