Skip to content

Commit

Permalink
chore: drop bundling and restructure output files
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Jun 22, 2022
1 parent 11bc1d5 commit bdaa67c
Show file tree
Hide file tree
Showing 16 changed files with 456 additions and 581 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage/
dist/
public/
*.map
.bob
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@
"jest": "27.5.1",
"rimraf": "3.0.2",
"ts-jest": "27.1.5",
"typescript": "4.7.3"
"typescript": "4.7.4"
},
"peerDependencies": {
"typescript": "^4.7.4"
},
"publishConfig": {
"access": "public"
Expand Down
114 changes: 76 additions & 38 deletions src/commands/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
import globby from "globby";
import pLimit from "p-limit";
import * as fse from "fs-extra";
import { createCommand } from "../command";

/** The default bob fields that should be within a package.json */
export const presetFields = Object.freeze({
main: "dist/index.js",
module: "dist/index.mjs",
typings: "dist/index.d.ts",
type: "module",
main: "dist/cjs/index.js",
module: "dist/esm/index.mjs",
typings: "dist/typings/index.d.ts",
typescript: {
definition: "dist/index.d.ts",
definition: "dist/typings/index.d.ts",
},
exports: {
".": {
require: {
types: "./dist/index.d.ts",
default: "./dist/index.js",
types: "./dist/typings/index.d.ts",
default: "./dist/cjs/index.js",
},
import: {
types: "./dist/index.d.ts",
default: "./dist/index.mjs",
types: "./dist/typings/index.d.ts",
default: "./dist/esm/index.mjs",
},
/** without this default (THAT MUST BE LAST!!!) webpack will have a midlife crisis. */
default: {
types: "./dist/index.d.ts",
default: "./dist/index.mjs",
types: "./dist/typings/index.d.mts",
default: "./dist/esm/index.mjs",
},
},
"./*": {
require: {
types: "./dist/*.d.ts",
default: "./dist/*.js",
types: "./dist/typings/*.d.ts",
default: "./dist/cjs/*.js",
},
import: {
types: "./dist/*.d.ts",
default: "./dist/*.mjs",
types: "./dist/typings/*.d.ts",
default: "./dist/esm/*.js",
},
/** without this default (THAT MUST BE LAST!!!) webpack will have a midlife crisis. */
default: {
types: "./dist/*.d.ts",
default: "./dist/*.mjs",
types: "./dist/typings/*.d.ts",
default: "./dist/esm/*.mjs",
},
},
"./package.json": "./package.json",
Expand All @@ -49,6 +51,44 @@ export const presetFields = Object.freeze({
},
});

function transformModuleImports(fileContents: string) {
return fileContents.replace(
/* this regex should hopefully catch all kind of import/export expressions that are relative. */
/((import|export)\s+.*\s+from\s+["'])((\.\/|\.\.\/).*(?!.js))(["'])/g,
(_, importFromPart, __, modulePath, ___, hyphenEndPart) =>
`${importFromPart}${modulePath}.js${hyphenEndPart}`
);
}

async function applyESMModuleTransform(distDirs: Array<string>) {
const files = await globby("**/*.ts", {
cwd: process.cwd(),
absolute: true,
ignore: ["**/node_modules/**", ...distDirs],
});

const limit = pLimit(20);

await Promise.all(
files.map((file) =>
limit(async () => {
const contents = await fse.readFile(file, "utf-8");
await fse.writeFile(file, transformModuleImports(contents));
})
)
);
}

async function applyPackageJSONPresetConfig(
packageJSONPath: string,
packageJSON: Record<string, unknown>
) {
Object.assign(packageJSON, presetFields);
await fse.writeFile(packageJSONPath, JSON.stringify(packageJSON, null, 2));
}

const limit = pLimit(20);

export const bootstrapCommand = createCommand<{}, {}>((api) => {
return {
command: "bootstrap",
Expand Down Expand Up @@ -77,37 +117,35 @@ export const bootstrapCommand = createCommand<{}, {}>((api) => {
const isSinglePackage =
Array.isArray(rootPackageJSON.workspaces) === false;

const applyPresetConfig = async (
packageJSONPath: string,
packageJSON: Record<string, unknown>
) => {
Object.assign(packageJSON, presetFields);
await fse.writeFile(
packageJSONPath,
JSON.stringify(packageJSON, null, 2)
);
};
const distDirs =
config.dists?.map(({ distDir }) => `**/${distDir}/**`) ?? [];

// Make sure all modules are converted to ESM
await applyESMModuleTransform(distDirs);

if (isSinglePackage) {
await applyPresetConfig(rootPackageJSONPath, rootPackageJSON);
await applyPackageJSONPresetConfig(
rootPackageJSONPath,
rootPackageJSON
);
return;
}

const packageJSONPaths = await globby("packages/**/package.json", {
cwd: process.cwd(),
absolute: true,
ignore: [
"**/node_modules/**",
...(config.dists?.map(({ distDir }) => `**/${distDir}/**`) ?? []),
],
ignore: ["**/node_modules/**", ...distDirs],
});

for (const packageJSONPath of packageJSONPaths) {
const packageJSON: Record<string, unknown> = await fse.readJSON(
packageJSONPath
);
await applyPresetConfig(packageJSONPath, packageJSON);
}
await Promise.all(
packageJSONPaths.map((packageJSONPath) =>
limit(async () => {
const packageJSON: Record<string, unknown> = await fse.readJSON(
packageJSONPath
);
await applyPackageJSONPresetConfig(packageJSONPath, packageJSON);
})
)
);
},
};
});
Loading

0 comments on commit bdaa67c

Please sign in to comment.