Skip to content

Build: add exports map and Node-valid ESM to the devextreme package - #36508

Draft
GoodDayForSurf wants to merge 27 commits into
mainfrom
feature/esm-package-format
Draft

GoodDayForSurf wants to merge 27 commits into
mainfrom
feature/esm-package-format

Conversation

@GoodDayForSurf

Copy link
Copy Markdown
Contributor

Title: Build: add exports maps and Node-valid ESM to the devextreme, devextreme-react, devextreme-vue and devextreme-themebuilder packages


Generates an exports map for the devextreme npm package and makes esm/ valid Node ESM, while cjs/ keeps shipping. The same is done for devextreme-react and devextreme-vue, and devextreme-themebuilder gets a static exports map. Supersedes the research in #34182: its extension rule skipped file names that contain a dot (495 imports), and its exports map left out type-only modules, the package root and bundles/*.

What does the PR change?

devextreme

Package output

  • esm/ loads in Node. Every relative import is fully specified (./button → ./button.js, ./grids → ./grids/index.js). Today import('devextreme/ui/button') fails in Node with ERR_UNSUPPORTED_DIR_IMPORT, so SSR and plain Node code only work through cjs/.
  • esm/ imports @babel/runtime/helpers/* instead of helpers/esm/*. These paths resolve to the ESM or CJS helper through @babel/runtime's own exports map, so tools that run esm/ as CommonJS (Jest with transformIgnorePatterns) can load them.
  • Root package.json gets an exports map (780 entries):
    • one entry per module stub, with types, module, import, require and default conditions;
    • every type-only module, read from the declarations (ui/button_types, data, core, …);
    • "." (types only, from the existing types field), ./package.json, ./dist/*, ./scss/*;
    • ./bundles/*, plus ./bundles/*.js and ./bundles/*.d.ts, so both devextreme/bundles/dx.all and devextreme/bundles/dx.all.js resolve;
    • ./localization/messages/*.json and ./localization/messages/*, so devextreme/localization/messages/ru keeps resolving to ru.json as it does today through require() and bundler extension lookup;
    • ./esm/* and ./cjs/*, so existing deep imports keep working, plus one entry per folder module in esm/ and cjs/ (138 entries: devextreme/cjs/__internal/ui/button → cjs/__internal/ui/button/index.js). A file wins over a folder with the same name (grid_core/utils.js next to grid_core/utils/), as it does today;
    • .d type imports (import type … from 'devextreme/ui/menu.d', used in our TestCafe tests): one ./<folder>/*.d pattern per top-level folder and an explicit entry per root declaration (./common.d). There is no root ./*.d pattern: Node warns (DEP0155) on every devextreme/ request while a pattern key starts with ./*, and bundlers make such requests for prefix shares, such as 'devextreme/' in Module Federation.
  • core/version gets declarations. New js/core/version.d.ts (version, fullVersion), so import { version } from 'devextreme/core/version' type-checks without a hand-written declare module.

Apart from that, cjs/, the per-folder package.json stubs (including sideEffects) and all other files are byte-identical to a build of main. The only new files are core/version.d.ts and the rrule helper module below.

Public modules fixed

  • js/localization/ldml/number.js re-exported FormatterConfig, a TypeScript interface. Bundlers only warn about it; Node ESM fails with "does not provide an export named". Nothing imports it from this path.
  • js/viz/core/renderers/animation.js imported the bare path '__internal/viz/core/renderers/animation', which doesn't resolve outside our build. It fails under require() today as well.
  • Scheduler loads under native Node ESM. import { RRule, RRuleSet } from 'rrule' failed there: rrule's main is a UMD bundle whose named exports Node can't detect. The new __internal/scheduler/recurrence/rrule.ts takes them from a namespace import, named exports from rrule's ESM build in bundlers, default from its UMD main in Node. webpack and esbuild bundle it without warnings and use rrule's ESM build, as before.

devextreme-react and devextreme-vue

  • esm/ loads in Node. Relative imports are fully specified, as in devextreme.
  • npm/package.json gets an exports map (React 118 entries, Vue 117):
    • "." from the existing main, module and types fields, ./package.json, and one entry per module stub (./data-grid, ./common/data, ./template, …), with the same conditions as devextreme;
    • ./esm/* and ./cjs/* with and without .js, ./cjs/*.d.ts, and one entry per folder module. Declarations ship only in cjs/, so the esm/ entries take their types from cjs/ too.
  • One copy of devextreme in Node ESM. Today Node always loads the wrappers from cjs/, which require()s cjs/ of devextreme. With devextreme's import condition pointing to esm/, app code that imports devextreme directly would get a second copy: a DataSource from devextreme/common/data wouldn't be recognized by a grid from the wrapper (it creates an empty one), and config, localization and the license key would land in another copy. With the wrappers on esm/ as well, both use one copy.

devextreme-themebuilder

  • dist/package.json gets a static exports map: "." (the builder), ./package.json, ./modules/* with and without .js, the metadata modules (./data/metadata/*, ./data/bootstrap-metadata/*, ./data/migration-metadata/*) with and without .js, and ./data/* for SCSS and other data files by full name. It covers everything devextreme-cli loads by name (devextreme-themebuilder/package.json, …/modules/builder, …/modules/base-parameters); SCSS is read by file path, which exports doesn't affect.

How did you achieve this?

devextreme

  • build/babel-plugin-add-import-extensions.js, added to the esm config in build/transpile-config.js. It resolves each relative import against the source tree, the way TypeScript does, so dotted names such as ui.errors or emitter.feedback get .js. A file wins over a directory with the same name. __internal files compiled into artifacts/dist_ts are mapped back to js/. An import that doesn't resolve fails the build.
  • create-dual-mode-manifest: the side-effect finder appended .js to every import, so fully specified imports would have produced empty sideEffects lists.
  • state-manager-optimize: the ESM re-export it writes is now ./prod/index.js.
  • QUnit artifacts (build:dev, build:qunit-esm, pnpm run dev) use a separate esmQunit Babel config: esm without the extensions plugin. The QUnit runner's import map gives tests extensionless URLs, and a test and the library must import a module by the same URL to share one instance. With .js imports in the library, stubs and spies set by tests didn't reach it, and 13 QUnit jobs failed on the first CI run. The import paths are the only difference, so QUnit still covers the same code.

nx-infra-plugin

  • New generate-exports-map executor, run by build:npm:exports-map after build:npm:root-package-json in every devextreme build:npm configuration, internal ones included, and by npm:exports-map after the final npm:prepare-modules in the React and Vue build and pack targets. It fails if an entry points to a missing file. Options:
    • declarationImports: the .d type import entries;
    • directoryIndexes: the folder module entries for deep-import folders such as esm and cjs;
    • directoryIndexTypesFrom: the declaration folder for a deep-import folder without its own ({ "esm": "cjs" } in the wrappers);
    • extraExports: entries appended as they are.
  • build-typescript: new fullySpecifiedImports option, enabled for the React and Vue build:esm. A TypeScript after transformer adds .js or /index.js to relative static imports, re-exports and dynamic import() calls, keeps the source quotes, and fails the build on an import that doesn't resolve. With resolvePaths, it also turns paths aliases of project sources into fully specified relative imports before the emit, since tsc-alias would emit them without an extension. It runs in normal and watch builds.
  • prepare-submodules: treats a fully specified import in esm/index.js (./button.js) as the same module as ./button, so the module stubs keep their names.

devextreme-themebuilder

  • src/tools/copy-resources.ts writes the exports map to dist/package.json. The source package.json stays without it: the source folder has no modules/builder.js to export.

Decisions worth reviewing

  • module comes first in each entry. With only import/require (as in Build: research for generating exports for ESM mode #34182), an app that both imports and require()s DevExtreme loads two copies in webpack and esbuild. Config, localization and the license key then land in a copy the components don't use. With module first, webpack resolves exactly as it does today through the stub's module field, and esbuild matches it. Node and Jest ignore module, so require() still gets cjs/ and the class itself.
    • Trade-off: esbuild app code that only uses require('devextreme/ui/button') now gets { default }. That's what webpack users already get today. devextreme-aspnet-data-nojquery already unwraps .default for this reason.
  • No "type": "module" in esm/. Node loads it through syntax detection (Node 20.19+, 22.7+) without a warning inside node_modules. Earlier Node 20 releases can't import esm/, but they couldn't import the deep paths before this PR either. Adding the field made webpack treat our files as strict ESM. That changed the bundled output for modules that default-import CommonJS/UMD libraries (Quill, Gantt, Diagram) and adds runtime risk with no benefit for Node.
  • The wrappers follow devextreme's conditions rather than keeping Node on cjs/: see "One copy of devextreme in Node ESM" above.

How can we verify these changes?

pnpm nx build devextreme
pnpm exec jest --selectProjects node-tests --testPathPatterns babel-plugin-add-import-extensions   # from packages/devextreme
pnpm --workspace-root nx test devextreme-nx-infra-plugin
pnpm --workspace-root nx pack devextreme-react
pnpm --workspace-root nx pack devextreme-vue
pnpm --workspace-root nx build devextreme-themebuilder
cd e2e/compilation-cases && pnpm test
pnpm nx test devextreme-modules-test

Results of the first iteration, compared with a build of main (Node 24.16.0):

Check Result
Package diff against main Root package.json differs by exports. cjs/, stubs and other files identical. esm/ differs in import paths (2,011 files) plus the module fixes. (The later changes add core/version.d.ts, its typings in the core/version stub, and the rrule helper module.)
import() of all 505 public modules in Node 488 load (0 today). The rest: Scheduler (now fixed, see below), the Globalize modules and aspnet (they fail under require() too), and jquery/knockout/ol integrations without those packages installed.
require() of all 505 public modules No new failures; viz/core/renderers/animation now loads.
e2e/bundlers (webpack ESM + CJS, Rollup, Vite, Parcel, Browserify) Pass
e2e/compilation-cases (moduleResolution: bundler) Pass
devextreme-angular, devextreme-react, devextreme-vue builds; React and Vue tests Pass (React 254, Vue 175)
Demos: build-vendor-bundles (esbuild) Pass; drops the same three type-only specifiers as on main
QUnit (headless Chrome, local runner) All 161 suites that failed in the first CI run now pass. Two tests (a DataGrid adaptive-columns width check and a Form resize-count check) fail locally on main too.
Consumer matrix: Jest (default config, transformIgnorePatterns, jest.mock), Vitest, Node require/import, webpack, esbuild, Browserify, TypeScript bundler/node10 Same as main except: Node import and webpack imports from .mjs/"type":"module" app files now pass; esbuild require()-only code gets { default }. Apps mixing import and require() still get one copy in webpack and esbuild.

Results for the later changes (Node 24.17.0):

Check Result
devextreme verify:public-modules, verify:ts-modules, verify:ts-jquery, verify:ts-bundle Pass
devextreme deep paths: bundles/dx.all and dx.all.js, cjs/__internal/ui/button (folder), grid_core/utils (file next to a folder), esm/ui/button.js, localization/messages/ru and ru.json, ui/button.d (TypeScript) Resolve as before exports
Module Federation builds with a 'devextreme/' prefix share (@module-federation/enhanced, Rsbuild, @module-federation/vite, nextjs-mf) No DEP0155 warning
Scheduler import() and require() of devextreme/ui/scheduler work in Node; webpack (target: web) bundles it with no warnings; webpack and esbuild bundle the rrule helper with no warnings, using rrule's ESM build; recurrence Jest tests 303/303
React and Vue in Node import() of every module stub entry loads (React 104, Vue 101); require() gets cjs/; DataSource from devextreme/common/data and from the wrapper is the same class
React and Vue deep paths: cjs/button, esm/button.js, cjs/button.d.ts, cjs/common, esm/common (folders), cjs/common/data (file next to a folder) Resolve, with types from cjs/ under TypeScript bundler and node16
React and Vue types under TypeScript bundler, node16, node10 (React) Pass
esbuild bundle of a Vue app Only esm/ of devextreme-vue and devextreme, no cjs/
devextreme-themebuilder: …/package.json, …/modules/builder, …/modules/base-parameters (as devextreme-cli loads them), metadata with and without .js, SCSS by full name Resolve and load
Seven Module Federation sample apps (webpack, @module-federation/enhanced, Rsbuild, Vite, Next.js, Angular MF, Native Federation) on packed builds of devextreme, devextreme-react, devextreme-angular from this branch Build and render; host and remote grids share one devextreme
nx-infra-plugin: generate-exports-map, build-typescript, prepare-submodules 10/10, 13/13, 9/9

Not run locally for the later changes: TestCafe, Playwright, QUnit, builds of apps/demos, apps/react, apps/vue, apps/react-storybook, e2e/wrappers, and the devextreme-internal package.

Known limitations

  • Paths outside the maps are closed. Every path that worked before and is used in practice stays open (see above). What no longer resolves as a module: devextreme/bin/* and devextreme/license/* (the bin commands and running them by file path still work), explicit .d.ts paths and the per-folder package.json stubs (/// <reference path> still works), and internal wrapper modules without a stub, such as devextreme-react/core/helpers, which didn't resolve before either (their code stays reachable through …/cjs/core/helpers). Nothing in this repository imports these paths.
  • Mixing import and require() in Node loads two copies (esm/ and cjs/). Bundlers resolve both to esm/ through module. Before this PR Node couldn't import the deep paths at all.
  • The .d.ts files are CommonJS-format for TypeScript (node16/nodenext with an ESM importer), so a default import such as import Button from 'devextreme/ui/button' doesn't type-check there; named imports do, and bundler, node10 and CommonJS importers are unaffected. This is unchanged from today. In devextreme-react and devextreme-vue the declarations ship in cjs/ and the import condition now resolves to esm/, so the same applies to them.

ajivanyandev and others added 26 commits November 10, 2026 15:25
- esm/: relative imports are fully specified by a new Babel plugin
  (build/babel-plugin-add-import-extensions.js), so Node's ESM loader and
  strict-ESM bundler modes can load the package. It resolves against the
  sources, so dotted file names such as ui.errors work, and it fails the build
  on an import it cannot resolve.
- esm/: import @babel/runtime/helpers/* instead of helpers/esm/* (drop
  useESModules). The helper resolves to ESM or CJS through @babel/runtime's
  exports map, so Jest can load it when it transforms esm/.
- package.json: new generate-exports-map executor and build:npm:exports-map
  target. One entry per module stub with types, module, import, require and
  default conditions; `module` first keeps import and require() on one copy
  in bundlers. Type-only modules come from the declarations. Assets,
  bundles/*, esm/* and cjs/* deep paths and *.d type imports are kept.
- create-dual-mode-manifest: follow fully specified imports when collecting
  sideEffects.
- state-manager-optimize: fully specified ESM re-export.
- js/localization/ldml/number.js: drop a type-only re-export that fails in
  Node ESM.
- js/viz/core/renderers/animation.js: relative import instead of a bare
  '__internal/...' path that never resolved outside the build.

cjs/, the per-folder stubs and the rest of the npm package are unchanged.
The QUnit runner's import map gives tests extensionless URLs. Library code
importing `./errors.js` and a test importing `ui/widget/ui.errors` loaded two
instances of the same module, so stubs and spies set by tests did not reach
the library.

The `qunit` configurations of the ESM transpile targets now use a separate
`esmQunit` Babel config: the npm `esm` config without the import-extensions
plugin. The npm package output is unchanged.
@GoodDayForSurf GoodDayForSurf self-assigned this Nov 23, 2026
@github-actions github-actions Bot added the .d.ts label Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants