CodeSampleX

Exemple

@rollup/plugin-commonjs 28.0.2: dynamicRequireTargets

Échantillon vérifié pour npm @rollup/plugin-commonjs 28.0.2: dynamicRequireTargets. Le contrat s'est exécuté sur node 22 · linux debian/x64 · docker et a…

sha256:106823d3a8ae58f11ed6eced961782c1e4a206410e028262cc5f6e8239a865c3

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré node linux 24 · ubuntu · glibc 2.39 x64 node javascript npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-03

Cas

HOW
Objectif
verify dynamicRequireTargets in pkg:npm/%40rollup/plugin-commonjs@28.0.2
Paquets
Symboles
  • dynamicRequireTargets
Environnement
node
Créé
2026-09-03T13:26:57Z

Contrat

  1. By default, @rollup/plugin-commonjs transforms dynamic require calls into commonjsRequire helper calls that throw an error instructing the user to configure dynamicRequireTargets or ignoreDynamicRequires.
  2. Setting dynamicRequireTargets with glob patterns crawls matching modules and generates an internal dynamic modules registry mapping virtual paths to bundled module factory functions.
  3. Executing a bundle configured with dynamicRequireTargets resolves dynamic require calls for targeted modules at runtime, returning their exported values.
  4. Executing a dynamic require call for a module not matched by dynamicRequireTargets throws the commonjsRequire error.
  5. Dynamic require glob patterns support negation (prefixed with '!') to selectively exclude specific files from the dynamic modules registry.
  6. When dynamicRequireTargets is combined with ignoreDynamicRequires: true, unmatched dynamic require calls fall back to invoking the ambient require function instead of throwing.

Fichiers

  • NOTES.md
  • PROMPT.md
  • csx.json
  • fixtures/alpha.js
  • fixtures/beta.js
  • fixtures/excluded.js
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

Télécharger l’artefact source (tar.gz)

Code source

NOTES.md
# Notes: Rollup CommonJS Plugin Dynamic Require Resolution with dynamicRequireTargets

## Search Result
`search_known_solution` returned `NO_SAFE_MATCH` for `dynamicRequireTargets` in `@rollup/plugin-commonjs@28.0.2`. While public samples for the 28 series cover `strictRequires`, `requireReturnsDefault`, and `transformMixedEsModules`, dynamic require handling via `dynamicRequireTargets` had no verified contracts. This sample demonstrates dynamic module registry generation, runtime resolution, pattern negation, and fallback semantics when bundling CommonJS dynamic requires.

## Naive Model Assumption
Developers and models often assume that `@rollup/plugin-commonjs` either automatically crawls and bundles all potential dynamic require targets at runtime, or conversely that dynamic require expressions are simply left untouched as native requires without explicit configuration.

