one
Version:
One is a React framework for web, iOS, and Android apps.
251 lines (246 loc) • 8.28 kB
JavaScript
import { existsSync, readFileSync, writeFileSync } from "fs";
import path from "path";
import colors from "picocolors";
import { getRouterRootFromOneOptions } from "../utils/getRouterRootFromOneOptions.native.js";
var ONE_GENERATED_MARKER = "@one/generated bundler-config";
function buildBabelConfigContent(param) {
var { eject, options } = param;
var header = eject ? `// you own this file. edit freely. \`one\` will not regenerate it.
// delegates to one/babel-preset which holds the canonical plugin chain.
` : `// ${ONE_GENERATED_MARKER}
//
// auto-generated by \`one metro-eject\`. delegates to one/babel-preset so
// Metro uses the same router/setup options as \`one dev\` and \`one build\`.
//
// to customize, delete this header and edit freely. re-runs will then
// leave this file alone.
`;
return `${header}
const oneBabelPreset = require('one/babel-preset')
const preset = oneBabelPreset.default || oneBabelPreset
const oneBundlerOptions = ${serializeBundlerConfigOptions(options)}
module.exports = function (api) {
return preset(api, oneBundlerOptions)
}
`;
}
function buildMetroConfigContent(param) {
var { eject, options } = param;
var header = eject ? `// you own this file. edit freely. \`one\` will not regenerate it.
// withOne() invokes the same Metro pipeline One uses for production bundles.
` : `// ${ONE_GENERATED_MARKER}
//
// auto-generated by \`one metro-eject\`. delegates to one/metro-config,
// which invokes the same metro pipeline One uses for production native
// bundles with your router and setup options.
//
// to customize, delete this header and edit freely. re-runs will then
// leave this file alone.
`;
return `${header}
const { withOne } = require('one/metro-config')
const oneBundlerOptions = ${serializeBundlerConfigOptions(options)}
module.exports = withOne(__dirname, oneBundlerOptions)
`;
}
var FILES = [{
name: "babel.config.cjs",
getContent: buildBabelConfigContent,
conflicting: [
"babel.config.js",
"babel.config.mjs",
".babelrc",
".babelrc.js"
]
}, {
name: "metro.config.cjs",
getContent: buildMetroConfigContent,
conflicting: ["metro.config.js", "metro.config.mjs"]
}];
function stripUndefined(value) {
if (Array.isArray(value)) {
return value.map(stripUndefined);
}
if (value && typeof value === "object") {
return Object.fromEntries(Object.entries(value).filter(function(param) {
var [, entry] = param;
return entry !== void 0;
}).map(function(param) {
var [key, entry] = param;
return [key, stripUndefined(entry)];
}));
}
return value;
}
function assertSerializable(value) {
var keyPath = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "one bundler options";
if (typeof value === "function" || typeof value === "symbol" || typeof value === "bigint") {
throw new Error(`[one] ${keyPath} must be JSON-serializable to generate Babel/Metro config files. Move function-valued native linking/customization into an ejected config.`);
}
if (Array.isArray(value)) {
value.forEach(function(entry2, index) {
return assertSerializable(entry2, `${keyPath}[${index}]`);
});
return;
}
if (value && typeof value === "object") {
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
try {
for (var _iterator = Object.entries(value)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var [key, entry] = _step.value;
assertSerializable(entry, `${keyPath}.${key}`);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
}
function serializeBundlerConfigOptions(options) {
var clean = stripUndefined(options);
assertSerializable(clean);
return JSON.stringify(clean, null, 2);
}
function getBundlerConfigOptionsFromOneOptions() {
var oneOptions = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
var _oneOptions_router, _oneOptions_router1;
return stripUndefined({
routerRoot: getRouterRootFromOneOptions(oneOptions),
ignoredRouteFiles: (_oneOptions_router = oneOptions.router) === null || _oneOptions_router === void 0 ? void 0 : _oneOptions_router.ignoredRouteFiles,
linking: (_oneOptions_router1 = oneOptions.router) === null || _oneOptions_router1 === void 0 ? void 0 : _oneOptions_router1.linking,
setupFile: oneOptions.setupFile
});
}
function generateBundlerConfig() {
var args = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
var _args_cwd;
var cwd = path.resolve((_args_cwd = args.cwd) !== null && _args_cwd !== void 0 ? _args_cwd : process.cwd());
var force = !!args.force;
var check = !!args.check;
var quiet = !!args.quiet;
var log = function log2(msg) {
if (!quiet) console.info(msg);
};
var warn = function warn2(msg) {
if (!quiet) console.warn(msg);
};
var results = [];
var eject = !!args.eject;
var bundlerOptions = getBundlerConfigOptionsFromOneOptions(args.oneOptions);
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
try {
for (var _iterator = FILES[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var file = _step.value;
var filePath = path.join(cwd, file.name);
var targetContent = file.getContent({
eject,
options: bundlerOptions
});
var conflict = file.conflicting.find(function(alt) {
return existsSync(path.join(cwd, alt));
});
if (conflict && !existsSync(filePath)) {
results.push({
filePath: path.join(cwd, conflict),
action: "skipped-other-format",
reason: `Found ${conflict}; not creating ${file.name}. To switch, delete ${conflict} and re-run with --force.`
});
warn(colors.yellow(`[one] found ${conflict}. Leaving it alone. Delete it and re-run with --force to switch to ${file.name}.`));
continue;
}
if (!existsSync(filePath)) {
if (check) {
results.push({
filePath,
action: "would-write"
});
log(colors.yellow(`[one] missing: ${file.name}`));
continue;
}
writeFileSync(filePath, targetContent);
results.push({
filePath,
action: "wrote"
});
log(colors.green(`[one] wrote ${file.name}`));
continue;
}
var existing = readFileSync(filePath, "utf8");
if (existing === targetContent) {
results.push({
filePath,
action: "kept"
});
log(colors.dim(`[one] up to date: ${file.name}`));
continue;
}
var hasMarker = existing.includes(ONE_GENERATED_MARKER);
if (!hasMarker && !force) {
results.push({
filePath,
action: "skipped-customized",
reason: `${file.name} has been customized (no @one marker). Re-add the marker comment or pass --force to overwrite.`
});
warn(colors.yellow(`[one] ${file.name} appears customized. Skipping. Pass --force to overwrite.`));
continue;
}
if (check) {
results.push({
filePath,
action: "would-overwrite"
});
log(colors.yellow(`[one] out of date: ${file.name}`));
continue;
}
writeFileSync(filePath, targetContent);
results.push({
filePath,
action: "wrote"
});
log(colors.green(`[one] updated ${file.name}`));
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
var acceptableAlways = /* @__PURE__ */ new Set([
"wrote",
"kept",
"skipped-other-format",
"skipped-customized"
]);
var acceptableInCheck = /* @__PURE__ */ new Set([
"kept",
"skipped-other-format",
"skipped-customized"
]);
var ok = (check ? acceptableInCheck : acceptableAlways).size ? results.every(function(r) {
return (check ? acceptableInCheck : acceptableAlways).has(r.action);
}) : false;
return {
results,
ok
};
}
export { ONE_GENERATED_MARKER, generateBundlerConfig, getBundlerConfigOptionsFromOneOptions };
//# sourceMappingURL=generateBundlerConfig.native.js.map