Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Langgenius Dify Optimize Standalone

From Leeroopedia
Knowledge Sources
Domains Frontend, Build_Tools
Last Updated 2026-02-12 07:00 GMT

Overview

Post-build optimization script that removes unnecessary files (primarily jest-worker packages) from the Next.js standalone output to reduce production image size.

Description

optimize-standalone.js cleans up the Next.js standalone output directory (.next/standalone) by removing bundled development dependencies that are not needed at runtime. The primary targets are jest-worker packages that are included as compiled dependencies of Next.js and terser-webpack-plugin but serve no purpose in production.

The script removes three categories of paths using wildcard pattern matching:

  • jest-worker from Next.js compiled dependencies -- node_modules/.pnpm/next@*/node_modules/next/dist/compiled/jest-worker
  • jest-worker symlinks from terser-webpack-plugin -- node_modules/.pnpm/terser-webpack-plugin@*/node_modules/jest-worker
  • Standalone jest-worker packages -- node_modules/.pnpm/jest-worker@*

The removePath function handles both direct paths and wildcard-containing paths. For wildcards, it converts glob-style * patterns to regex, iterates matching directory entries, and correctly handles both symbolic links (removed via unlinkSync) and regular directories (removed via rmSync with recursive option).

After removal, the script performs a verification walk of the standalone directory to check for any remaining jest-related files, reporting them as warnings if found.

Usage

Run this script after next build and before creating a Docker image or deploying the standalone output, to minimize the production artifact size.

Code Reference

Source Location

Signature

const pathsToRemove = [
  'node_modules/.pnpm/next@*/node_modules/next/dist/compiled/jest-worker',
  'node_modules/.pnpm/terser-webpack-plugin@*/node_modules/jest-worker',
  'node_modules/.pnpm/jest-worker@*',
]

function removePath(basePath, relativePath)
const checkForJest = (dir) => { ... }

Import

// CLI script - run directly
node web/scripts/optimize-standalone.js

I/O Contract

Inputs

Name Type Required Description
.next/standalone directory Yes The Next.js standalone build output directory; must exist before running the script

Outputs

Name Type Description
.next/standalone (modified) directory The standalone directory with jest-worker packages and symlinks removed
stdout string Progress messages listing each removed path, optimization summary, and verification results
exit code 0 number Optimization completed (standalone directory exists)
exit code 1 number Standalone directory not found; next build must be run first

Usage Examples

Run after building the Next.js application

# Build the Next.js app with standalone output
cd web
pnpm build

# Optimize the standalone output
node scripts/optimize-standalone.js

Integrate into a Docker build

RUN pnpm build && node scripts/optimize-standalone.js

Verify no jest files remain

node web/scripts/optimize-standalone.js
# Output: "No jest-related files found in standalone output!"

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment