UNPKG

npmize

Version:

Let's create an npm package without worrying about anything.

78 lines (77 loc) 3.68 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; const fs_1 = __importDefault(require("fs")); const tsc_1 = __importDefault(require("../scripts/tsc")); const ansi_colors_1 = __importDefault(require("ansi-colors")); const makeOutputFile_1 = __importDefault(require("../scripts/makeOutputFile")); const utils_1 = require("../utils"); const fs_2 = require("../utils/fs"); function default_1(rootPath, options) { return __awaiter(this, void 0, void 0, function* () { console.log(`Build started at ${rootPath}`); console.log(''); (0, fs_2.cleanDir)(options.tsConfig.outDir); if (options.module) { return runBuild(rootPath, options.tsConfig.outDir, options.module, options); } yield runBuild(rootPath, options.tsConfig.outDir, 'cjs', options); yield runBuild(rootPath, options.tsConfig.outDir, 'mjs', options); }); } function runBuild(rootPath, fullOutDir, moduleType, options) { return __awaiter(this, void 0, void 0, function* () { console.log(`Building ${moduleType}...`); console.time('Build time'); const tempOutDir = (0, utils_1.getNodeModulesTempDir)(rootPath, 'build-' + moduleType); (0, fs_2.cleanDir)(tempOutDir); (0, tsc_1.default)(rootPath, [ ...options.tsc, '--outDir', tempOutDir, '--module', moduleType === 'cjs' ? 'commonjs' : 'esnext', ]); const promisesOfFiles = (0, fs_2.getAllFiles)(tempOutDir).map((file) => { var _a, _b; return (0, makeOutputFile_1.default)(file, { tempOutDir: tempOutDir, finalOutDir: fullOutDir, moduleType: moduleType, useWorker: options.worker, pushNodeCode: options.node, tsConfig: { paths: (_a = options.tsConfig) === null || _a === void 0 ? void 0 : _a.paths, baseUrl: (_b = options.tsConfig) === null || _b === void 0 ? void 0 : _b.baseUrl, }, }); }); const newFiles = yield Promise.all(promisesOfFiles); console.timeEnd('Build time'); const fileSizes = newFiles .filter((a) => a.endsWith('js')) .map((file) => { const stat = fs_1.default.statSync(file); return { file, size: stat.size, }; }); const totalSize = fileSizes.reduce((acc, { size }) => acc + size, 0); console.log(`Built ${ansi_colors_1.default.yellow(String(fileSizes.length))} JavaScript files with a total size of ${ansi_colors_1.default.yellow((totalSize / 1024).toFixed(2))} KB`); (0, fs_2.cleanDir)(tempOutDir, false); console.log(''); }); }