Utilities for building Next.js applications with Bazel and rules_js.
All invocations of Next.js are done through a next_js_binary
target passed into the macros.
This is normally generated once alongside the package.json
containing the next
dependency:
load("@npm//:next/package_json.bzl", next_bin = "bin")
next_bin.next_binary(
name = "next_js_binary",
visibility = ["//visibility:public"],
)
The next binary is then passed into the macros, for example:
nextjs_build(
name = "next",
config = "next.config.mjs",
srcs = glob(["src/**"]),
next_js_binary = "//:next_js_binary",
)
There are two sets of macros for building Next.js applications: standard and standalone.
nextjs()
: wrap the build+dev+start targetsnextjs_build()
: the Next.js build commandnextjs_dev()
: the Next.js dev commandnextjs_start()
: the Next.js start command, accepting a Next.js build artifact to start
nextjs_standalone_build()
: the Next.js build command, configured for a standalone application within bazelnextjs_standalone_server()
: constructs a standalone Next.js serverjs_binary
following the standalone directory structure guidelines
nextjs(name, srcs, next_js_binary, config, data, serve_data, kwargs)
Generates Next.js build, dev & start targets.
{name}
- a Next.js production bundle
{name}.dev
- a Next.js devserver
{name}.start
- a Next.js prodserver
Use this macro in the BUILD file at the root of a next app where the next.config.mjs
file is located.
For example, a target such as //app:next
in app/BUILD.bazel
next(
name = "next",
config = "next.config.mjs",
srcs = glob(["src/**"]),
data = [
"//:node_modules/next",
"//:node_modules/react-dom",
"//:node_modules/react",
"package.json",
],
next_js_binary = "//:next_js_binary",
)
will create the targets:
//app:next
//app:next.dev
//app:next.start
To build the above next app, equivalent to running next build
outside Bazel:
bazel build //app:next
To run the development server in watch mode with
ibazel, equivalent to running
next dev
outside Bazel:
ibazel run //app:next.dev
To run the production server in watch mode with
ibazel, equivalent to running
next start
outside Bazel:
ibazel run //app:next.start
PARAMETERS
nextjs_build(name, config, srcs, next_js_binary, data, kwargs)
Build the Next.js production artifact.
See https://nextjs.org/docs/pages/api-reference/cli/next#build
PARAMETERS
nextjs_dev(name, config, srcs, data, next_js_binary, kwargs)
Run the Next.js development server.
See https://nextjs.org/docs/pages/api-reference/cli/next#next-dev-options
PARAMETERS
nextjs_standalone_build(name, config, srcs, next_js_binary, data, kwargs)
Compile a standalone Next.js application.
NOTE: a next.config.mjs
is generated, wrapping the passed config
, to overcome Next.js limitation with bazel,
rules_js and pnpm (with hoist=false, as required by rules_js).
Due to the generated next.config.mjs
file the nextjs_standalone_build(config)
must have a unique name
or file path that does not conflict with standard Next.js config files.
Issues worked around by the generated config include:
PARAMETERS
nextjs_standalone_server(name, app, pkg, data, kwargs)
Configures the output of a standalone Next.js application to be a standalone server binary.
See the Next.js standalone server documentation for details on the standalone server directory structure.
This function is normally used in conjunction with nextjs_standalone_build
to create a standalone
Next.js application. The standalone server is a js_binary
target that can be run with bazel run
or deployed in a container image etc.
PARAMETERS
nextjs_start(name, config, app, next_js_binary, data, kwargs)
Run the Next.js production server for an app.
See https://nextjs.org/docs/pages/api-reference/cli/next#next-start-options
PARAMETERS