UNPKG

@sanity/pkg-utils

Version:

Simple utilities for modern npm packages.

122 lines (121 loc) 5.85 kB
"use strict"; var path = require("node:path"), rolldownPluginDts = require("rolldown-plugin-dts"), index = require("./index.cjs"); function _interopDefaultCompat(e) { return e && typeof e == "object" && "default" in e ? e : { default: e }; } var path__default = /* @__PURE__ */ _interopDefaultCompat(path); function resolveRolldownConfig(ctx, buildTask) { const { format, runtime, target } = buildTask, { config, cwd, exports: _exports, external, distPath, logger, pkg, ts } = ctx, outputExt = index.pkgExtMap[pkg.type || "commonjs"][format], outDir = path__default.default.relative(cwd, distPath), pathAliases = Object.fromEntries( Object.entries(ts.config?.options.paths || {}).map(([key, val]) => [key, path__default.default.resolve(cwd, ts.config?.options.baseUrl || ".", val[0])]) ), entries = buildTask.entries.map((entry) => ({ ...entry, name: path__default.default.relative(outDir, entry.output).replace(/\.[^/.]+$/, "") }), {}), exportIds = _exports && Object.keys(_exports).map((exportPath) => path__default.default.join(pkg.name, exportPath)), sourcePaths = _exports && Object.values(_exports).map((e) => path__default.default.resolve(cwd, e.source)), replacements = Object.fromEntries( Object.entries(config?.define || {}).map(([key, val]) => [key, JSON.stringify(val)]) ), entryFileNames = `[name]${outputExt}`, inputOptions = { cwd, jsx: { mode: config?.jsx === "automatic" ? "automatic" : config?.jsx === "preserve" ? "preserve" : config?.jsx === "transform" ? "classic" : "automatic", factory: config?.jsxFactory, fragment: config?.jsxFragment, importSource: config?.jsxImportSource }, define: pkg.name === "@sanity/pkg-utils" ? { ...replacements } : { "process.env.PKG_FORMAT": JSON.stringify(format), "process.env.PKG_RUNTIME": JSON.stringify(runtime), "process.env.PKG_VERSION": JSON.stringify(process.env.PKG_VERSION || pkg.version), ...replacements }, platform: buildTask.type === "rolldown:dts" || runtime === "node" ? "node" : runtime === "browser" ? "browser" : "neutral", resolve: { alias: pathAliases, tsconfigFilename: ctx.ts.configPath || "tsconfig.json" }, transform: { target }, experimental: { attachDebugInfo: "none" }, external: (id, importer) => { if (buildTask.type === "rolldown:dts" && ctx.bundledPackages.length > 0 && !id.startsWith(".") && (id.includes("/node_modules/") || id.split("/").length < 3)) return ctx.bundledPackages.some((name) => name === id || id.includes(`/${name}/`)) === !1; if (exportIds?.includes(id)) return !0; if (importer && (id.startsWith(".") || id.startsWith("/"))) { const idPath = path__default.default.resolve(path__default.default.dirname(importer), id); if (sourcePaths?.includes(idPath)) return logger.warn( `detected self-referencing import \u2013 treating as external: ${path__default.default.relative( cwd, idPath )}` ), !0; } return external.some((name) => name === id || id.includes(`/${name}/`)); }, input: entries.reduce( (acc, entry) => Object.assign(acc, { [entry.name]: entry.source }), {} ), plugins: [ rolldownPluginDts.dts({ emitDtsOnly: !0, tsconfig: ctx.ts.configPath || "tsconfig.json", tsgo: typeof pkg.devDependencies == "object" && "@typescript/native-preview" in pkg.devDependencies, // Enable the resolver by giving an allow list of packages to dig through node_modules. // If there are none that should be resolved, then this process is skipped and our build is faster. // If there's a match then it'll call the resolver function defined on `inputOptions.external` resolve: ctx.bundledPackages, // Always create dts from scratch, don't reuse contexts from previous builds newContext: !0 }) ], treeshake: { moduleSideEffects: "no-external", unknownGlobalSideEffects: !1, annotations: !0 } }, outputOptions = { dir: outDir, entryFileNames, chunkFileNames: buildTask.type === "rolldown:dts" ? `_chunks-dts/[name]${outputExt}` : void 0, esModule: !0, format: buildTask.type === "rolldown:dts" ? "es" : format, sourcemap: buildTask.type === "rolldown:dts" ? !1 : config?.sourcemap ?? !0, hoistTransitiveImports: !1 /** * rolldown doesn't permit disabling chunks, we can only choose between automatic chunking (the default) * and manual chunking (using advancedChunks). * This matters for how we generate dts files. * While we use rolldown for dts generation, and rollup for the rest, we want to reduce the amount of dts files emitted as chunks, * and to make them predictable in how they're generated. */ // @TODO the following breaks the dts generation, report bug to rolldown // advancedChunks: // buildTask.type === 'rolldown:dts' // ? { // groups: [ // { // /** // * Groups all inlined typings specified by bundledPackages in extract.bundledPackages to its own chunk. // */ // name: 'bundled-packages', // test: (id) => ctx.bundledPackages.includes(id), // priority: 1, // }, // { // /** // * Put all other shared chunks into a single chunk. // */ // name: 'shared', // minShareCount: 1, // }, // ], // } // : {}, }; return { inputOptions, outputOptions }; } exports.resolveRolldownConfig = resolveRolldownConfig; //# sourceMappingURL=resolveRolldownConfig.cjs.map