Repository navigation
Build: add exports map and Node-valid ESM to the devextreme package - #36508
Draft
GoodDayForSurf wants to merge 27 commits into
Draft
GoodDayForSurf wants to merge 27 commits into
GoodDayForSurf wants to merge 27 commits into
Conversation
- 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.
…feature/esm-exports-map
…ture/esm-exports-map
…ture/esm-exports-map
…ndev/DevExtreme into feature/esm-exports-map
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Title: Build: add exports maps and Node-valid ESM to the devextreme, devextreme-react, devextreme-vue and devextreme-themebuilder packages
Generates an
exportsmap for thedevextremenpm package and makesesm/valid Node ESM, whilecjs/keeps shipping. The same is done fordevextreme-reactanddevextreme-vue, anddevextreme-themebuildergets a staticexportsmap. 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 andbundles/*.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). Todayimport('devextreme/ui/button')fails in Node withERR_UNSUPPORTED_DIR_IMPORT, so SSR and plain Node code only work throughcjs/.esm/imports@babel/runtime/helpers/*instead ofhelpers/esm/*. These paths resolve to the ESM or CJS helper through@babel/runtime's own exports map, so tools that runesm/as CommonJS (Jest withtransformIgnorePatterns) can load them.package.jsongets anexportsmap (780 entries):types,module,import,requireanddefaultconditions;ui/button_types,data,core, …);"."(types only, from the existingtypesfield),./package.json,./dist/*,./scss/*;./bundles/*, plus./bundles/*.jsand./bundles/*.d.ts, so bothdevextreme/bundles/dx.allanddevextreme/bundles/dx.all.jsresolve;./localization/messages/*.jsonand./localization/messages/*, sodevextreme/localization/messages/rukeeps resolving toru.jsonas it does today throughrequire()and bundler extension lookup;./esm/*and./cjs/*, so existing deep imports keep working, plus one entry per folder module inesm/andcjs/(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.jsnext togrid_core/utils/), as it does today;.dtype imports (import type … from 'devextreme/ui/menu.d', used in our TestCafe tests): one./<folder>/*.dpattern per top-level folder and an explicit entry per root declaration (./common.d). There is no root./*.dpattern: Node warns (DEP0155) on everydevextreme/request while a pattern key starts with./*, and bundlers make such requests for prefix shares, such as'devextreme/'in Module Federation.core/versiongets declarations. Newjs/core/version.d.ts(version,fullVersion), soimport { version } from 'devextreme/core/version'type-checks without a hand-writtendeclare module.Apart from that,
cjs/, the per-folderpackage.jsonstubs (includingsideEffects) and all other files are byte-identical to a build ofmain. The only new files arecore/version.d.tsand therrulehelper module below.Public modules fixed
js/localization/ldml/number.jsre-exportedFormatterConfig, 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.jsimported the bare path'__internal/viz/core/renderers/animation', which doesn't resolve outside our build. It fails underrequire()today as well.import { RRule, RRuleSet } from 'rrule'failed there: rrule'smainis a UMD bundle whose named exports Node can't detect. The new__internal/scheduler/recurrence/rrule.tstakes them from a namespace import, named exports from rrule's ESM build in bundlers,defaultfrom its UMDmainin 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 indevextreme.npm/package.jsongets anexportsmap (React 118 entries, Vue 117):"."from the existingmain,moduleandtypesfields,./package.json, and one entry per module stub (./data-grid,./common/data,./template, …), with the same conditions asdevextreme;./esm/*and./cjs/*with and without.js,./cjs/*.d.ts, and one entry per folder module. Declarations ship only incjs/, so theesm/entries take theirtypesfromcjs/too.devextremein Node ESM. Today Node always loads the wrappers fromcjs/, whichrequire()scjs/ofdevextreme. Withdevextreme'simportcondition pointing toesm/, app code that importsdevextremedirectly would get a second copy: aDataSourcefromdevextreme/common/datawouldn'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 onesm/as well, both use one copy.devextreme-themebuilder
dist/package.jsongets a staticexportsmap:"."(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 everythingdevextreme-cliloads by name (devextreme-themebuilder/package.json,…/modules/builder,…/modules/base-parameters); SCSS is read by file path, whichexportsdoesn't affect.How did you achieve this?
devextreme
build/babel-plugin-add-import-extensions.js, added to theesmconfig inbuild/transpile-config.js. It resolves each relative import against the source tree, the way TypeScript does, so dotted names such asui.errorsoremitter.feedbackget.js. A file wins over a directory with the same name.__internalfiles compiled intoartifacts/dist_tsare mapped back tojs/. An import that doesn't resolve fails the build.create-dual-mode-manifest: the side-effect finder appended.jsto every import, so fully specified imports would have produced emptysideEffectslists.state-manager-optimize: the ESM re-export it writes is now./prod/index.js.build:dev,build:qunit-esm,pnpm run dev) use a separateesmQunitBabel config:esmwithout 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.jsimports 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-plugingenerate-exports-mapexecutor, run bybuild:npm:exports-mapafterbuild:npm:root-package-jsonin everydevextremebuild:npmconfiguration, internal ones included, and bynpm:exports-mapafter the finalnpm:prepare-modulesin the React and Vuebuildandpacktargets. It fails if an entry points to a missing file. Options:declarationImports: the.dtype import entries;directoryIndexes: the folder module entries for deep-import folders such asesmandcjs;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: newfullySpecifiedImportsoption, enabled for the React and Vuebuild:esm. A TypeScriptaftertransformer adds.jsor/index.jsto relative static imports, re-exports and dynamicimport()calls, keeps the source quotes, and fails the build on an import that doesn't resolve. WithresolvePaths, it also turnspathsaliases of project sources into fully specified relative imports before the emit, sincetsc-aliaswould emit them without an extension. It runs in normal and watch builds.prepare-submodules: treats a fully specified import inesm/index.js(./button.js) as the same module as./button, so the module stubs keep their names.devextreme-themebuilder
src/tools/copy-resources.tswrites theexportsmap todist/package.json. The sourcepackage.jsonstays without it: the source folder has nomodules/builder.jsto export.Decisions worth reviewing
modulecomes first in each entry. With onlyimport/require(as in Build: research for generating exports for ESM mode #34182), an app that both imports andrequire()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. Withmodulefirst, webpack resolves exactly as it does today through the stub'smodulefield, and esbuild matches it. Node and Jest ignoremodule, sorequire()still getscjs/and the class itself.require('devextreme/ui/button')now gets{ default }. That's what webpack users already get today.devextreme-aspnet-data-nojqueryalready unwraps.defaultfor this reason."type": "module"inesm/. Node loads it through syntax detection (Node 20.19+, 22.7+) without a warning insidenode_modules. Earlier Node 20 releases can't importesm/, 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.devextreme's conditions rather than keeping Node oncjs/: see "One copy ofdevextremein Node ESM" above.How can we verify these changes?
Results of the first iteration, compared with a build of
main(Node 24.16.0):mainpackage.jsondiffers byexports.cjs/, stubs and other files identical.esm/differs in import paths (2,011 files) plus the module fixes. (The later changes addcore/version.d.ts, itstypingsin thecore/versionstub, and therrulehelper module.)import()of all 505 public modules in Nodeaspnet(they fail underrequire()too), andjquery/knockout/olintegrations without those packages installed.require()of all 505 public modulesviz/core/renderers/animationnow loads.e2e/bundlers(webpack ESM + CJS, Rollup, Vite, Parcel, Browserify)e2e/compilation-cases(moduleResolution: bundler)devextreme-angular,devextreme-react,devextreme-vuebuilds; React and Vue testsbuild-vendor-bundles(esbuild)mainmaintoo.transformIgnorePatterns,jest.mock), Vitest, Noderequire/import, webpack, esbuild, Browserify, TypeScriptbundler/node10mainexcept: Nodeimportand webpack imports from.mjs/"type":"module"app files now pass; esbuildrequire()-only code gets{ default }. Apps mixingimportandrequire()still get one copy in webpack and esbuild.Results for the later changes (Node 24.17.0):
devextremeverify:public-modules,verify:ts-modules,verify:ts-jquery,verify:ts-bundledevextremedeep paths:bundles/dx.allanddx.all.js,cjs/__internal/ui/button(folder),grid_core/utils(file next to a folder),esm/ui/button.js,localization/messages/ruandru.json,ui/button.d(TypeScript)exports'devextreme/'prefix share (@module-federation/enhanced, Rsbuild,@module-federation/vite,nextjs-mf)DEP0155warningimport()andrequire()ofdevextreme/ui/schedulerwork 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/303import()of every module stub entry loads (React 104, Vue 101);require()getscjs/;DataSourcefromdevextreme/common/dataand from the wrapper is the same classcjs/button,esm/button.js,cjs/button.d.ts,cjs/common,esm/common(folders),cjs/common/data(file next to a folder)cjs/under TypeScriptbundlerandnode16bundler,node16,node10(React)esm/ofdevextreme-vueanddevextreme, nocjs/devextreme-themebuilder:…/package.json,…/modules/builder,…/modules/base-parameters(asdevextreme-cliloads them), metadata with and without.js, SCSS by full name@module-federation/enhanced, Rsbuild, Vite, Next.js, Angular MF, Native Federation) on packed builds ofdevextreme,devextreme-react,devextreme-angularfrom this branchdevextremenx-infra-plugin:generate-exports-map,build-typescript,prepare-submodulesNot run locally for the later changes: TestCafe, Playwright, QUnit, builds of
apps/demos,apps/react,apps/vue,apps/react-storybook,e2e/wrappers, and thedevextreme-internalpackage.Known limitations
devextreme/bin/*anddevextreme/license/*(thebincommands and running them by file path still work), explicit.d.tspaths and the per-folderpackage.jsonstubs (/// <reference path>still works), and internal wrapper modules without a stub, such asdevextreme-react/core/helpers, which didn't resolve before either (their code stays reachable through…/cjs/core/helpers). Nothing in this repository imports these paths.importandrequire()in Node loads two copies (esm/andcjs/). Bundlers resolve both toesm/throughmodule. Before this PR Node couldn't import the deep paths at all..d.tsfiles are CommonJS-format for TypeScript (node16/nodenextwith an ESM importer), so a default import such asimport Button from 'devextreme/ui/button'doesn't type-check there; named imports do, andbundler,node10and CommonJS importers are unaffected. This is unchanged from today. Indevextreme-reactanddevextreme-vuethe declarations ship incjs/and theimportcondition now resolves toesm/, so the same applies to them.