Skip to content

Commit bdaa67c

Browse files
committed
chore: drop bundling and restructure output files
1 parent 11bc1d5 commit bdaa67c

File tree

16 files changed

+456
-581
lines changed

16 files changed

+456
-581
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ coverage/
55
dist/
66
public/
77
*.map
8+
.bob

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@
6767
"jest": "27.5.1",
6868
"rimraf": "3.0.2",
6969
"ts-jest": "27.1.5",
70-
"typescript": "4.7.3"
70+
"typescript": "4.7.4"
71+
},
72+
"peerDependencies": {
73+
"typescript": "^4.7.4"
7174
},
7275
"publishConfig": {
7376
"access": "public"

src/commands/bootstrap.ts

Lines changed: 76 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
11
import globby from "globby";
2+
import pLimit from "p-limit";
23
import * as fse from "fs-extra";
34
import { createCommand } from "../command";
45

56
/** The default bob fields that should be within a package.json */
67
export const presetFields = Object.freeze({
7-
main: "dist/index.js",
8-
module: "dist/index.mjs",
9-
typings: "dist/index.d.ts",
8+
type: "module",
9+
main: "dist/cjs/index.js",
10+
module: "dist/esm/index.mjs",
11+
typings: "dist/typings/index.d.ts",
1012
typescript: {
11-
definition: "dist/index.d.ts",
13+
definition: "dist/typings/index.d.ts",
1214
},
1315
exports: {
1416
".": {
1517
require: {
16-
types: "./dist/index.d.ts",
17-
default: "./dist/index.js",
18+
types: "./dist/typings/index.d.ts",
19+
default: "./dist/cjs/index.js",
1820
},
1921
import: {
20-
types: "./dist/index.d.ts",
21-
default: "./dist/index.mjs",
22+
types: "./dist/typings/index.d.ts",
23+
default: "./dist/esm/index.mjs",
2224
},
2325
/** without this default (THAT MUST BE LAST!!!) webpack will have a midlife crisis. */
2426
default: {
25-
types: "./dist/index.d.ts",
26-
default: "./dist/index.mjs",
27+
types: "./dist/typings/index.d.mts",
28+
default: "./dist/esm/index.mjs",
2729
},
2830
},
2931
"./*": {
3032
require: {
31-
types: "./dist/*.d.ts",
32-
default: "./dist/*.js",
33+
types: "./dist/typings/*.d.ts",
34+
default: "./dist/cjs/*.js",
3335
},
3436
import: {
35-
types: "./dist/*.d.ts",
36-
default: "./dist/*.mjs",
37+
types: "./dist/typings/*.d.ts",
38+
default: "./dist/esm/*.js",
3739
},
3840
/** without this default (THAT MUST BE LAST!!!) webpack will have a midlife crisis. */
3941
default: {
40-
types: "./dist/*.d.ts",
41-
default: "./dist/*.mjs",
42+
types: "./dist/typings/*.d.ts",
43+
default: "./dist/esm/*.mjs",
4244
},
4345
},
4446
"./package.json": "./package.json",
@@ -49,6 +51,44 @@ export const presetFields = Object.freeze({
4951
},
5052
});
5153

54+
function transformModuleImports(fileContents: string) {
55+
return fileContents.replace(
56+
/* this regex should hopefully catch all kind of import/export expressions that are relative. */
57+
/((import|export)\s+.*\s+from\s+["'])((\.\/|\.\.\/).*(?!.js))(["'])/g,
58+
(_, importFromPart, __, modulePath, ___, hyphenEndPart) =>
59+
`${importFromPart}${modulePath}.js${hyphenEndPart}`
60+
);
61+
}
62+
63+
async function applyESMModuleTransform(distDirs: Array<string>) {
64+
const files = await globby("**/*.ts", {
65+
cwd: process.cwd(),
66+
absolute: true,
67+
ignore: ["**/node_modules/**", ...distDirs],
68+
});
69+
70+
const limit = pLimit(20);
71+
72+
await Promise.all(
73+
files.map((file) =>
74+
limit(async () => {
75+
const contents = await fse.readFile(file, "utf-8");
76+
await fse.writeFile(file, transformModuleImports(contents));
77+
})
78+
)
79+
);
80+
}
81+
82+
async function applyPackageJSONPresetConfig(
83+
packageJSONPath: string,
84+
packageJSON: Record<string, unknown>
85+
) {
86+
Object.assign(packageJSON, presetFields);
87+
await fse.writeFile(packageJSONPath, JSON.stringify(packageJSON, null, 2));
88+
}
89+
90+
const limit = pLimit(20);
91+
5292
export const bootstrapCommand = createCommand<{}, {}>((api) => {
5393
return {
5494
command: "bootstrap",
@@ -77,37 +117,35 @@ export const bootstrapCommand = createCommand<{}, {}>((api) => {
77117
const isSinglePackage =
78118
Array.isArray(rootPackageJSON.workspaces) === false;
79119

80-
const applyPresetConfig = async (
81-
packageJSONPath: string,
82-
packageJSON: Record<string, unknown>
83-
) => {
84-
Object.assign(packageJSON, presetFields);
85-
await fse.writeFile(
86-
packageJSONPath,
87-
JSON.stringify(packageJSON, null, 2)
88-
);
89-
};
120+
const distDirs =
121+
config.dists?.map(({ distDir }) => `**/${distDir}/**`) ?? [];
122+
123+
// Make sure all modules are converted to ESM
124+
await applyESMModuleTransform(distDirs);
90125

91126
if (isSinglePackage) {
92-
await applyPresetConfig(rootPackageJSONPath, rootPackageJSON);
127+
await applyPackageJSONPresetConfig(
128+
rootPackageJSONPath,
129+
rootPackageJSON
130+
);
93131
return;
94132
}
95133

96134
const packageJSONPaths = await globby("packages/**/package.json", {
97135
cwd: process.cwd(),
98136
absolute: true,
99-
ignore: [
100-
"**/node_modules/**",
101-
...(config.dists?.map(({ distDir }) => `**/${distDir}/**`) ?? []),
102-
],
137+
ignore: ["**/node_modules/**", ...distDirs],
103138
});
104-
105-
for (const packageJSONPath of packageJSONPaths) {
106-
const packageJSON: Record<string, unknown> = await fse.readJSON(
107-
packageJSONPath
108-
);
109-
await applyPresetConfig(packageJSONPath, packageJSON);
110-
}
139+
await Promise.all(
140+
packageJSONPaths.map((packageJSONPath) =>
141+
limit(async () => {
142+
const packageJSON: Record<string, unknown> = await fse.readJSON(
143+
packageJSONPath
144+
);
145+
await applyPackageJSONPresetConfig(packageJSONPath, packageJSON);
146+
})
147+
)
148+
);
111149
},
112150
};
113151
});

0 commit comments

Comments
 (0)