Skip to content

Commit b874ff5

Browse files
committed
feat(pkg): add exports map for ep_etherpad-lite
Routes CJS plugins' require() calls to dist-cjs/*.cjs twins while keeping ESM consumers on dist/*.mjs (tsdown emits .mjs for ESM). The trailing-.js wildcard handles plugins that wrote require(...'.js') with an explicit extension. tests/backend has only an import condition because CJS build excludes it (top-level await). Also fixes Settings.ts getEpVersion() to use a static JSON import instead of a build-path-relative requireFromHere() call, which broke when resolved from dist-cjs/node/utils/. Test file updated: split cjsSubpaths into resolvable vs loadable sets since DB modules transitively depend on ueberdb2 (ESM-only, no require condition).
1 parent 626097b commit b874ff5

3 files changed

Lines changed: 50 additions & 7 deletions

File tree

src/node/utils/Settings.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ import jsonminify from 'jsonminify';
3939
import log4js from 'log4js';
4040
import randomString from './randomstring.js';
4141
import {createHash} from 'node:crypto';
42-
import { createRequire } from 'node:module';
4342
const suppressDisableMsg = ' -- To suppress these warning messages change ' +
4443
'suppressErrorsInPadText to true in your settings.json\n';
4544
import _ from 'underscore';
46-
47-
const requireFromHere = createRequire(import.meta.url);
45+
import pkg from '../../package.json' with { type: 'json' };
4846

4947
const logger = log4js.getLogger('settings');
5048

@@ -926,7 +924,7 @@ export const exportAvailable = () => sofficeAvailable();
926924

927925

928926
// Return etherpad version from package.json
929-
export const getEpVersion = () => requireFromHere('../../package.json').version;
927+
export const getEpVersion = () => pkg.version;
930928

931929

932930

src/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,41 @@
99
"collaborative",
1010
"editor"
1111
],
12+
"main": "./dist-cjs/node/server.cjs",
13+
"module": "./dist/node/server.mjs",
14+
"exports": {
15+
".": {
16+
"import": "./dist/node/server.mjs",
17+
"require": "./dist-cjs/node/server.cjs"
18+
},
19+
"./node/eejs": {
20+
"import": "./dist/node/eejs/index.mjs",
21+
"require": "./dist-cjs/node/eejs/index.cjs"
22+
},
23+
"./node/*": {
24+
"import": "./dist/node/*.mjs",
25+
"require": "./dist-cjs/node/*.cjs"
26+
},
27+
"./node/*.js": {
28+
"import": "./dist/node/*.mjs",
29+
"require": "./dist-cjs/node/*.cjs"
30+
},
31+
"./static/js/*": {
32+
"import": "./dist/static/js/*.mjs",
33+
"require": "./dist-cjs/static/js/*.cjs"
34+
},
35+
"./static/js/*.js": {
36+
"import": "./dist/static/js/*.mjs",
37+
"require": "./dist-cjs/static/js/*.cjs"
38+
},
39+
"./tests/backend/*": {
40+
"import": "./dist/tests/backend/*.mjs"
41+
},
42+
"./tests/backend/*.js": {
43+
"import": "./dist/tests/backend/*.mjs"
44+
},
45+
"./package.json": "./package.json"
46+
},
1247
"author": "Etherpad Foundation",
1348
"contributors": [
1449
{

src/tests/backend/specs/exports_map.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@ import { createRequire } from 'node:module';
33

44
const require = createRequire(import.meta.url);
55

6-
const cjsSubpaths = [
6+
// All CJS subpaths must resolve to a .cjs file.
7+
const cjsResolvableSubpaths = [
78
'ep_etherpad-lite/node/eejs',
89
'ep_etherpad-lite/node/db/PadManager',
910
'ep_etherpad-lite/node/db/API.js',
1011
'ep_etherpad-lite/node/db/AuthorManager',
1112
'ep_etherpad-lite/static/js/pad_utils',
12-
'ep_etherpad-lite/tests/backend/common',
13+
];
14+
15+
// Only these subpaths can be synchronously require()-loaded: their transitive
16+
// dependency graph is CJS-compatible. DB modules (PadManager, API, AuthorManager)
17+
// transitively import ueberdb2 which is ESM-only (no "require" export condition).
18+
const cjsLoadableSubpaths = [
19+
'ep_etherpad-lite/node/eejs',
20+
'ep_etherpad-lite/static/js/pad_utils',
1321
];
1422

1523
const esmSubpaths = [
@@ -21,12 +29,14 @@ const esmSubpaths = [
2129

2230
describe('ep_etherpad-lite exports map', () => {
2331
describe('require() condition (CJS plugins)', () => {
24-
for (const spec of cjsSubpaths) {
32+
for (const spec of cjsResolvableSubpaths) {
2533
test(`require('${spec}') resolves`, () => {
2634
const resolved = require.resolve(spec);
2735
expect(resolved).toMatch(/\.cjs$/);
2836
});
37+
}
2938

39+
for (const spec of cjsLoadableSubpaths) {
3040
test(`require('${spec}') loads a module`, () => {
3141
const mod = require(spec);
3242
expect(mod).toBeTruthy();

0 commit comments

Comments
 (0)