bob-the-bundler
Version:
Bob The Bundler!
119 lines (118 loc) • 4.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bootstrapCommand = exports.presetFieldsESM = exports.presetFields = void 0;
const tslib_1 = require("tslib");
const globby_1 = tslib_1.__importDefault(require("globby"));
const p_limit_1 = tslib_1.__importDefault(require("p-limit"));
const path = tslib_1.__importStar(require("path"));
const fse = tslib_1.__importStar(require("fs-extra"));
const command_1 = require("../command");
const constants_1 = require("../constants");
const get_root_package_json_1 = require("../utils/get-root-package-json");
const get_workspaces_1 = require("../utils/get-workspaces");
const get_workspace_package_paths_1 = require("../utils/get-workspace-package-paths");
const rewrite_code_imports_1 = require("../utils/rewrite-code-imports");
/** The default bob fields that should be within a package.json */
exports.presetFields = Object.freeze({
type: "module",
main: "dist/cjs/index.js",
module: "dist/esm/index.js",
typings: "dist/typings/index.d.ts",
typescript: {
definition: "dist/typings/index.d.ts",
},
exports: {
".": {
require: {
types: "./dist/typings/index.d.cts",
default: "./dist/cjs/index.js",
},
import: {
types: "./dist/typings/index.d.ts",
default: "./dist/esm/index.js",
},
/** without this default (THAT MUST BE LAST!!!) webpack will have a midlife crisis. */
default: {
types: "./dist/typings/index.d.ts",
default: "./dist/esm/index.js",
},
},
"./package.json": "./package.json",
},
publishConfig: {
directory: "dist",
access: "public",
},
});
exports.presetFieldsESM = {
type: "module",
main: "dist/esm/index.js",
module: "dist/esm/index.js",
typings: "dist/typings/index.d.ts",
typescript: {
definition: "dist/typings/index.d.ts",
},
exports: {
".": {
import: {
types: "./dist/typings/index.d.ts",
default: "./dist/esm/index.js",
},
/** without this default (THAT MUST BE LAST!!!) webpack will have a midlife crisis. */
default: {
types: "./dist/typings/index.d.ts",
default: "./dist/esm/index.js",
},
},
"./package.json": "./package.json",
},
publishConfig: {
directory: "dist",
access: "public",
},
};
async function applyESMModuleTransform(cwd) {
const filePaths = await (0, globby_1.default)("**/*.ts", {
cwd,
absolute: true,
ignore: ["**/node_modules/**", ...constants_1.buildArtifactDirectories],
});
const limit = (0, p_limit_1.default)(20);
await Promise.all(filePaths.map((filePath) => limit(async () => {
const contents = await fse.readFile(filePath, "utf-8");
await fse.writeFile(filePath, (0, rewrite_code_imports_1.rewriteCodeImports)(contents, filePath));
})));
}
async function applyPackageJSONPresetConfig(packageJSONPath, packageJSON) {
Object.assign(packageJSON, exports.presetFields);
await fse.writeFile(packageJSONPath, JSON.stringify(packageJSON, null, 2));
}
const limit = (0, p_limit_1.default)(20);
exports.bootstrapCommand = (0, command_1.createCommand)(() => {
return {
command: "bootstrap",
describe: "The easiest way of setting all the right exports on your package.json files without hassle.",
builder(yargs) {
return yargs.options({});
},
async handler() {
const cwd = process.cwd();
const rootPackageJSON = await (0, get_root_package_json_1.getRootPackageJSON)(cwd);
const workspaces = (0, get_workspaces_1.getWorkspaces)(rootPackageJSON);
const isSinglePackage = workspaces === null;
// Make sure all modules are converted to ESM
if (isSinglePackage) {
await applyESMModuleTransform(cwd);
await applyPackageJSONPresetConfig(path.join(cwd, "package.json"), rootPackageJSON);
return;
}
const workspacePackagePaths = await (0, get_workspace_package_paths_1.getWorkspacePackagePaths)(cwd, workspaces);
await Promise.all(workspacePackagePaths.map((packagePath) => limit(async () => {
const packageJSONPath = path.join(packagePath, "package.json");
const packageJSON = await fse.readJSON(packageJSONPath);
await applyESMModuleTransform(packagePath);
await applyPackageJSONPresetConfig(packageJSONPath, packageJSON);
})));
},
};
});