@sanity/pkg-utils
Version:
Simple utilities for modern npm packages.
161 lines (160 loc) • 5.64 kB
JavaScript
import path from "node:path";
import { up } from "empathic/package";
import { switchMap } from "rxjs";
import { loadConfig } from "./defaults.js";
import { resolveVanillaExtract, writeBundleCssExports, resolveVanillaExtractCssName, watchTaskHandlers } from "./index.js";
import { getTargetPaths, loadPkgWithReporting, resolveBuildContext } from "./resolveBuildContext.js";
import { createLogger, handleError } from "./handleError.js";
function resolveWatchTasks(ctx) {
const { pkg, target } = ctx, tasks = [], exports = Object.entries(ctx.exports || {}).map(
([_path, exp]) => Object.assign({}, exp, { _path })
), dtsTask = {
type: "watch:dts",
entries: []
}, rollupTasks = {};
function addRollupTaskEntry(format, runtime, entry) {
const buildId = `${format}:${runtime}`;
rollupTasks[buildId] ? rollupTasks[buildId].entries.push(entry) : rollupTasks[buildId] = {
type: "watch:js",
buildId,
entries: [entry],
runtime,
format,
target: target[runtime]
};
}
for (const exp of exports) {
const importId = path.join(pkg.name, exp._path);
exp.source?.endsWith(".ts") && dtsTask.entries.push({
importId,
exportPath: exp._path,
sourcePath: exp.source,
targetPaths: getTargetPaths(pkg.type, exp)
}), exp.browser?.source?.endsWith(".ts") && dtsTask.entries.push({
importId,
exportPath: exp._path,
sourcePath: exp.browser.source,
targetPaths: getTargetPaths(pkg.type, exp.browser)
}), exp.node?.source?.endsWith(".ts") && dtsTask.entries.push({
importId,
exportPath: exp._path,
sourcePath: exp.node.source,
targetPaths: getTargetPaths(pkg.type, exp.node)
});
}
for (const exp of exports) {
const output = exp.require;
output && addRollupTaskEntry("commonjs", ctx.runtime, {
path: exp._path,
source: exp.source,
output
});
}
for (const exp of exports) {
const output = exp.browser?.require;
output && addRollupTaskEntry("commonjs", "browser", {
path: exp._path,
source: exp.browser?.source || exp.source,
output
});
}
for (const exp of exports) {
const output = exp.import;
output && addRollupTaskEntry("esm", ctx.runtime, {
path: exp._path,
source: exp.source,
output
});
}
for (const exp of exports) {
const output = exp.browser?.import;
output && addRollupTaskEntry("esm", "browser", {
path: exp._path,
source: exp.browser?.source || exp.source,
output
});
}
for (const exp of exports) {
const output = exp.node?.require;
output && addRollupTaskEntry("commonjs", "node", {
path: exp._path,
source: exp.node?.source || exp.source,
output
});
}
for (const exp of exports) {
const output = exp.node?.import;
output && addRollupTaskEntry("esm", "node", {
path: exp._path,
source: exp.node?.source || exp.source,
output
});
}
return dtsTask.entries.length && tasks.push(dtsTask), tasks.push(...Object.values(rollupTasks)), tasks;
}
async function watch(options) {
const { cwd, strict = !1, tsconfig: tsconfigOption, signal } = options, logger = createLogger(), { watchConfigFiles } = await import("./watchConfigFiles.js"), configFiles$ = await watchConfigFiles({ cwd, logger }), taskSubscriptions = [], ctxSubscription = configFiles$.pipe(
switchMap(async (configFiles) => {
const packageJsonPath = configFiles.map((f) => path.relative(cwd, f)).find((f) => f === "package.json"), pkgPath = up({ cwd });
if (!packageJsonPath || !pkgPath)
throw new Error("missing package.json", { cause: { cwd } });
const config = await loadConfig({ cwd, pkgPath }), { parseStrictOptions } = await import("./resolveBuildContext.js").then(function(n) {
return n.strict;
}), strictOptions = parseStrictOptions(config?.strictOptions ?? {}), pkg = await loadPkgWithReporting({ pkgPath, logger, strict, strictOptions }), tsconfig = tsconfigOption || config?.tsconfig || "tsconfig.json";
return resolveBuildContext({ config, cwd, logger, pkg, strict, tsconfig });
})
).subscribe(async (ctx) => {
for (const sub of taskSubscriptions)
sub.unsubscribe();
taskSubscriptions.length = 0;
const vanillaExtract = resolveVanillaExtract(ctx.config);
vanillaExtract.compatMode && await writeBundleCssExports({
cwd,
distPath: ctx.distPath,
cssName: resolveVanillaExtractCssName(vanillaExtract.options, {
compatMode: !0,
runtime: "*"
}),
logger
});
const watchTasks = resolveWatchTasks(ctx);
for (const task of watchTasks) {
const handler = watchTaskHandlers[task.type], sub = handler.exec(ctx, task).subscribe({
error: (err) => {
ctx.logger.error(err), ctx.logger.log(), process.exit(1);
},
next: (result) => {
handler.complete(ctx, task, result);
},
complete: () => {
ctx.logger.success(handler.name(ctx, task)), ctx.logger.log();
}
});
taskSubscriptions.push(sub);
}
});
signal && signal.addEventListener(
"abort",
() => {
for (const sub of taskSubscriptions)
sub.unsubscribe();
taskSubscriptions.length = 0, ctxSubscription.unsubscribe();
},
{ once: !0 }
);
}
async function watchAction(options) {
try {
await watch({
cwd: process.cwd(),
strict: options.strict,
tsconfig: options.tsconfig
});
} catch (err) {
handleError(err);
}
}
export {
watchAction
};
//# sourceMappingURL=watchAction.js.map