## Failure Mode
By default, `@rollup/plugin-commonjs` rewrites dynamic `require(...)` calls to an internal `commonjsRequire` helper that unconditionally throws an error at runtime (`Error: Could not dynamically require "...". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.`). Configuring `dynamicRequireTargets` instructs Rollup to crawl matching files, bundle them as wrapped modules, and register them in an internal dynamic modules table (`getDynamicModules()`) so that dynamic require calls resolve properly at runtime. Glob negation (`!`) selectively excludes files from bundling, and combining `dynamicRequireTargets` with `ignoreDynamicRequires: true` enables fallback to the ambient runtime `require` for unmatched targets.
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify dynamicRequireTargets in pkg:npm/%40rollup/plugin-commonjs@28.0.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40rollup/plugin-commonjs@28.0.2
Demonstrate these symbols/APIs:
  - dynamicRequireTargets

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"believed":"When bundling CommonJS modules containing dynamic require calls, @rollup/plugin-commonjs resolves dynamic calls automatically or leaves them untouched without requiring explicit configuration.","caseId":"case:sha256:2179a84e8465c2ff0b65e5a102d55c06d1a68317f4e718c017d5400b9432638b","constraints":{"executionContext":"node"},"contract":["By default, @rollup/plugin-commonjs transforms dynamic require calls into commonjsRequire helper calls that throw an error instructing the user to configure dynamicRequireTargets or ignoreDynamicRequires.","Setting dynamicRequireTargets with glob patterns crawls matching modules and generates an internal dynamic modules registry mapping virtual paths to bundled module factory functions.","Executing a bundle configured with dynamicRequireTargets resolves dynamic require calls for targeted modules at runtime, returning their exported values.","Executing a dynamic require call for a module not matched by dynamicRequireTargets throws the commonjsRequire error.","Dynamic require glob patterns support negation (prefixed with '!') to selectively exclude specific files from the dynamic modules registry.","When dynamicRequireTargets is combined with ignoreDynamicRequires: true, unmatched dynamic require calls fall back to invoking the ambient require function instead of throwing."],"goal":"verify dynamicRequireTargets in pkg:npm/%40rollup/plugin-commonjs@28.0.2","kind":"HOW","packages":["pkg:npm/%40rollup/plugin-commonjs@28.0.2"],"schemaVersion":1,"symbols":["dynamicRequireTargets"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40rollup/plugin-commonjs@28.0.2"],"schemaVersion":1,"subject":"pkg:npm/%40rollup/plugin-commonjs@28.0.2","symbols":["dynamicRequireTargets"],"verifierAdapter":"node-typescript@1"}
fixtures/alpha.js
module.exports = { name: 'alpha', value: 10 };
fixtures/beta.js
module.exports = { name: 'beta', value: 20 };
fixtures/excluded.js
module.exports = { name: 'excluded', value: 99 };
package-lock.json
{
  "name": "sample-rollup-plugin-commonjs-dynamic-require-targets",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-rollup-plugin-commonjs-dynamic-require-targets",
      "version": "1.0.0",
      "dependencies": {
        "@rollup/plugin-commonjs": "28.0.2",
        "rollup": "4.62.4"
      }
    },
    "node_modules/@jridgewell/sourcemap-codec": {
      "version": "1.6.0",
      "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.6.0.tgz",
      "integrity": "sha512-T7jf+5zgsZHwNJ4lvQ7/aezbyk0nNX+zJVWpmHA7VYsEx7a7qr5Rg5IbtJFqkgze5Y2sruq1RUY8Q837Od7iFw==",
      "license": "MIT"
    },
    "node_modules/@napi-rs/lzma-linux-x64-gnu": {
      "version": "1.5.1",
      "resolved": "https://registry.npmjs.org/@napi-rs/lzma-linux-x64-gnu/-/lzma-linux-x64-gnu-1.5.1.tgz",
      "integrity": "sha512-oTXEIha4SsuXdTA4Iyskj0kpdx2yVXdhd75c2v3xGrHFfVMsbhTPZU/nMPL4sWKo4pBHm3aucLaqGlF696dTyQ==",
      "cpu": [
        "x64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": "^22.20 || ^24.12 || >=25"
      }
    },
    "node_modules/@rollup/plugin-commonjs": {
      "version": "28.0.2",
      "resolved": "https://registry.npmjs.org/@rollup/plugin-commonjs/-/plugin-commonjs-28.0.2.tgz",
      "integrity": "sha512-BEFI2EDqzl+vA1rl97IDRZ61AIwGH093d9nz8+dThxJNH8oSoB7MjWvPCX3dkaK1/RCJ/1v/R1XB15FuSs0fQw==",
      "license": "MIT",
      "dependencies": {
        "@rollup/pluginutils": "^5.0.1",
        "commondir": "^1.0.1",
        "estree-walker": "^2.0.2",
        "fdir": "^6.2.0",
        "is-reference": "1.2.1",
        "magic-string": "^0.30.3",
        "picomatch": "^4.0.2"
      },
      "engines": {
        "node": ">=16.0.0 || 14 >= 14.17"
      },
      "peerDependencies": {
        "rollup": "^2.68.0||^3.0.0||^4.0.0"
      },
      "peerDependenciesMeta": {
        "rollup": {
          "optional": true
        }
      }
    },
    "node_modules/@rollup/pluginutils": {
      "version": "5.4.0",
      "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.4.0.tgz",
      "integrity": "sha512-MfPp06CjRLfXQ3wY0R8vJDYBy/MvVcc9OulEfR0B8Iv9ko+GCNaRZ+EpJYFl27LhKsZK0o420sYCRHCjfCgeUg==",
      "license": "MIT",
      "dependencies": {
        "@types/estree": "^1.0.0",
        "estree-walker": "^2.0.2",
        "picomatch": "^4.0.2"
      },
      "engines": {
        "node": ">=14.0.0"
      },
      "peerDependencies": {
        "rollup": "^1.20.0||^2.0.0||^3.0.0||^4.0.0"
      },
      "peerDependenciesMeta": {
        "rollup": {
          "optional": true
        }
      }
    },
    "node_modules/@rollup/rollup-android-arm-eabi": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.62.4.tgz",
      "integrity": "sha512-RrPokAb7dmbxFoeO3TloqHyOjgye8RkBhSqmp4aJMIex4c9r46ZstPnleDQOq1t46VOVjwIuwNogIqbodV1Vvg==",
      "cpu": [
        "arm"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "android"
      ]
    },
    "node_modules/@rollup/rollup-android-arm64": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm64/-/rollup-android-arm64-4.62.4.tgz",
      "integrity": "sha512-JKuJc+pnpks2pjy7L/N3v/cAkZxYlnmuZoD840ldbMI5KDbC4iO9NKwPKYdjYFCMAIIlBzYSFHxIJVYzRo2/8A==",
      "cpu": [
        "arm64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "android"
      ]
    },
    "node_modules/@rollup/rollup-darwin-arm64": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-arm64/-/rollup-darwin-arm64-4.62.4.tgz",
      "integrity": "sha512-krw5uS2STmvJ02x0uTXHbqQNuz+9eZ1iw+qXk9dmW2gvV4jV7O2hEoOnuhFrpOPiel1mBFtqbxYZZtC46hXLOw==",
      "cpu": [
        "arm64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "darwin"
      ]
    },
    "node_modules/@rollup/rollup-darwin-x64": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-x64/-/rollup-darwin-x64-4.62.4.tgz",
      "integrity": "sha512-wsTxtgApb4PrOsNJIm0FZ1h3WvCC+k9uxLJ4ad75hgoS4NiRes2SoJFlDAyMwiUY8IssDqGcHbXuN0sx1tfF1A==",
      "cpu": [
        "x64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "darwin"
      ]
    },
    "node_modules/@rollup/rollup-freebsd-arm64": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-arm64/-/rollup-freebsd-arm64-4.62.4.tgz",
      "integrity": "sha512-GUOnQlyZe3yAXhWOtOMsn5Qkrv5E5mZXa0thbARWi5Ei2szlVXJFQhddZ4HbAzh8q92w5twp+CQvs/eFanz9YQ==",
      "cpu": [
        "arm64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "freebsd"
      ]
    },
    "node_modules/@rollup/rollup-freebsd-x64": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-x64/-/rollup-freebsd-x64-4.62.4.tgz",
      "integrity": "sha512-/Y7f3QuxjzPKsjA/rfEDa3+0vXqyjmJ50Ln8dPpCmWkKTrUoWHG1cWhTqaAMLob2m2nESWuC7yGrREz019Ztqg==",
      "cpu": [
        "x64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "freebsd"
      ]
    },
    "node_modules/@rollup/rollup-linux-arm-gnueabihf": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-gnueabihf/-/rollup-linux-arm-gnueabihf-4.62.4.tgz",
      "integrity": "sha512-81wiiX3v7aqy+T+bT61TJ78yJjRquqFFTTbAPt08imfQQzkPIW8t6aJbkTagtCCrXMNc9D66+geqlK7ydLPNqA==",
      "cpu": [
        "arm"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ]
    },
    "node_modules/@rollup/rollup-linux-arm-musleabihf": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-musleabihf/-/rollup-linux-arm-musleabihf-4.62.4.tgz",
      "integrity": "sha512-9kmDIvNZqdoHOBZgNtpTBeLWYO/LVipM3H/j62P8848/l/VPEQL6N3uxU9pvP1oZAsXyC2MEnFP3ovRjo7WYNQ==",
      "cpu": [
        "arm"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ]
    },
    "node_modules/@rollup/rollup-linux-arm64-gnu": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-gnu/-/rollup-linux-arm64-gnu-4.62.4.tgz",
      "integrity": "sha512-CcnXHWnXg69g+DX5VWL3FHts3qMRN2uVEHX+BZvGLdd07/gXkn3ePjYtO1LDJvxkGKVHMclKBRa1QUTH+6toYQ==",
      "cpu": [
        "arm64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ]
    },
    "node_modules/@rollup/rollup-linux-arm64-musl": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-musl/-/rollup-linux-arm64-musl-4.62.4.tgz",
      "integrity": "sha512-iFOibiHnTRuhrWLlRsOQFdZJJIa7S8OwkneJr4ocALP16u5yk6lWLINFwhHaEqBFMsKDUZofLkGos7+CPzGB3g==",
      "cpu": [
        "arm64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ]
    },
    "node_modules/@rollup/rollup-linux-loong64-gnu": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-loong64-gnu/-/rollup-linux-loong64-gnu-4.62.4.tgz",
      "integrity": "sha512-XnWYMI7euHlb5a871xPja+Gm7DRCFU+FGRrtS2sMq9N8FvqtpagUy6gD4YOemC5MRk9xbh8+jYMEJbigFQwsgA==",
      "cpu": [
        "loong64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ]
    },
    "node_modules/@rollup/rollup-linux-loong64-musl": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-loong64-musl/-/rollup-linux-loong64-musl-4.62.4.tgz",
      "integrity": "sha512-qGDAlO0U8xedCcsdRm9oaoQY8DAx/QT7uIxJWhCdx0ceIWX783UC9QSYkdpzAe29wNiVfp24+bZdQmn49o45SQ==",
      "cpu": [
        "loong64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ]
    },
    "node_modules/@rollup/rollup-linux-ppc64-gnu": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-ppc64-gnu/-/rollup-linux-ppc64-gnu-4.62.4.tgz",
      "integrity": "sha512-ru4H6ezD7ysA5EiEK6qkkaEb4modH8CTej6kUy/gQi20u3kB3G7Zn8snXXkeJSCOFKG/rbPPtM/+9Wgas1961w==",
      "cpu": [
        "ppc64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ]
    },
    "node_modules/@rollup/rollup-linux-ppc64-musl": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-ppc64-musl/-/rollup-linux-ppc64-musl-4.62.4.tgz",
      "integrity": "sha512-2W4MO5WQVJnbJaZdvDb9rhBDuFU1nKIepPFpJUBsTh2k1YY2g+ODViaWuyOAjQ5cOP7NvrvLzt3wvHOoiAvc7w==",
      "cpu": [
        "ppc64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ]
    },
    "node_modules/@rollup/rollup-linux-riscv64-gnu": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-gnu/-/rollup-linux-riscv64-gnu-4.62.4.tgz",
      "integrity": "sha512-+fxjfuoAmVMCYV5QyjoIpu0cp5DOiOTeqYFk1AVaxGr+/ravWLX89XfQmptsoWcaVy/TGf2hexzbUOrCQIL1CQ==",
      "cpu": [
        "riscv64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ]
    },
    "node_modules/@rollup/rollup-linux-riscv64-musl": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-musl/-/rollup-linux-riscv64-musl-4.62.4.tgz",
      "integrity": "sha512-jTn8JfHGL4djjFxPuM06LmNUJDsst2jeVlsd9OmIH6zc5sC9K6rIuO4YajXatLUpBmBKl6b35ro1QZocLi+tcA==",
      "cpu": [
        "riscv64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ]
    },
    "node_modules/@rollup/rollup-linux-s390x-gnu": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-s390x-gnu/-/rollup-linux-s390x-gnu-4.62.4.tgz",
      "integrity": "sha512-oCJCJL4pXsoDcP2QZ+JVlPTIRc6266zsIaeJJsWImmF7HO0W8nb6HuSgZlMWxJwaPf8ehbSw8yo0EUw925hKsA==",
      "cpu": [
        "s390x"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ]
    },
    "node_modules/@rollup/rollup-linux-x64-gnu": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.62.4.tgz",
      "integrity": "sha512-W69hukhZ3KKNRCaMIEzKvcFye42hh0FE1+YoYaf5+Ikacuftoco6yO/xouz0hc5d5W/s3yBro5jRiuEE/Q5vUw==",
      "cpu": [
        "x64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ]
    },
    "node_modules/@rollup/rollup-linux-x64-musl": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-musl/-/rollup-linux-x64-musl-4.62.4.tgz",
      "integrity": "sha512-qiXbGG2jkjXhzXpsFZSR2Xpb8DN/UaxYsbb/STbuR/6fpaDgRmmaq1B/LmtF2wQFOFOSsK2jdE0RZ3a0zHn4QA==",
      "cpu": [
        "x64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "linux"
      ]
    },
    "node_modules/@rollup/rollup-openbsd-x64": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-openbsd-x64/-/rollup-openbsd-x64-4.62.4.tgz",
      "integrity": "sha512-nWeM//hxv8mIo6jD7Hu4o48DVmV9pbV6gsKaWU+4NFyqHoPKwrkRiZGLKUhOBk8qNmDmpwFtPKg80Bo/Tn4xiQ==",
      "cpu": [
        "x64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "openbsd"
      ]
    },
    "node_modules/@rollup/rollup-openharmony-arm64": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-openharmony-arm64/-/rollup-openharmony-arm64-4.62.4.tgz",
      "integrity": "sha512-s62SQ/vgsRSvMwDkOEfTqfgASF0f26ZNaQuTA6Aok5lrikf89yI2W0gFHvZb2Jpgc6N8JnOKZgCK2iciO3CsxQ==",
      "cpu": [
        "arm64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "openharmony"
      ]
    },
    "node_modules/@rollup/rollup-win32-arm64-msvc": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.62.4.tgz",
      "integrity": "sha512-J6wGf8TVGbXJq+HH+ttTvrcfNKPbuZecV6KT1B8I18BC5IURUh5kl4Yl5OEP5eFIUoI5BWxCsyYMhFsDx8kekw==",
      "cpu": [
        "arm64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "win32"
      ]
    },
    "node_modules/@rollup/rollup-win32-ia32-msvc": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.62.4.tgz",
      "integrity": "sha512-zmfrQd/0wu6oJs8Vq8KwY/YtsKSsLtKe/HwAP4Wqy8LhWjeT55fHRAkOhYQ12wI3ayS4Tt12d5CDRD7N96SAYQ==",
      "cpu": [
        "ia32"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "win32"
      ]
    },
    "node_modules/@rollup/rollup-win32-x64-gnu": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-gnu/-/rollup-win32-x64-gnu-4.62.4.tgz",
      "integrity": "sha512-qPzHqdj9rfUD+w79dtE07zi/kFwKyCJqplp5K5ygeLTp7jLpAoc16OAH39HSmRC9UpozaecsleI8uAdEj6v2yw==",
      "cpu": [
        "x64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "win32"
      ]
    },
    "node_modules/@rollup/rollup-win32-x64-msvc": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.62.4.tgz",
      "integrity": "sha512-zD6NdeWEByGE9QF9vCrlJ5YQB4oq9q91kPZS37Jwj5hOkvR1lTBSpsKhKDw4IJtbQ35LsTS1HD9DZYGKIshU1Q==",
      "cpu": [
        "x64"
      ],
      "license": "MIT",
      "optional": true,
      "os": [
        "win32"
      ]
    },
    "node_modules/@types/estree": {
      "version": "1.0.9",
      "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.9.tgz",
      "integrity": "sha512-GhdPgy1el4/ImP05X05Uw4cw2/M93BCUmnEvWZNStlCzEKME4Fkk+YpoA5OiHNQmoS7Cafb8Xa3Pya8m1Qrzeg==",
      "license": "MIT"
    },
    "node_modules/commondir": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz",
      "integrity": "sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==",
      "license": "MIT"
    },
    "node_modules/estree-walker": {
      "version": "2.0.2",
      "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-2.0.2.tgz",
      "integrity": "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==",
      "license": "MIT"
    },
    "node_modules/fdir": {
      "version": "6.5.0",
      "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz",
      "integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==",
      "license": "MIT",
      "engines": {
        "node": ">=12.0.0"
      },
      "peerDependencies": {
        "picomatch": "^3 || ^4"
      },
      "peerDependenciesMeta": {
        "picomatch": {
          "optional": true
        }
      }
    },
    "node_modules/fsevents": {
      "version": "2.3.3",
      "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
      "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
      "hasInstallScript": true,
      "license": "MIT",
      "optional": true,
      "os": [
        "darwin"
      ],
      "engines": {
        "node": "^8.16.0 || ^10.6.0 || >=11.0.0"
      }
    },
    "node_modules/is-reference": {
      "version": "1.2.1",
      "resolved": "https://registry.npmjs.org/is-reference/-/is-reference-1.2.1.tgz",
      "integrity": "sha512-U82MsXXiFIrjCK4otLT+o2NA2Cd2g5MLoOVXUZjIOhLurrRxpEXzI8O0KZHr3IjLvlAH1kTPYSuqer5T9ZVBKQ==",
      "license": "MIT",
      "dependencies": {
        "@types/estree": "*"
      }
    },
    "node_modules/magic-string": {
      "version": "0.30.21",
      "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz",
      "integrity": "sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==",
      "license": "MIT",
      "dependencies": {
        "@jridgewell/sourcemap-codec": "^1.5.5"
      }
    },
    "node_modules/picomatch": {
      "version": "4.0.7",
      "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.7.tgz",
      "integrity": "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA==",
      "license": "MIT",
      "engines": {
        "node": ">=12"
      },
      "funding": {
        "url": "https://github.com/sponsors/jonschlinkert"
      }
    },
    "node_modules/rollup": {
      "version": "4.62.4",
      "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.62.4.tgz",
      "integrity": "sha512-RXOqwaPsBGjMNMa4sQjDjHieHEZDFoj/Rdr46l2MU5DfEs16wHJPC2RPTPHWhNl+M3aI472LLqFkFKut4SblOg==",
      "license": "MIT",
      "dependencies": {
        "@types/estree": "1.0.9"
      },
      "bin": {
        "rollup": "dist/bin/rollup"
      },
      "engines": {
        "node": ">=18.0.0",
        "npm": ">=8.0.0"
      },
      "optionalDependencies": {
        "@napi-rs/lzma-linux-x64-gnu": "1.5.1",
        "@rollup/rollup-android-arm-eabi": "4.62.4",
        "@rollup/rollup-android-arm64": "4.62.4",
        "@rollup/rollup-darwin-arm64": "4.62.4",
        "@rollup/rollup-darwin-x64": "4.62.4",
        "@rollup/rollup-freebsd-arm64": "4.62.4",
        "@rollup/rollup-freebsd-x64": "4.62.4",
        "@rollup/rollup-linux-arm-gnueabihf": "4.62.4",
        "@rollup/rollup-linux-arm-musleabihf": "4.62.4",
        "@rollup/rollup-linux-arm64-gnu": "4.62.4",
        "@rollup/rollup-linux-arm64-musl": "4.62.4",
        "@rollup/rollup-linux-loong64-gnu": "4.62.4",
        "@rollup/rollup-linux-loong64-musl": "4.62.4",
        "@rollup/rollup-linux-ppc64-gnu": "4.62.4",
        "@rollup/rollup-linux-ppc64-musl": "4.62.4",
        "@rollup/rollup-linux-riscv64-gnu": "4.62.4",
        "@rollup/rollup-linux-riscv64-musl": "4.62.4",
        "@rollup/rollup-linux-s390x-gnu": "4.62.4",
        "@rollup/rollup-linux-x64-gnu": "4.62.4",
        "@rollup/rollup-linux-x64-musl": "4.62.4",
        "@rollup/rollup-openbsd-x64": "4.62.4",
        "@rollup/rollup-openharmony-arm64": "4.62.4",
        "@rollup/rollup-win32-arm64-msvc": "4.62.4",
        "@rollup/rollup-win32-ia32-msvc": "4.62.4",
        "@rollup/rollup-win32-x64-gnu": "4.62.4",
        "@rollup/rollup-win32-x64-msvc": "4.62.4",
        "fsevents": "~2.3.2"
      }
    }
  }
}
package.json
{
  "name": "sample-rollup-plugin-commonjs-dynamic-require-targets",
  "version": "1.0.0",
  "dependencies": {
    "@rollup/plugin-commonjs": "28.0.2",
    "rollup": "4.62.4"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify dynamicRequireTargets in pkg:npm/%40rollup/plugin-commonjs@28.0.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40rollup/plugin-commonjs@28.0.2"
  ],
  "symbols": [
    "dynamicRequireTargets"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import path from 'node:path';
import { rollup } from 'rollup';
import commonjs from '@rollup/plugin-commonjs';

async function bundleEntry(sourceCode, pluginOptions = {}) {
  const entryId = path.resolve('virtual-entry.js');
  const bundle = await rollup({
    input: entryId,
    plugins: [
      {
        name: 'virtual-entry-plugin',
        resolveId(id) {
          if (id === entryId) return id;
        },
        load(id) {
          if (id === entryId) return sourceCode;
        }
      },
      commonjs(pluginOptions)
    ]
  });
  const { output } = await bundle.generate({ format: 'cjs' });
  return output[0].code;
}

function evaluateCjs(code, ambientRequire = () => { throw new Error('ambient require not expected'); }) {
  const fn = new Function('exports', 'module', 'require', code);
  const mod = { exports: {} };
  fn(mod.exports, mod, ambientRequire);
  return mod.exports;
}

async function runContracts() {
  const entrySource = `
    function loadDynamic(name) {
      return require('./fixtures/' + name);
    }
    module.exports = { loadDynamic };
  `;

  // Contract 1:
  // By default, @rollup/plugin-commonjs transforms dynamic require calls into
  // commonjsRequire helper calls that throw an error instructing the user to
  // configure dynamicRequireTargets or ignoreDynamicRequires.
  {
    const code = await bundleEntry(entrySource);

    assert.match(
      code,
      /function commonjsRequire\(path\)/,
      'Default transform emits commonjsRequire helper'
    );
    assert.match(
      code,
      /Could not dynamically require/,
      'Default transform contains error message mentioning dynamicRequireTargets'
    );

    const mod = evaluateCjs(code);
    assert.throws(
      () => mod.loadDynamic('alpha.js'),
      {
        name: 'Error',
        message:
          'Could not dynamically require "./fixtures/alpha.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'
      },
      'Dynamic require without dynamicRequireTargets throws expected error'
    );
  }

  // Contract 2:
  // Setting dynamicRequireTargets with glob patterns crawls matching modules and
  // generates an internal dynamic modules registry mapping virtual paths to
  // bundled module factory functions.
  {
    const code = await bundleEntry(entrySource, {
      dynamicRequireTargets: ['fixtures/*.js']
    });

    assert.match(
      code,
      /function createCommonjsRequire\(/,
      'Emits createCommonjsRequire runtime helper'
    );
    assert.match(
      code,
      /getDynamicModules\(\)/,
      'Emits getDynamicModules registry getter'
    );
    assert.match(
      code,
      /"\/fixtures\/alpha\.js": requireAlpha/,
      'Dynamic registry registers virtual path for alpha.js'
    );
    assert.match(
      code,
      /"\/fixtures\/beta\.js": requireBeta/,
      'Dynamic registry registers virtual path for beta.js'
    );
  }

  // Contract 3:
  // Executing a bundle configured with dynamicRequireTargets resolves dynamic
  // require calls for targeted modules at runtime, returning their exported values.
  {
    const code = await bundleEntry(entrySource, {
      dynamicRequireTargets: ['fixtures/*.js']
    });

    const mod = evaluateCjs(code);

    const alpha = mod.loadDynamic('alpha.js');
    assert.deepEqual(
      alpha,
      { name: 'alpha', value: 10 },
      'Dynamically requiring alpha.js returns its exports'
    );

    const beta = mod.loadDynamic('beta.js');
    assert.deepEqual(
      beta,
      { name: 'beta', value: 20 },
      'Dynamically requiring beta.js returns its exports'
    );
  }

  // Contract 4:
  // Executing a dynamic require call for a module not matched by
  // dynamicRequireTargets throws the commonjsRequire error.
  {
    const code = await bundleEntry(entrySource, {
      dynamicRequireTargets: ['fixtures/*.js']
    });

    const mod = evaluateCjs(code);

    assert.throws(
      () => mod.loadDynamic('unmatched.js'),
      {
        name: 'Error',
        message:
          'Could not dynamically require "./fixtures/unmatched.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'
      },
      'Unmatched dynamic require throws error even when dynamicRequireTargets is active'
    );
  }

  // Contract 5:
  // Dynamic require glob patterns support negation (prefixed with '!') to
  // selectively exclude specific files from the dynamic modules registry.
  {
    const code = await bundleEntry(entrySource, {
      dynamicRequireTargets: [
        'fixtures/*.js',
        '!fixtures/excluded.js'
      ]
    });

    assert.equal(
      code.includes('requireExcluded'),
      false,
      'Negated file must not be bundled or assigned a require helper'
    );
    assert.equal(
      code.includes('/fixtures/excluded.js'),
      false,
      'Dynamic registry must not contain entry for negated file'
    );

    const mod = evaluateCjs(code);

    // Included target works
    assert.deepEqual(
      mod.loadDynamic('alpha.js'),
      { name: 'alpha', value: 10 }
    );

    // Excluded target throws
    assert.throws(
      () => mod.loadDynamic('excluded.js'),
      /Could not dynamically require "\.\/fixtures\/excluded\.js"/,
      'Negated target throws error when dynamically required'
    );
  }

  // Contract 6:
  // When dynamicRequireTargets is combined with ignoreDynamicRequires: true,
  // unmatched dynamic require calls fall back to invoking the ambient require
  // function instead of throwing.
  {
    const code = await bundleEntry(entrySource, {
      dynamicRequireTargets: ['fixtures/*.js'],
      ignoreDynamicRequires: true
    });

    const ambientCalls = [];
    const ambientRequire = (specifier) => {
      ambientCalls.push(specifier);
      return { resolvedByAmbient: specifier };
    };

    const mod = evaluateCjs(code, ambientRequire);

    // Bundled target resolves internally without calling ambient require
    const alpha = mod.loadDynamic('alpha.js');
    assert.deepEqual(alpha, { name: 'alpha', value: 10 });
    assert.equal(ambientCalls.length, 0);

    // Unmatched target falls back to ambient require
    const fallback = mod.loadDynamic('external-dep.js');
    assert.deepEqual(fallback, { resolvedByAmbient: './fixtures/external-dep.js' });
    assert.deepEqual(ambientCalls, ['./fixtures/external-dep.js']);
  }

  console.log('All dynamicRequireTargets contract assertions passed successfully.');
}

runContracts().catch((err) => {
  console.error('Contract run failed:', err);
  process.exit(1);
});

Seeder d'origine

anonyme