A fast asset pipeline built in Rust for processing JavaScript and SCSS.
- Node.js >= 18
- Rust >= 1.70
- npm >= 9
# Install from npm
npm install @danielhaim/packr
# Or install globally
npm install -g @danielhaim/packr
Packr can be used via the JavaScript API, a packr.json
config file, or the CLI.
const packr = require('@danielhaim/packr');
packr({
scss_input: 'src/scss/app.scss',
scss_output: 'dist/app.css',
js_input: 'src/js/app.js',
js_output: 'dist/app.js',
minify: true,
watch: false
});
Create a .packr.json
in your project root:
{
"scss_input": "src/scss/app.scss",
"scss_output": "dist/app.css",
"js_input": "src/js/app.js",
"js_output": "dist/app.js",
"css_destination": "public/css",
"js_destination": "public/js",
"minify": true,
"target": "es2020",
"verbose": true,
"sourcemap": true,
"format": "iife",
"watch": true
}
Then run:
const packr = require('@danielhaim/packr');
packr(); // Loads from .packr.json
# Default config (.packr.json)
packr
# Use a specific config file
packr --config custom/packr.json
# Enable watch mode
packr --watch
Option | Type | Default | Description |
---|---|---|---|
scss_input |
string |
required | Path to SCSS input file |
scss_output |
string |
required | Path to SCSS output file |
js_input |
string |
required | Path to JavaScript input file |
js_output |
string |
required | Path to JavaScript output file |
css_destination |
string |
— | Optional alternate output path for CSS |
js_destination |
string |
— | Optional alternate output path for JS |
minify |
boolean |
true |
Minify the output |
target |
string |
'es2020' |
JavaScript target version |
watch |
boolean |
false |
Watch files for changes |
verbose |
boolean |
false |
Enable extra console output |
sourcemap |
boolean |
false |
Include source maps in the output |
format |
string |
'iife' |
Output format: iife , cjs , or esm |
eslint |
boolean |
false |
Enable ESLint checking |
eslint_config |
string |
— | Path to custom ESLint config file |
Packr supports configuration through environment variables, which can be set in environment files in your project root. This allows for flexible configuration across different environments.
Packr loads environment variables from the following files in order of precedence (highest to lowest):
.env
(base configuration).env-{NODE_ENV}
(e.g.,.env-production
,.env-staging
).env-local
(only in development mode)
When working with environment files and sensitive data:
-
File Security
- Never commit
.env
files to version control - Use
.env.example
as a template (included in repo) - Set restrictive permissions:
chmod 600 .env*
- Keep environment files outside of public directories
- Never commit
-
Sensitive Data
- Use strong, unique values for API keys and secrets
- Avoid using test/example values in production
- Never log sensitive values in production
- Rotate secrets regularly
-
Production Checks
- Required variables are validated
- File permissions are verified
- Sensitive variables are masked in logs
- Example/test values are rejected
-
Development Safety
- Use
.env-local
for development-only values - Keep production secrets separate from development
- Document required variables in
.env.example
- Use
Variable | Description | Default |
---|---|---|
PACKR_SCSS_INPUT |
Path to SCSS input file | - |
PACKR_SCSS_OUTPUT |
Path to output compiled CSS | - |
PACKR_JS_INPUT |
Path to JavaScript input file | - |
PACKR_JS_OUTPUT |
Path to output bundled JS | - |
PACKR_CSS_DESTINATION |
Output directory for CSS | - |
PACKR_JS_DESTINATION |
Output directory for JS | - |
PACKR_MINIFY |
Minify output | true |
PACKR_TARGET |
JavaScript target | es2020 |
PACKR_VERBOSE |
Enable verbose logging | false |
PACKR_SOURCEMAP |
Generate source maps | false |
PACKR_FORMAT |
JavaScript output format (iife, cjs, esm) | iife |
PACKR_ESLINT |
Enable ESLint | false |
PACKR_ESLINT_CONFIG |
Path to ESLint config | - |
PACKR_MINIFY_JS |
JavaScript-specific minification | true |
PACKR_MINIFY_CSS |
CSS-specific minification | true |
PACKR_UGLIFY_MANGLE |
Enable name mangling | true |
PACKR_UGLIFY_KEEP_FNAMES |
Preserve function names | false |
PACKR_UGLIFY_KEEP_CLASSNAMES |
Preserve class names | false |
PACKR_UGLIFY_RESERVED |
Comma-separated list of names to preserve | - |
PACKR_SCSS_INPUT=src/scss/app.scss
PACKR_SCSS_OUTPUT=dist/app.css
PACKR_JS_INPUT=src/js/app.js
PACKR_JS_OUTPUT=dist/app.js
PACKR_CSS_DESTINATION=public/css
PACKR_JS_DESTINATION=public/js
PACKR_MINIFY=true
PACKR_TARGET=es2020
PACKR_VERBOSE=false
PACKR_SOURCEMAP=false
PACKR_FORMAT=iife
PACKR_ESLINT=false
Environment variables take precedence over configuration file options, allowing you to override settings for specific environments without changing your configuration files.
Packr provides granular control over minification and uglification:
PACKR_MINIFY
: Global switch for all minification (default:true
)PACKR_MINIFY_JS
: JavaScript-specific minification (default:true
)PACKR_MINIFY_CSS
: CSS-specific minification (default:true
)
PACKR_UGLIFY_MANGLE
: Enable name mangling (default:true
)PACKR_UGLIFY_KEEP_FNAMES
: Preserve function names (default:false
)PACKR_UGLIFY_KEEP_CLASSNAMES
: Preserve class names (default:false
)PACKR_UGLIFY_RESERVED
: Comma-separated list of names to preserve (e.g.,jQuery,$
)
Example configuration in .packr.json
:
{
"minify": true,
"minify_js": true,
"minify_css": true,
"uglify": {
"mangle": true,
"keep_fnames": false,
"keep_classnames": false,
"reserved": ["jQuery", "$"]
}
}
The minification process:
-
JavaScript:
- Removes whitespace and comments
- Shortens variable names (mangling)
- Optimizes code structure
- Preserves specified names
-
CSS:
- Removes whitespace and comments
- Combines identical selectors
- Optimizes property values
- Removes unused rules
Packr includes a comprehensive test suite that covers various build scenarios:
-
Default Configuration
- Tests default Packr settings with IIFE format and sourcemaps
- Verifies basic SCSS and JavaScript compilation
-
Custom Config File
- Tests custom configuration with different paths and settings
- Validates alternate destination paths for CSS and JS
-
ESM Format
- Tests Packr with ESM (ECMAScript Modules) output format
- Ensures proper module bundling
-
No Sourcemaps
- Tests builds with sourcemaps disabled
- Verifies clean output without map files
-
Minification
- Tests minification of both SCSS and JavaScript
- Validates
.min
file generation
-
ESLint Integration
- Tests JavaScript linting with ESLint
- Provides detailed warning summaries with line numbers
- Supports custom ESLint configurations
# Run all tests
node __tests__/test.js
# Run specific test
node __tests__/test.js --test "Default Packr Configuration"
node __tests__/test.js --test "Custom Config File"
node __tests__/test.js --test "ESM Format"
node __tests__/test.js --test "No Sourcemaps"
node __tests__/test.js --test "Minification"
node __tests__/test.js --test "ESLint"
Packr includes built-in ESLint support for JavaScript files:
{
"eslint": true,
"eslintConfig": ".eslintrc.json"
}
ESLint warnings are displayed in a detailed format:
File: src/js/app.js
Warnings:
Line 8, Column 5: no-var - Unexpected var, use let or const instead.
Line 15, Column 9: prefer-const - 'y' is never reassigned. Use 'const' instead.
Line 16, Column 13: semi - Missing semicolon.
Line 38, Column 5: no-var - Unexpected var, use let or const instead.
Packr uses Rust for fast and efficient asset builds:
- SCSS: Compiled with the Rust-based
grass
- JavaScript: Bundled using
esbuild
- CSS Optimization: Handled by
lightningcss
- Memory + Speed: Lightweight, with fast cold and incremental builds
- PostCSS integration
- SortCSS integration
- TypeScript support
- CSS Modules support
- Source map improvements
- Incremental builds
- Tree-shaking / dead code elimination
- Asset hashing / versioning
- Multi-entry compilation support
- Improve environment-based config support (
.env
, NODE_ENV, etc.)
- Watch mode with hot reload hooks
- Verbose and colorized logging
- Build profiling (timing per task)
- Interactive CLI or TUI dashboard
- Custom JS output formats (CommonJS, ESM, IIFE)
- Configurable output directory structure
See the Contributing Guide for details on submitting issues or PRs.
If you find a security issue, please report it privately via a GitHub discussion. Avoid using the issue tracker.
Packr is licensed under the Apache License 2.0.
Packr is built with:
- esbuild – JavaScript bundler
- grass – SCSS compiler in Rust
- lightningcss – CSS optimizer