Skip to content

Commit

Permalink
Fix declaration maps for .d.mts in build
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Apr 28, 2023
1 parent 34ca570 commit f9011d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/cli/src/build/__tests__/build.ts
Expand Up @@ -1054,9 +1054,10 @@ test("correct default export using mjs and dmts proxies", async () => {
} from "./declarations/src/index.js";
import ns from "./declarations/src/index.js";
export default ns.default;
//# sourceMappingURL=pkg-a.cjs.d.mts.map
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ packages/pkg-a/dist/pkg-a.cjs.d.mts.map ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
{"version":3,"file":"pkg-a.cjs.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA"}
{"version":3,"file":"pkg-a.cjs.d.mts","sourceRoot":"","sources":["./declarations/src/index.d.ts"],"names":[],"mappings":"AAAA"}
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ packages/pkg-a/dist/pkg-a.cjs.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export * from "./declarations/src/index";
Expand Down Expand Up @@ -1229,9 +1230,10 @@ test("importing a package via dynamic import from another package provides the r
export {
thing
} from "./declarations/src/index.js";
//# sourceMappingURL=pkg-b.cjs.d.mts.map
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ packages/pkg-b/dist/pkg-b.cjs.d.mts.map ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
{"version":3,"file":"pkg-b.cjs.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA"}
{"version":3,"file":"pkg-b.cjs.d.mts","sourceRoot":"","sources":["./declarations/src/index.d.ts"],"names":[],"mappings":"AAAA"}
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ packages/pkg-b/dist/pkg-b.cjs.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export * from "./declarations/src/index";
Expand Down Expand Up @@ -1488,6 +1490,7 @@ test("importing another package via dynamic import and exporting something that
} from "./declarations/src/index.js";
import ns from "./declarations/src/index.js";
export default ns.default;
//# sourceMappingURL=pkg-a.cjs.d.mts.map
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ packages/pkg-a/dist/pkg-a.cjs.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export * from "./declarations/src/index";
Expand All @@ -1503,6 +1506,7 @@ test("importing another package via dynamic import and exporting something that
pkgADefault,
pkgAThing
} from "./declarations/src/index.js";
//# sourceMappingURL=pkg-b.cjs.d.mts.map
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ packages/pkg-b/dist/pkg-b.cjs.d.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export * from "./declarations/src/index";
Expand Down
19 changes: 17 additions & 2 deletions packages/cli/src/rollup-plugins/typescript-declarations/index.ts
Expand Up @@ -156,12 +156,27 @@ export default function typescriptDeclarations(pkg: Package): Plugin {
if (
pkg.exportsFieldConfig()?.importDefaultExport === "unwrapped-default"
) {
const dmtsFilename = dtsFileName.replace(/\.d\.ts$/, ".d.mts");
const basedmtsFilename = baseDtsFilename.replace(
/\.d\.ts$/,
".d.mts"
);
this.emitFile({
type: "asset",
fileName: dtsFileName.replace(/\.d\.ts$/, ".d.mts"),
fileName: dmtsFilename,
// even though we are emitting a declaration file it's currently safe to reuse the `.mjs` template
// it only has reexports, no runtime expressions and even no types in it
source: mjsTemplate(file.exports, `${relativeToSource}.js`),
source:
mjsTemplate(file.exports, `${relativeToSource}.js`) +
`//# sourceMappingURL=${basedmtsFilename}.map\n`,
});
this.emitFile({
type: "asset",
fileName: `${dmtsFilename}.map`,
source: tsReexportDeclMap(
basedmtsFilename,
`${relativeToSource}.d.ts`
),
});
}
}
Expand Down

0 comments on commit f9011d4

Please sign in to comment.