Skip to content

Commit

Permalink
fix: exports types resolution in package.json (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
Macil committed Dec 19, 2022
1 parent ac3a0ae commit 7ff9996
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 28 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,22 +423,22 @@ This will create a package.json with these as exports:
"exports": {
".": {
"import": {
"default": "./esm/mod.js",
"types": "./types/mod.d.ts"
"types": "./types/mod.d.ts",
"default": "./esm/mod.js"
},
"require": {
"default": "./script/mod.js",
"types": "./types/mod.d.ts"
"types": "./types/mod.d.ts",
"default": "./script/mod.js"
}
},
"./internal": {
"import": {
"default": "./esm/internal.js",
"types": "./types/internal.d.ts"
"types": "./types/internal.d.ts",
"default": "./esm/internal.js"
},
"require": {
"default": "./script/internal.js",
"types": "./types/internal.d.ts"
"types": "./types/internal.d.ts",
"default": "./script/internal.js"
}
}
}
Expand Down
70 changes: 57 additions & 13 deletions lib/package_json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ Deno.test("single entrypoint", () => {
exports: {
".": {
import: {
default: "./esm/mod.js",
types: "./types/mod.d.ts",
default: "./esm/mod.js",
},
require: {
default: "./script/mod.js",
types: "./types/mod.d.ts",
default: "./script/mod.js",
},
},
},
Expand Down Expand Up @@ -105,12 +105,12 @@ Deno.test("single entrypoint", () => {
exports: {
".": {
import: {
default: "./esm/mod.js",
types: "./types/mod.d.ts",
default: "./esm/mod.js",
},
require: {
default: "./script/mod.js",
types: "./types/mod.d.ts",
default: "./script/mod.js",
},
},
},
Expand Down Expand Up @@ -139,8 +139,8 @@ Deno.test("single entrypoint", () => {
exports: {
".": {
import: {
default: "./esm/mod.js",
types: "./types/mod.d.ts",
default: "./esm/mod.js",
},
require: undefined,
},
Expand Down Expand Up @@ -232,6 +232,50 @@ Deno.test("single entrypoint", () => {
);
});

Deno.test("exports have default last", () => {
const props: GetPackageJsonOptions = {
transformOutput: {
main: {
files: [],
dependencies: [],
entryPoints: ["mod.ts"],
},
test: {
entryPoints: [],
files: [],
dependencies: [],
},
warnings: [],
},
entryPoints: [
{
name: ".",
path: "./mod.ts",
},
],
package: {
name: "package",
version: "0.1.0",
},
testEnabled: true,
includeEsModule: true,
includeScriptModule: true,
includeDeclarations: true,
includeTsLib: false,
shims: {
deno: "dev",
},
};

const result: any = getPackageJson(props);
assertEquals(Object.keys(result.exports), ["."]);
assertEquals(Object.keys(result.exports["."]), ["import", "require"]);

// "types" must always be first: https://github.com/denoland/dnt/issues/228
assertEquals(Object.keys(result.exports["."].import), ["types", "default"]);
assertEquals(Object.keys(result.exports["."].require), ["types", "default"]);
});

Deno.test("multiple entrypoints", () => {
const props: GetPackageJsonOptions = {
transformOutput: {
Expand Down Expand Up @@ -284,22 +328,22 @@ Deno.test("multiple entrypoints", () => {
exports: {
".": {
import: {
default: "./esm/mod.js",
types: "./types/mod.d.ts",
default: "./esm/mod.js",
},
require: {
default: "./script/mod.js",
types: "./types/mod.d.ts",
default: "./script/mod.js",
},
},
"./my-other-entrypoint.js": {
import: {
default: "./esm/other.js",
types: "./types/other.d.ts",
default: "./esm/other.js",
},
require: {
default: "./script/other.js",
types: "./types/other.d.ts",
default: "./script/other.js",
},
},
},
Expand Down Expand Up @@ -363,12 +407,12 @@ Deno.test("binary entrypoints", () => {
exports: {
".": {
import: {
default: "./esm/mod.js",
types: "./types/mod.d.ts",
default: "./esm/mod.js",
},
require: {
default: "./script/mod.js",
types: "./types/mod.d.ts",
default: "./script/mod.js",
},
},
},
Expand Down Expand Up @@ -445,12 +489,12 @@ Deno.test("peer dependencies", () => {
exports: {
".": {
import: {
default: "./esm/mod.js",
types: "./types/mod.d.ts",
default: "./esm/mod.js",
},
require: {
default: "./script/mod.js",
types: "./types/mod.d.ts",
default: "./script/mod.js",
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion lib/package_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ export function getPackageJson({
function getPathOrTypesObject(path: string) {
if (includeDeclarations) {
return {
default: path,
types:
(e.name === "." ? packageJsonObj.types : undefined) ??
`./types/${e.types}`,
default: path,
};
} else {
return path;
Expand Down
12 changes: 6 additions & 6 deletions tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ Deno.test("should build test project", async () => {
exports: {
".": {
import: {
default: "./esm/mod.js",
types: "./types/mod.d.ts",
default: "./esm/mod.js",
},
require: {
default: "./script/mod.js",
types: "./types/mod.d.ts",
default: "./script/mod.js",
},
},
},
Expand Down Expand Up @@ -453,12 +453,12 @@ Deno.test("should build with package mappings", async () => {
exports: {
".": {
import: {
default: "./esm/mod.js",
types: "./types/mod.d.ts",
default: "./esm/mod.js",
},
require: {
default: "./script/mod.js",
types: "./types/mod.d.ts",
default: "./script/mod.js",
},
},
},
Expand Down Expand Up @@ -520,12 +520,12 @@ Deno.test("should build with peer depependencies in mappings", async () => {
exports: {
".": {
import: {
default: "./esm/mod.js",
types: "./types/mod.d.ts",
default: "./esm/mod.js",
},
require: {
default: "./script/mod.js",
types: "./types/mod.d.ts",
default: "./script/mod.js",
},
},
},
Expand Down

0 comments on commit 7ff9996

Please sign in to comment.