UNPKG

react-native-builder-bob

Version:

CLI to build JavaScript files for React Native libraries

142 lines (141 loc) 4.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.args = void 0; exports.build = build; var _arktype = require("arktype"); var _fsExtra = _interopRequireDefault(require("fs-extra")); var _kleur = _interopRequireDefault(require("kleur")); var _path = _interopRequireDefault(require("path")); var _schema = require("./schema"); var _loadConfig = require("./utils/loadConfig"); var logger = _interopRequireWildcard(require("./utils/logger")); var _workerize = require("./utils/workerize"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const args = exports.args = { target: { type: 'string', description: 'The target to build', choices: ['commonjs', 'module', 'typescript', 'codegen'] } }; async function build(argv) { const root = process.cwd(); const projectPackagePath = _path.default.resolve(root, 'package.json'); if (!(await _fsExtra.default.pathExists(projectPackagePath))) { throw new Error(`Couldn't find a 'package.json' file in '${root}'. Are you in a project folder?`); } const result = (0, _loadConfig.loadConfig)(root); if (!result?.config) { throw new Error(`No configuration found. Run '${argv.$0} init' to create one automatically.`); } const parsed = (0, _schema.config)(result.config); if (parsed instanceof _arktype.type.errors) { throw new Error(`Invalid configuration in ${result.filepath}: ${parsed.summary}`); } const { source, output, targets, exclude } = parsed; const commonjs = targets.some(t => Array.isArray(t) ? t[0] === 'commonjs' : t === 'commonjs'); const module = targets.some(t => Array.isArray(t) ? t[0] === 'module' : t === 'module'); const variants = { commonjs, module }; if (argv.target != null) { await buildTarget({ root, target: argv.target, source, output, exclude, config: parsed, variants }); } else { await Promise.all(targets.map(async target => buildTarget({ root, target: Array.isArray(target) ? target[0] : target, source, output, exclude, config: parsed, variants }))); } } async function buildTarget({ root, target, source, output, exclude, config, variants }) { const options = config.targets.map(t => Array.isArray(t) ? t : [t, undefined]).find(t => t[0] === target)?.[1]; const report = logger.grouped(target); switch (target) { case 'commonjs': case 'module': await (0, _workerize.run)(target, { root, source: _path.default.resolve(root, source), output: _path.default.resolve(root, output, target), exclude, // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion options: options, variants, report }); break; case 'typescript': { const esm = config.targets?.some(t => { if (Array.isArray(t)) { const [name, options] = t; if (name === 'module') { return options && 'esm' in options && options?.esm; } } return false; }) ?? false; await (0, _workerize.run)('typescript', { root, source: _path.default.resolve(root, source), output: _path.default.resolve(root, output, 'typescript'), // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion options: options, esm, variants, report }); } break; case 'codegen': await (0, _workerize.run)('codegen', { root, source: _path.default.resolve(root, source), report }); break; case 'custom': await (0, _workerize.run)('custom', { root, source: _path.default.resolve(root, source), // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion options: options, report }); break; default: throw new Error(`Invalid target ${_kleur.default.blue(target)}.`); } } //# sourceMappingURL=build.js.map