build: the ES module build gets its own declarations
An ES module TypeScript consumer was given the CommonJS declarations in dist/types. With those,
`import alex2node from "alex2node"` compiled (module Node16) and then failed when Node loaded it: "The requested
module 'alex2node' does not provide an export named 'default'". The ES module build has named exports only.
tsconfig.esm.json now emits declarations next to dist/esm/*.js, under that directory's {"type": "module"}, and
"exports" selects per condition: import -> dist/esm/index.d.ts, require -> dist/types/index.d.ts. The default
import is now refused with TS1192; named imports are unchanged. "main", "module" and "types" are as before.
test/fixtures/types.mts holds the default import under @ts-expect-error, and a new test requires "types" before
"default" and a declaration for every built file. dist/: 25 files, 132,291 B -> 33 files, 155,647 B; the npm
tarball 25,125 B -> 25,837 B. npm test: 25 pass in 5.6 s.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
09b5da0992
commit
70072d39e3
13 changed files with 522 additions and 10 deletions
7
test/fixtures/types.mts
vendored
7
test/fixtures/types.mts
vendored
|
|
@ -1,7 +1,10 @@
|
|||
// Compiled, never run: the same declarations as seen from an ES module. Without a "types" condition in "exports" the
|
||||
// import resolves to dist/esm/index.js, which has no declarations next to it, and this file fails with TS7016.
|
||||
// Compiled, never run: an ES module TypeScript consumer. The "import" condition in "exports" gives it the
|
||||
// declarations next to the ES module build (dist/esm/*.d.ts), which has named exports only. Read through the
|
||||
// CommonJS declarations in dist/types, the default import below compiles and then fails when Node loads it.
|
||||
import { Alex2MQTT, DEFAULT_HOST, EndpointHealth } from "alex2node";
|
||||
import type { Device } from "alex2node";
|
||||
// @ts-expect-error the ES module build has no default export
|
||||
import alex2node from "alex2node";
|
||||
|
||||
const bridge = new Alex2MQTT("user", "password", "root", false, { host: DEFAULT_HOST });
|
||||
const devices: Device[] = bridge.getDevices();
|
||||
|
|
|
|||
|
|
@ -37,11 +37,23 @@ test("import(\"alex2node\") loads the ES module build, with the export names of
|
|||
|
||||
test("\"main\", \"module\" and \"types\" name the same files as \"exports\", for resolvers that do not read it", () => {
|
||||
const entry = pkg.exports["."];
|
||||
assert.deepEqual([pkg.main, pkg.module, pkg.types], [entry.require, entry.import, entry.types]);
|
||||
assert.deepEqual([pkg.main, pkg.module, pkg.types], [entry.require.default, entry.import.default, entry.require.types]);
|
||||
for (const file of [pkg.main, pkg.module, pkg.types]) assert.ok(fs.existsSync(path.join(ROOT, file)), file);
|
||||
assert.equal(require(path.join(ROOT, pkg.main)), require("alex2node"));
|
||||
});
|
||||
|
||||
test("each build has its own declarations, and \"exports\" names them before the code", () => {
|
||||
for (const [condition, dir] of [["require", "types"], ["import", "esm"]]) {
|
||||
const target = pkg.exports["."][condition];
|
||||
// TypeScript takes the first condition that matches, so "types" after "default" is never read
|
||||
assert.deepEqual(Object.keys(target), ["types", "default"], condition);
|
||||
assert.equal(target.types, `./dist/${dir}/index.d.ts`, condition);
|
||||
const declared = files(path.join(DIST, dir)).filter((f) => f.endsWith(".d.ts")).map((f) => f.replace(/\.d\.ts$/, ".js"));
|
||||
const built = files(path.join(ROOT, path.dirname(target.default))).filter((f) => f.endsWith(".js"));
|
||||
assert.deepEqual(declared, built, `a declaration for every file of the ${condition} build`);
|
||||
}
|
||||
});
|
||||
|
||||
test("the builds import Node built-ins and the declared dependencies, nothing else", () => {
|
||||
// "mqtt/lib/x" and "@scope/name/x" belong to the packages "mqtt" and "@scope/name"
|
||||
const packageOf = (specifier) => specifier.split("/").slice(0, specifier.startsWith("@") ? 2 : 1).join("/");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue