dce-edge

Describes DCE-safe require() patterns compatible with edge runtime constraints. Intended for writing conditional require() calls, guarding Node.js-only imports (e.g., node:stream), or editing define-env-plugin.ts, app-render, or stream-utils to fit edge builds. Covers webpack DCE handling of if/else branches, explicit TypeScript assignments, the difference between NEXT_RUNTIME and actual feature flags, and forcing flags to false in define-env.ts for edge environments. Metadata: internal: true.

Last repository push
GitHub Stars
143,631
Forks
32,732
Issues / PRs
4,112

Screenshots

A demonstration from skillπ Lab

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "dce-edge" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-01eed7a9139719c00ef756db063bbd2c0acdcffb5d2cde6d4edc4356d3977e31. Verify the upstream source and version against https://github.com/vercel/next.js before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

github.com

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

Describes DCE-safe require() patterns compatible with edge runtime constraints. Intended for writing conditional require() calls, guarding Node.js-only imports (e.g., node:stream), or editing define-env-plugin.ts, app-render, or stream-utils to fit edge builds. Covers webpack DCE handling of if/else branches, explicit TypeScript assignments, the difference between NEXT_RUNTIME and actual feature flags, and forcing flags to false in define-env.ts for edge environments. Metadata: internal: true.

0 Downloads

Introduction source: third_party · vercel · 1.0.0 Reference

SkillPi interpretation

Tasks
Use this skill when changing conditional `require()` paths, Node-only imports, or edge/runtime branching.
Audience
Not specified in the source documentation.
Inputs and outputs
Not specified in the source documentation.
Software, accounts and paid services
Not specified in the source documentation.
Limits and differences
Edge routes do NOT use pre-compiled runtime bundles. They are compiled by the user's webpack/Turbopack, so `define-env.ts` controls DCE. Feature flags that gate `node:*` imports must be forced to `false` for edge builds in `define-env.ts` (`isEdgeServer ? false : flagValue`), otherwise webpack will try to resolve `node:stream` etc. and fail.
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: dce-edge
description: >
  DCE-safe require() patterns and edge runtime constraints. Use when writing
  conditional require() calls, guarding Node-only imports (node:stream etc.),
  or editing define-env-plugin.ts / app-render / stream-utils for edge builds.
  Covers if/else branching for webpack DCE, TypeScript definite assignment,
  the NEXT_RUNTIME vs real feature flag distinction, and forcing flags false
  for edge in define-env.ts.
metadata:
  internal: true

DCE + Edge

Use this skill when changing conditional require() paths, Node-only imports, or edge/runtime branching.

DCE-Safe require() Pattern

Webpack only DCEs a require() when it sits inside the dead branch of an if/else whose condition DefinePlugin can evaluate at compile time.

// CORRECT - webpack can eliminate the dead branch
if (process.env.__NEXT_USE_NODE_STREAMS) {
  require('node:stream')
} else {
  // web path
}

What does NOT work:

  • Early-return/throw guards: webpack doesn't do control-flow analysis for throws/returns, so the require() is still traced.
  • Bare if without else: works for inline node:* specifiers but NOT for require('./some-module') that pulls a new file into the module graph.

Always test edge changes with pnpm test-start-webpack on test/e2e/app-dir/app/standalone.test.ts (has edge routes), not with NEXT_SKIP_ISOLATE=1 which skips the full webpack compilation.

TypeScript + DCE Interaction

Use if/else (not two independent if blocks) when assigning a variable conditionally on process.env.X. TypeScript cannot prove exhaustiveness across if (flag) { x = a }; if (!flag) { x = b } and will error with "variable used before being assigned". The if/else pattern satisfies both TypeScript (definite assignment) and webpack DCE.

Compile-Time Switcher Pattern

Platform-specific code (node vs web) can use a single .ts switcher module that conditionally require()s either .node.ts or .web.ts into a typed variable, then re-exports the shared runtime API as named exports. Keep the branch as if/else so DefinePlugin can dead-code-eliminate the unused require(). Keep shared types canonical in .node.ts, with .web.ts importing them via import type and the switcher re-exporting types as needed. Examples: stream-ops.ts and debug-channel-server.ts.

NEXT_RUNTIME Is Not a Feature Flag

In user-project webpack server compilers, process.env.NEXT_RUNTIME is inlined to 'nodejs'. Guarding Node-only require('node:*') paths with NEXT_RUNTIME === 'nodejs' does not prune anything. For feature-gated codepaths, guard on the real feature define (e.g. process.env.__NEXT_USE_NODE_STREAMS).

Edge Runtime Constraints

Edge routes do NOT use pre-compiled runtime bundles. They are compiled by the user's webpack/Turbopack, so define-env.ts controls DCE. Feature flags that gate node:* imports must be forced to false for edge builds in define-env.ts (isEdgeServer ? false : flagValue), otherwise webpack will try to resolve node:stream etc. and fail.

app-page.ts Template Gotchas

  • app-page.ts is a build template compiled by the user's bundler. Any require() in this file is traced by webpack/turbopack at next build time. You cannot require internal modules with relative paths because they won't be resolvable from the user's project. Instead, export new helpers from entry-base.ts and access them via entryBase.* in the template.
  • Template helpers should stay out of RenderResult. If app-page.ts needs a Node-stream-only utility, prefer a small dedicated helper module in server/stream-utils/ (with DCE-safe if/else + require()).

Verification

  • Validate edge bundling regressions with pnpm test-start-webpack test/e2e/app-dir/app/standalone.test.ts
  • For module-resolution/build-graph fixes, verify without NEXT_SKIP_ISOLATE=1

Related Skills

  • $flags - flag wiring (config/schema/define-env/runtime env)
  • $react-vendoring - entry-base boundaries and vendored React
  • $runtime-debug - reproduction and verification workflow

Current listing version

1.0.0

Verify the source revision ↗

Safety and assessment details

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value62/100

Highlights

  • 提供 DCE 安全的 require() 模式与边缘运行时约束的具体实现方案
  • 涵盖 TypeScript 明确赋值、NEXT_RUNTIME 与功能标志区别等关键细节
  • 包含验证命令与测试路径,具备可操作性
Trigger Precision92/100

Highlights

  • 触发场景描述精准,覆盖 conditional require、Node-only imports、define-env 编辑等核心用例
  • 边界清晰,能有效区分适用与不适用场景
Structure and Standards78/100

Areas to improve

  • 缺少 When to Use 章节与文档过长 Reported severity: Minor

    文档未设置显式的 'When to Use' 章节标题,且内容长度超过 500 行

    Recommendation: 建议添加 'When to Use' 章节以提升可读性,并考虑拆分超长内容为子章节

Highlights

  • 文档结构逻辑清晰,分节合理,代码示例充分
  • frontmatter 完整,元数据标注 internal: true 符合规范
Execution Reliability94/100

Highlights

  • 提供具体验证命令(pnpm test-start-webpack)与测试文件路径
  • 明确指出常见误用场景(如 NEXT_SKIP_ISOLATE 的跳过问题)
  • 工具来源与参数说明清晰,覆盖主要异常处理
Evidence Verifiability (mean of the first four)82/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

These common operations were not identified in the main document. This does not mean no permissions or risks exist.

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Not specified in the source documentation.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.