Skip to main content

PEP 517 build backend + Sphinx extension that orchestrates Vite via pnpm

Project description

sphinx-vite-builder

PEP 517 build backend and Sphinx extension that transparently orchestrates Vite builds via pnpm for Sphinx-theme packages whose static assets (CSS / JS) are produced by a JavaScript toolchain.

What it solves

A common pattern for modern Sphinx themes is a Python package whose theme/<name>/static/ directory ships built CSS and JS that were produced by a JS build tool (Vite, webpack, …). The build artefacts are gitignored — they're reproducibly built, not source code. But that creates two friction points:

  1. Editable installs and source-tree builds crash with confusing errors when the static dir is empty (e.g. hatchling's Forced include not found).
  2. CI workflows must duplicate pnpm install + vite build setup steps in every job that touches the package.

sphinx-vite-builder owns the Vite invocation end-to-end — exactly the way maturin owns Cargo for Rust+Python packages, or sphinx-theme-builder owns webpack for older Sphinx themes.

The contract

Sources should check for node, pnpm, etc and error if it's not good, then build. Wheels should have the build files baked in and not need node and pnpm at all.

This is the central invariant of the package. The two install paths behave asymmetrically by design:

Wheel installs — zero toolchain required

A user running pip install <package> from PyPI gets a wheel that already contains the vite-built static/ tree, populated by this backend at release time. The PEP 517 chain doesn't run on the consumer side. No backend invocation. No pnpm. No Node. The end user sees Python and only Python.

$ # No pnpm, no Node, no problem
$ pip install gp-furo-theme

Source builds — fail loud, fail informatively

A contributor (or downstream packager building from source) goes through the PEP 517 chain. The backend runs pnpm exec vite build to produce static/, and that requires pnpm + Node on PATH. If the toolchain is missing, the backend raises a typed exception with a multi-line, copy-pasteable hint:

sphinx-vite-builder: cannot bootstrap the vite toolchain.
`pnpm` is not on PATH. Install it via one of:

  corepack enable        # Node 16.10+ ships corepack
  curl -fsSL https://get.pnpm.io/install.sh | sh -

See https://pnpm.io/installation

…

Detected CI provider: GitHub Actions. Add the following to your pipeline
config (before the Python build step that triggers this backend):

          - uses: pnpm/action-setup@v6
            with:
              version: 10
          - uses: actions/setup-node@v6
            with:
              node-version: 22
              cache: pnpm

The error includes the resolved vite-root path, the platform-specific CI setup recipe (GitHub Actions, CircleCI, Azure Pipelines, GitLab CI, or generic), and the SPHINX_VITE_BUILDER_SKIP=1 escape hatch for environments that genuinely don't need vite to run.

The web/-absent short-circuit (sdist install bridge)

A user running pip install <pkg>.tar.gz from an sdist runs the PEP 517 chain too — but the sdist excludes web/ (the Vite source tree). The backend detects the absence, short-circuits cleanly, and hatchling packs the pre-baked static/ (carried in the sdist via [tool.hatch.build] artifacts) into the wheel. Sdist installs need no toolchain either.

The asymmetry is the whole product: the same backend is strict (running and failing loudly) when there's a web/ to act on, and silent (skipping cleanly) when there's no web/ to begin with. The two shapes match the two consumer worlds.

Two heads, one subprocess core

PEP 517 build backend

Drop-in replacement for hatchling.build. Runs pnpm exec vite build before delegating wheel/sdist construction to hatchling.

# packages/your-theme/pyproject.toml
[build-system]
requires = ["hatchling>=1.0", "sphinx-vite-builder"]
build-backend = "sphinx_vite_builder.build"

[tool.hatch.build.targets.sdist]
exclude = ["web/"]    # so the sdist→wheel chain hits the short-circuit

[tool.hatch.build]
artifacts = ["src/<your-theme>/theme/<theme-name>/static/"]

Sphinx extension

Loaded from conf.py. Runs Vite as part of the docs lifecycle:

  • sphinx-buildpnpm exec vite build once before the docs build
  • sphinx-autobuildpnpm exec vite build --watch as a child process for the lifetime of the autobuild server, with idempotent re-fire on rebuilds and graceful teardown on signal / atexit
# docs/conf.py
extensions = ["sphinx_vite_builder"]

Fast-fail diagnostics — error type reference

Error When Hint surface
PnpmMissingError pnpm not on PATH during a source build corepack enable, pnpm.io/installation, per-CI YAML recipe, SPHINX_VITE_BUILDER_SKIP=1
NodeModulesInstallError pnpm install exited non-zero cd <vite-root> && pnpm install --frozen-lockfile rerun command, captured stderr
ViteFailedError pnpm exec vite build exited non-zero invocation context (cwd, exit code), captured stderr

All three inherit from SphinxViteBuilderError, so consumers can except SphinxViteBuilderError for a single catch surface.

CI detection

The PnpmMissingError hint is self-healing when the backend detects a CI environment. Detection precedence (most-specific wins):

CI provider Env var Recipe shape
GitHub Actions GITHUB_ACTIONS=true pnpm/action-setup@v6 + actions/setup-node@v6
CircleCI CIRCLECI=true corepack enable && corepack prepare pnpm@latest-10 --activate step
Azure Pipelines TF_BUILD=True NodeTool@0 + corepack script
GitLab CI GITLAB_CI=true before_script corepack invocations
Generic CI=true "Use your CI's package-manager setup mechanism"

Source: each provider's own canonical detection variable per the pnpm Continuous Integration docs.

License

MIT — see LICENSE.

Agent / contributor guidance

See AGENTS.md for the design contract, architecture map, and conventions agents and contributors should follow when making changes. (CLAUDE.md is a passthrough to AGENTS.md for Claude Code.)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinx_vite_builder-0.0.1a16.dev1.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sphinx_vite_builder-0.0.1a16.dev1-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file sphinx_vite_builder-0.0.1a16.dev1.tar.gz.

File metadata

File hashes

Hashes for sphinx_vite_builder-0.0.1a16.dev1.tar.gz
Algorithm Hash digest
SHA256 7e7196a1dc4462e7c6223d3eea81038d69d312e19ef76d55f382b18f000f4dae
MD5 8926af91bf14c2fa703aa1e1b7e99cbb
BLAKE2b-256 e482150912bd8cb45b36fbc49161c9ff198147ecea7bbf2bc589c011c5b87d40

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinx_vite_builder-0.0.1a16.dev1.tar.gz:

Publisher: release.yml on git-pull/gp-sphinx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sphinx_vite_builder-0.0.1a16.dev1-py3-none-any.whl.

File metadata

File hashes

Hashes for sphinx_vite_builder-0.0.1a16.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 e099e9d213d9c2735dee016570aca056ec7e5e133d0caa349a473d7da9e666dd
MD5 014b37bf2bde5c8be4aaa19516628935
BLAKE2b-256 f16e76c98d3c0e95a29bfa4dbb9a947103191690530e71ea085d04db5b1acbdc

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinx_vite_builder-0.0.1a16.dev1-py3-none-any.whl:

Publisher: release.yml on git-pull/gp-sphinx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page