[bug]: build ignores exports file names and package.json#type, breaking "type": "module" plugins

Aperta
#242 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 6 giorni

@unrevised6419 ci sta già lavorando.

Dal 2/10/2026.

  • #243 di @unrevised6419 — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
node.js, typescript, vite

Direzione di ricerca

Start in src/cli/commands/utils/build/vite-config.ts and run strapi-plugin build with the package.json exports and type settings from the reproduction. Done means import and require outputs use the exact exports paths, including .js, .cjs, or .mjs, and validation accepts ESM-only or CJS-only plugins.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

What version of @strapi/sdk-plugin are you using?
  • @strapi/sdk-plugin 6.1.1 (also current main)
  • Node.js 24.21.0
What's Wrong?

strapi-plugin build ignores the file names in the package.json exports map and the package's "type". It reads the import / require paths only to pick the output directory, then always writes index.mjs (ESM) and index.js (CommonJS):

// src/cli/commands/utils/build/vite-config.ts
const outputPath = bundle.output.cjs ?? bundle.output.esm;
const outDir = path.dirname(outputPath);
// ...
fileName: (format) => (format === 'es' ? 'index.mjs' : 'index.js'),

This makes "type": "module" plugins impossible to build correctly:

  1. Dual build: CommonJS always lands in index.js. In a "type": "module" package Node loads that file as ESM, so Strapi fails at boot with exports is not defined in ES module scope. Pointing require at ./dist/server/index.cjs doesn't help: the build still writes index.js, and index.cjs never exists.
  2. ESM-only build: a plugin with only import / default and no require is rejected before building, because ./strapi-admin and ./strapi-server must declare both import and require:
    exports["./strapi-server"].require is a required field
    
    If that check were removed, the ES output would still be written to index.mjs, not to the import path.
To Reproduce
  1. Create a plugin with "type": "module" and:
    "exports": {
      "./strapi-server": {
        "source": "./server/src/index.js",
        "import": "./dist/server/index.js",
        "require": "./dist/server/index.cjs",
        "default": "./dist/server/index.cjs"
      },
      "./package.json": "./package.json"
    }
    
  2. Run strapi-plugin build.
  3. dist/server contains index.js (CommonJS) and index.mjs. Neither file matches the exports map, and index.cjs is missing.
  4. Load the output: node --input-type=module -e "import('./dist/server/index.js')"
    exports is not defined in ES module scope
    This file is being treated as an ES module because it has a '.js' file extension and '…/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    

For the ESM-only case, drop require and set "default": "./dist/server/index.js". Step 2 then fails with the validation error above.

Expected Behaviour
  • Each output file is written to the exact path its exports condition points at (import for ESM, require for CommonJS). The .cjs / .js / .mjs extension the author picked for their "type" is then respected.
  • ./strapi-admin and ./strapi-server need at least one of import or require, not both, so ESM-only (and CJS-only) plugins can be built.
  • Optionally, build warns when an extension makes Node load the file in the wrong format, e.g. require → .js in a "type": "module" package.

init writes "type": "commonjs" with index.mjs / index.js, which stays correct under this behaviour.

Related: #241 (type declarations have the same one-file-for-both-formats problem).

I have a fix with tests in a fork and can open a PR.

Lingua principale
TypeScript
Stelle
24
Fork
13
Merge medio
5g 3h
PR unite (30g)
5

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di strapi/sdk-plugin

Tutte le issue di strapi/sdk-plugin

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.