Skip to content

Commit cefbc02

Browse files
committed
javascript: Add esbuild section
1 parent ccb1f2b commit cefbc02

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

docs/javascript/index.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,80 @@ Run Biome with :ref:`pre-commit<linting-pre-commit>`:
236236
237237
:ref:`javascript-ci` runs Biome if you reuse the ``js`` workflow.
238238

239+
.. _esbuild:
240+
241+
build.js
242+
~~~~~~~~
243+
244+
Configure esbuild in a ``build.js`` file (or ``build.mjs``, if ``package.json`` doesn't set ``"type": "module"``).
245+
246+
.. code-block:: javascript
247+
:caption: build.js
248+
249+
import autoprefixer from "autoprefixer";
250+
import browserslist from "browserslist";
251+
import * as esbuild from "esbuild";
252+
import { esbuildPluginBrowserslist } from "esbuild-plugin-browserslist";
253+
import { sassPlugin } from "esbuild-sass-plugin";
254+
import postcss from "postcss";
255+
256+
const production = process.env.NODE_ENV === "production";
257+
258+
const options = {
259+
entryPoints: { // edit as needed
260+
main: "src/scss/main.scss",
261+
script: "src/js/main.js",
262+
},
263+
bundle: true,
264+
outdir: "core/static",
265+
minify: production,
266+
sourcemap: !production,
267+
legalComments: "linked",
268+
logLevel: "info",
269+
// any other options
270+
plugins: [
271+
esbuildPluginBrowserslist(browserslist(), { printUnknownTargets: false }),
272+
sassPlugin({
273+
async transform(source) {
274+
const { css } = await postcss([autoprefixer]).process(source, { from: undefined });
275+
return css;
276+
},
277+
}),
278+
],
279+
};
280+
281+
if (process.argv.includes("--watch")) {
282+
const context = await esbuild.context(options);
283+
await context.watch();
284+
console.log("Watching for changes …");
285+
} else {
286+
await esbuild.build(options);
287+
}
288+
289+
`esbuild-plugin-browserslist <https://www.npmjs.com/package/esbuild-plugin-browserslist>`__ sets esbuild's `target <https://esbuild.github.io/api/#target>`__ from `browserslist <https://github.com/browserslist/browserslist>`__'s `defaults <https://browsersl.ist/#q=defaults>`__ query, so that esbuild transpiles any JavaScript syntax that those browsers don't support.
290+
291+
Compile Sass with `esbuild-sass-plugin <https://github.com/glromeo/esbuild-sass-plugin>`__, adding vendor prefixes with `Autoprefixer <https://github.com/postcss/autoprefixer>`__ (via `PostCSS <https://postcss.org>`__).
292+
293+
Set ``NODE_ENV=production`` in Dockerfiles so that esbuild minifies bundles and omits sourcemaps.
294+
295+
With ``bundle: true``, esbuild resolves each ``url()`` in Sass, to copy and content-hash it. However:
296+
297+
- An asset from a dependency may not resolve: for example, if the dependency points ``url()`` to a path that isn't built yet. If so, configure your application to build and serve that file (for example, add it to Django's ``STATICFILES_DIRS``), and mark the path as ``external``:
298+
299+
.. code-block:: javascript
300+
301+
external: ["/static/*"],
302+
303+
- If a file is used outside Sass (like images in HTML templates), configure it as above.
304+
- If a file is exclusive to Sass, point ``url()`` to its source file with a relative path. If the file is a font or image, map its extension to the ``file`` loader, because esbuild assigns no loader to fonts or images by default:
305+
306+
.. code-block:: javascript
307+
308+
loader: {
309+
".woff2": "file",
310+
".otf": "file",
311+
},
312+
239313
Vue
240314
~~~
241315

0 commit comments

Comments
 (0)