Skip to content

Commit 2e18f50

Browse files
committed
fix: please webpack module resolution
1 parent dd77d39 commit 2e18f50

File tree

9 files changed

+185
-108
lines changed

9 files changed

+185
-108
lines changed

.changeset/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
"baseBranch": "master",
88
"updateInternalDependencies": "patch",
99
"ignore": []
10-
}
10+
}

src/commands/bootstrap.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as fse from "fs-extra";
33
import { createCommand } from "../command";
44

55
/** The default bob fields that should be within a package.json */
6-
const presetFields = {
6+
export const presetFields = Object.freeze({
77
main: "dist/index.js",
88
module: "dist/index.mjs",
99
typings: "dist/index.d.ts",
@@ -13,22 +13,32 @@ const presetFields = {
1313
exports: {
1414
".": {
1515
require: {
16-
default: "./dist/index.js",
1716
types: "./dist/index.d.ts",
17+
default: "./dist/index.js",
1818
},
1919
import: {
20+
types: "./dist/index.d.ts",
2021
default: "./dist/index.mjs",
22+
},
23+
/** without this default (THAT MUST BE LAST!!!) webpack will have a midlife crisis. */
24+
default: {
2125
types: "./dist/index.d.ts",
26+
default: "./dist/index.js",
2227
},
2328
},
2429
"./*": {
2530
require: {
26-
default: "./dist/*.js",
2731
types: "./dist/*.d.ts",
32+
default: "./dist/*.js",
2833
},
2934
import: {
35+
types: "./dist/*.d.ts",
3036
default: "./dist/*.mjs",
37+
},
38+
/** without this default (THAT MUST BE LAST!!!) webpack will have a midlife crisis. */
39+
default: {
3140
types: "./dist/*.d.ts",
41+
default: "./dist/*.mjs",
3242
},
3343
},
3444
"./package.json": "./package.json",
@@ -37,7 +47,7 @@ const presetFields = {
3747
directory: "dist",
3848
access: "public",
3949
},
40-
};
50+
});
4151

4252
export const bootstrapCommand = createCommand<{}, {}>((api) => {
4353
return {

src/commands/build.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import mkdirp from "mkdirp";
1717
import { createCommand } from "../command";
1818
import { BobConfig } from "../config";
1919
import { rewriteExports } from "../utils/rewrite-exports";
20+
import { presetFields } from "./bootstrap";
2021

2122
interface BuildOptions {
2223
external?: string[];
@@ -406,17 +407,7 @@ function rewritePackageJson(pkg: Record<string, any>, distPath: string) {
406407
if (pkg.exports) {
407408
newPkg.exports = rewriteExports(pkg.exports, DIST_DIR);
408409
} else {
409-
newPkg.exports = {
410-
".": {
411-
require: "./index.js",
412-
import: "./index.mjs",
413-
},
414-
"./*": {
415-
require: "./*.js",
416-
import: "./*.mjs",
417-
},
418-
"./package.json": "./package.json",
419-
};
410+
newPkg.exports = presetFields.exports;
420411
}
421412

422413
if (pkg.bin) {
@@ -449,21 +440,23 @@ export function validatePackageJson(pkg: any) {
449440
expect("typescript.definition", `${DIST_DIR}/index.d.ts`);
450441

451442
expect("exports['.'].require", {
452-
default: `./${DIST_DIR}/index.js`,
453443
types: `./${DIST_DIR}/index.d.ts`,
444+
default: `./${DIST_DIR}/index.js`,
454445
});
455446
expect("exports['.'].import", {
456-
default: `./${DIST_DIR}/index.mjs`,
457447
types: `./${DIST_DIR}/index.d.ts`,
448+
default: `./${DIST_DIR}/index.mjs`,
458449
});
450+
expect("exports['.'].default", `./${DIST_DIR}/index.mjs`);
459451
expect("exports['./*'].require", {
460-
default: `./${DIST_DIR}/*.js`,
461452
types: `./${DIST_DIR}/*.d.ts`,
453+
default: `./${DIST_DIR}/*.js`,
462454
});
463455
expect("exports['./*'].import", {
464-
default: `./${DIST_DIR}/*.mjs`,
465456
types: `./${DIST_DIR}/*.d.ts`,
457+
default: `./${DIST_DIR}/*.mjs`,
466458
});
459+
expect("exports['./*'].default", `./${DIST_DIR}/*.mjs`);
467460
}
468461

469462
async function copyToDist(cwd: string, files: string[], distDir: string) {

src/utils/rewrite-exports.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
export function rewriteExports(
2-
exports: Record<string, string | { require?: string | { [key: string]: string }; import?: string | { [key: string]: string } }>,
2+
exports: Record<
3+
string,
4+
| string
5+
| {
6+
require?: string | { [key: string]: string };
7+
import?: string | { [key: string]: string };
8+
default?: string | { [key: string]: string };
9+
}
10+
>,
311
distDir: string
412
) {
513
const newExports = { ...exports };
@@ -8,52 +16,68 @@ export function rewriteExports(
816

917
newExports["."] = {
1018
require: {
19+
types: "./index.d.ts",
1120
default: "./index.js",
12-
types: "./index.d.ts"
1321
},
1422
import: {
23+
types: "./index.d.ts",
1524
default: "./index.mjs",
25+
},
26+
default: {
1627
types: "./index.d.ts",
28+
default: "./index.mjs",
1729
},
1830
};
1931

2032
newExports["./*"] = {
21-
require: {
33+
require: {
34+
types: "./*.d.ts",
2235
default: "./*.js",
23-
types: "./*.d.ts"
2436
},
2537
import: {
38+
types: "./*.d.ts",
39+
default: "./*.mjs",
40+
},
41+
default: {
42+
types: "./*.d.ts",
2643
default: "./*.mjs",
27-
types: "./*.d.ts"
2844
},
2945
};
3046

3147
for (const [key, value] of Object.entries(newExports)) {
3248
if (!value) continue;
3349

34-
let newValue = value as string | { require?: string | { [key: string]: string }; import?: string | { [key: string]: string } };
50+
let newValue = value as
51+
| string
52+
| {
53+
require?: string | { [key: string]: string };
54+
import?: string | { [key: string]: string };
55+
default?: string | { [key: string]: string };
56+
};
3557

3658
if (typeof newValue === "string") {
3759
newValue = newValue.replace(`${distDir}/`, "");
3860
} else if (typeof newValue === "object" && newValue != null) {
39-
40-
function transformValue(value: string | { [key: string]: string } | undefined) {
61+
function transformValue(
62+
value: string | { [key: string]: string } | undefined
63+
) {
4164
if (value == null) {
42-
return undefined
65+
return undefined;
4366
}
4467
if (typeof value === "object") {
45-
const newValue: Record<string, string> = {}
68+
const newValue: Record<string, string> = {};
4669
for (const [key, path] of Object.entries(value)) {
47-
newValue[key] = path.replace(`${distDir}/`, "")
70+
newValue[key] = path.replace(`${distDir}/`, "");
4871
}
49-
return newValue
72+
return newValue;
5073
}
51-
return value.replace(`${distDir}/`, "")
74+
return value.replace(`${distDir}/`, "");
5275
}
5376

5477
newValue = {
5578
require: transformValue(newValue.require),
5679
import: transformValue(newValue.import),
80+
default: transformValue(newValue.import),
5781
};
5882
}
5983
newExports[key.replace(`${distDir}/`, "")] = newValue;

test/__fixtures__/simple-monorepo/packages/a/package.json

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,25 @@
99
"exports": {
1010
".": {
1111
"require": {
12-
"default": "./dist/index.js",
13-
"types": "./dist/index.d.ts"
12+
"types": "./dist/index.d.ts",
13+
"default": "./dist/index.js"
1414
},
1515
"import": {
16-
"default": "./dist/index.mjs",
17-
"types": "./dist/index.d.ts"
18-
}
16+
"types": "./dist/index.d.ts",
17+
"default": "./dist/index.mjs"
18+
},
19+
"default": "./dist/index.mjs"
1920
},
2021
"./*": {
2122
"require": {
22-
"default": "./dist/*.js",
23-
"types": "./dist/*.d.ts"
23+
"types": "./dist/*.d.ts",
24+
"default": "./dist/*.js"
2425
},
2526
"import": {
26-
"default": "./dist/*.mjs",
27-
"types": "./dist/*.d.ts"
28-
}
27+
"types": "./dist/*.d.ts",
28+
"default": "./dist/*.mjs"
29+
},
30+
"default": "./dist/*.mjs"
2931
},
3032
"./package.json": "./package.json"
3133
},

test/__fixtures__/simple-monorepo/packages/b/package.json

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,25 @@
99
"exports": {
1010
".": {
1111
"require": {
12-
"default": "./dist/index.js",
13-
"types": "./dist/index.d.ts"
12+
"types": "./dist/index.d.ts",
13+
"default": "./dist/index.js"
1414
},
1515
"import": {
16-
"default": "./dist/index.mjs",
17-
"types": "./dist/index.d.ts"
18-
}
16+
"types": "./dist/index.d.ts",
17+
"default": "./dist/index.mjs"
18+
},
19+
"default": "./dist/index.mjs"
1920
},
2021
"./*": {
2122
"require": {
22-
"default": "./dist/*.js",
23-
"types": "./dist/*.d.ts"
23+
"types": "./dist/*.d.ts",
24+
"default": "./dist/*.js"
2425
},
2526
"import": {
26-
"default": "./dist/*.mjs",
27-
"types": "./dist/*.d.ts"
28-
}
27+
"types": "./dist/*.d.ts",
28+
"default": "./dist/*.mjs"
29+
},
30+
"default": "./dist/*.mjs"
2931
},
3032
"./package.json": "./package.json"
3133
},

test/__fixtures__/simple/package.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,30 @@
99
"exports": {
1010
".": {
1111
"require": {
12-
"default": "./dist/index.js",
13-
"types": "./dist/index.d.ts"
12+
"types": "./dist/index.d.ts",
13+
"default": "./dist/index.js"
1414
},
1515
"import": {
16-
"default": "./dist/index.mjs",
17-
"types": "./dist/index.d.ts"
18-
}
16+
"types": "./dist/index.d.ts",
17+
"default": "./dist/index.mjs"
18+
},
19+
"default": "./dist/index.mjs"
1920
},
2021
"./*": {
2122
"require": {
22-
"default": "./dist/*.js",
23-
"types": "./dist/*.d.ts"
23+
"types": "./dist/*.d.ts",
24+
"default": "./dist/*.js"
2425
},
2526
"import": {
26-
"default": "./dist/*.mjs",
27-
"types": "./dist/*.d.ts"
28-
}
27+
"types": "./dist/*.d.ts",
28+
"default": "./dist/*.mjs"
29+
},
30+
"default": "./dist/*.mjs"
2931
},
3032
"./package.json": "./package.json"
3133
},
3234
"publishConfig": {
3335
"directory": "dist",
3436
"access": "public"
3537
}
36-
}
38+
}

0 commit comments

Comments
 (0)