Setuptools Node.js extension plugin
Project description
setuptools-nodejs
A setuptools extension for building Node.js frontend projects and packaging them with Python code, specifically designed for full-stack applications with Python backend frameworks like Flask and FastAPI.
Design Inspiration: This project draws inspiration from setuptools-rust and setuptools-scm, adopting similar extension patterns and configuration approaches for seamless integration with the Python packaging ecosystem.
Overview
setuptools-nodejs extends setuptools to automatically build Node.js frontend projects and include the built artifacts in your Python packages. It's perfect for full-stack Python applications that include frontend components built with frameworks like React, Vue, Angular, etc.
Project Implementation Entry Points
Main Entry Files
setuptools_nodejs.setuptools_ext.pyprojecttoml_config- Configuration parsing entry pointsetuptools_nodejs.build.build_nodejs- Build command implementationsetuptools_nodejs.extension.NodeJSExtension- Frontend project configuration class
Project Structure
setuptools-nodejs/
├── src/setuptools_nodejs/
│ ├── setuptools_ext.py # setuptools integration and configuration parsing
│ ├── extension.py # NodeJSExtension class definition
│ ├── build.py # Build command implementation
│ ├── command.py # Command base class
│ ├── clean.py # Clean command
│ └── _utils.py # Utility functions
├── examples/vue-helloworld/ # Working example project
│ ├── browser/ # Vue frontend project
│ │ ├── package.json # Frontend dependency configuration
│ │ ├── vite.config.ts # Vite build configuration
│ │ └── src/ # Frontend source code
│ ├── python/ # Python package
│ └── pyproject.toml # Project configuration
└── tests/ # Test files
Quick Start
1. Configure Your Project
Add the following to your pyproject.toml:
[build-system]
requires = ["setuptools", "setuptools-nodejs"]
build-backend = "setuptools.build_meta"
[project]
name = "my-fullstack-app"
version = "0.1.0"
[tool.setuptools-nodejs]
frontend-projects = [
{target = "my-frontend", source_dir = "frontend", artifacts_dir = "dist"}
]
2. Build Your Package
python -m build
This will automatically:
- Build your frontend project using npm (
npm installandnpm run build) - Copy the build artifacts to the package directory
- Package everything into a Python wheel or sdist
Configuration Examples
Basic Configuration (Based on vue-helloworld example)
[build-system]
requires = ["setuptools", "setuptools-nodejs"]
build-backend = "setuptools.build_meta"
[project]
name = "vue-helloworld"
version = "0.1.0"
description = "Test project for setuptools-nodejs integration"
[tool.setuptools-nodejs]
frontend-projects = [
{target = "vue-helloworld", source_dir = "browser", artifacts_dir = "dist"}
]
[tool.setuptools.packages.find]
where = ["python"]
Multiple Frontend Projects with Output Directories
output_dir specifies the relative subdirectory path inside the package for frontend build artifacts. It does not create a separate package namespace. If not specified, it defaults to frontend.
For example, with a package named myapp:
output_dir value |
Artifact install path |
|---|---|
Not set (default frontend) |
myapp/frontend/index.html |
static/admin |
myapp/static/admin/index.html |
assets/client |
myapp/assets/client/index.html |
[tool.setuptools-nodejs]
frontend-projects = [
{target = "admin-panel", source_dir = "admin", artifacts_dir = "dist", output_dir = "static/admin"},
{target = "client-app", source_dir = "client", artifacts_dir = "build", output_dir = "static/client"}
]
Accessing Frontend Artifacts in Python Code
Use importlib.resources to access frontend build artifacts inside the package:
# Example: Serve frontend static files in Flask/FastAPI
try:
import importlib.resources as resources
except ImportError:
# Python < 3.9 fallback
import importlib_resources as resources
def get_frontend_dir(package_name: str = "myapp", sub_dir: str = "frontend") -> str:
"""Get frontend artifacts directory path, works with both
pip install -e . and pip install ."""
with resources.path(package_name, sub_dir) as path:
return str(path)
# Usage example (Flask)
from flask import Flask, send_from_directory
app = Flask(__name__)
FRONTEND_DIR = get_frontend_dir("myapp", "frontend")
@app.route("/")
def serve_index():
return send_from_directory(FRONTEND_DIR, "index.html")
@app.route("/assets/<path:filename>")
def serve_assets(filename):
return send_from_directory(FRONTEND_DIR, f"assets/{filename}")
Note: With
pip install -e .(editable install), Python references the source directory directly instead of site-packages. Frontend artifacts are automatically copied to<package_dir>/<package_name>/<output_dir>/, consistent with regular install behavior.
Advanced Configuration
[tool.setuptools-nodejs]
frontend-projects = [
{
target = "my-app",
source_dir = "frontend",
artifacts_dir = "dist",
args = ["--production"], # Additional npm arguments
quiet = false, # Show npm output
optional = false # Fail build if frontend build fails
}
]
Currently Implemented Features
✅ Implemented and Working
- Automatic Frontend Building: Automatically runs
npm installandnpm run build - Build Artifact Copying: Automatically copies frontend build artifacts to Python package
- Multiple Project Support: Supports configuring multiple frontend projects
- Configuration Parsing: Correctly parses configuration from
pyproject.toml - Vue Project Support: vue-helloworld example verified to work correctly
- Basic Error Handling: Basic build error handling
Command Line Interface
# Build frontend using configuration from pyproject.toml
python -m setuptools_nodejs build
# Build without installing dependencies
python -m setuptools_nodejs build --no-install
# Clean output directory before build
python -m setuptools_nodejs build --clean
# Verbose logging
python -m setuptools_nodejs build --verbose
Unimplemented Features and TODO List
❌ Code Exists but Untested/Incomplete
-
Framework Auto-detection Feature
_detect_artifacts_dirmethod exists but not thoroughly tested- Vue detection: Only checks config file existence, doesn't parse actual configuration
- Angular detection: Attempts to parse
angular.jsonbut error handling is simple - React detection: Only checks for "build" script, doesn't parse output directory
- Need to add test cases to verify detection logic
-
Version Check Feature
get_node_version()andget_npm_version()methods exist but never called- No actual validation of Node.js and npm versions during build process
- Need to implement version check logic and add calls
-
Dependency Declaration
- Version check requires
semantic_versionpackage but not declared in dependencies - Need to add dependency declaration in pyproject.toml
- Version check requires
Priority TODO
- Add test cases for framework auto-detection feature
- Implement Node.js and npm version check functionality
- Actually call version check methods during build process
- Add
semantic_versiondependency declaration - Improve error handling and user feedback for framework detection
- npm Version Validation: Add proper npm version checking before build
- Framework-Specific Artifacts Detection:
- Vue.js: Properly parse
vite.config.*andvue.config.jsfor output directory - Angular: Complete
angular.jsonparsing with proper error handling - React: Parse
package.jsonbuild scripts and configuration files for output paths
- Vue.js: Properly parse
- Enhanced Configuration Validation: Validate all configuration parameters before build
- Better Error Messages: Provide more informative error messages for common issues
Development
Setup Development Environment
# Clone the repository
git clone https://github.com/TuvokYang/setuptools-nodejs
cd setuptools-nodejs
# Install in editable mode
pip install -e .
# Run tests
pytest
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
If you encounter any problems or have questions, please:
- Check the documentation
- Search existing issues
- Create a new issue
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file setuptools_nodejs-0.1.4.tar.gz.
File metadata
- Download URL: setuptools_nodejs-0.1.4.tar.gz
- Upload date:
- Size: 75.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ffc5316b68ee3cae0537473aace85110ec73607d045148c9c57eecd12e20a0db
|
|
| MD5 |
1a45c65a4f8da72dc73eb29d54d1983a
|
|
| BLAKE2b-256 |
1005030185a266425b05c5f8dc96a458250e7b5bdf35a6aa45e51db6e4b89969
|
Provenance
The following attestation bundles were made for setuptools_nodejs-0.1.4.tar.gz:
Publisher:
publish-pypi.yml on TuvokYang/setuptools-nodejs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
setuptools_nodejs-0.1.4.tar.gz -
Subject digest:
ffc5316b68ee3cae0537473aace85110ec73607d045148c9c57eecd12e20a0db - Sigstore transparency entry: 1409706779
- Sigstore integration time:
-
Permalink:
TuvokYang/setuptools-nodejs@0cbe2c558ee37049afc7d648e5a57048cef7b2a2 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/TuvokYang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@0cbe2c558ee37049afc7d648e5a57048cef7b2a2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file setuptools_nodejs-0.1.4-py3-none-any.whl.
File metadata
- Download URL: setuptools_nodejs-0.1.4-py3-none-any.whl
- Upload date:
- Size: 20.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e97c2ea83abc8f3684b2d346a995b09cd8fed8346367a18825fae2178551b20
|
|
| MD5 |
6ceb5600d3c112e166dfe36a23572523
|
|
| BLAKE2b-256 |
5e50bfbd8409320b40ef7a2bc2fc930827425c6f7aa176475c7adb1649e8f2f8
|
Provenance
The following attestation bundles were made for setuptools_nodejs-0.1.4-py3-none-any.whl:
Publisher:
publish-pypi.yml on TuvokYang/setuptools-nodejs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
setuptools_nodejs-0.1.4-py3-none-any.whl -
Subject digest:
9e97c2ea83abc8f3684b2d346a995b09cd8fed8346367a18825fae2178551b20 - Sigstore transparency entry: 1409706798
- Sigstore integration time:
-
Permalink:
TuvokYang/setuptools-nodejs@0cbe2c558ee37049afc7d648e5a57048cef7b2a2 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/TuvokYang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@0cbe2c558ee37049afc7d648e5a57048cef7b2a2 -
Trigger Event:
push
-
Statement type: