UNPKG

esbuild-plugin-node-externals

Version:
147 lines (138 loc) 5.44 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { default: () => src_default, nodeExternals: () => nodeExternals }); module.exports = __toCommonJS(src_exports); // src/lib/find-package-path.ts var import_path2 = __toESM(require("path")); var import_find_up = __toESM(require("find-up")); // src/lib/is-in-dir.ts var import_path = __toESM(require("path")); var isInDirectory = /* @__PURE__ */ __name((parent, child) => { const relativePath = import_path.default.relative(parent, child); return !relativePath.startsWith("..") && !import_path.default.isAbsolute(relativePath); }, "isInDirectory"); var isInGitDirectory = /* @__PURE__ */ __name((path3, gitRootPath) => { return gitRootPath === void 0 || isInDirectory(gitRootPath, path3); }, "isInGitDirectory"); // src/lib/find-package-path.ts var findPackagePaths = /* @__PURE__ */ __name(() => { const gitDirectoryPath = import_find_up.default.sync(".git", { type: "directory" }); const gitRootPath = gitDirectoryPath === void 0 ? void 0 : import_path2.default.dirname(gitDirectoryPath); let cwd = process.cwd(); let packagePath; const packagePaths = []; while ((packagePath = import_find_up.default.sync("package.json", { type: "file", cwd })) && isInGitDirectory(packagePath, gitRootPath)) { packagePaths.push(packagePath); cwd = import_path2.default.dirname(import_path2.default.dirname(packagePath)); } return packagePaths; }, "findPackagePaths"); // src/lib/normalize-options.ts var normalizeOptions = /* @__PURE__ */ __name(({ packagePaths, withDeps, withDevDeps, withPeerDeps, withOptDeps, include } = {}) => { const normalizedOptions = { packagePaths: [], withDeps: withDeps ?? true, withDevDeps: withDevDeps ?? true, withPeerDeps: withPeerDeps ?? true, withOptDeps: withOptDeps ?? true, include: include ?? [] }; if (!packagePaths) { normalizedOptions.packagePaths = findPackagePaths(); } if (typeof packagePaths === "string") { normalizedOptions.packagePaths.push(packagePaths); } if (Array.isArray(packagePaths)) { normalizedOptions.packagePaths.push(...packagePaths.filter((item) => typeof item === "string")); } return normalizedOptions; }, "normalizeOptions"); // src/lib/find-deps.ts var import_jsonfile = __toESM(require("jsonfile")); var collectDepsToExclude = /* @__PURE__ */ __name((options) => { const depKeys = [ options.withDeps ? "dependencies" : void 0, options.withDevDeps ? "devDependencies" : void 0, options.withPeerDeps ? "peerDependencies" : void 0, options.withOptDeps ? "optionalDependencies" : void 0 ].filter((item) => !!item); return options.packagePaths.map((packagePath) => { let parsedPackageJsonData; try { parsedPackageJsonData = import_jsonfile.default.readFileSync(packagePath, { encoding: "utf-8" }); } catch (error) { console.error(error); throw new Error(`Read json file ${packagePath} failed with error above.`); } return depKeys.map((key) => parsedPackageJsonData[key] ? Object.keys(parsedPackageJsonData[key]) : []).flat().filter((packageName) => !options.include.includes(packageName)); }).flat(); }, "collectDepsToExclude"); // src/lib/esbuild-plugin-node-externals.ts var nodeExternals = /* @__PURE__ */ __name((options = {}) => ({ name: "plugin:node-externals", setup(build) { const normalizedOptions = normalizeOptions(options); const depsToExclude = collectDepsToExclude(normalizedOptions); build.onResolve({ namespace: "file", filter: /.*/ }, ({ path: path3 }) => { if (normalizedOptions.include.includes(path3)) { return null; } const [mainModuleOrScope, subModuleOrMainModule] = path3.split("/"); let moduleName = mainModuleOrScope; if (path3.startsWith("@")) { moduleName = `${mainModuleOrScope}/${subModuleOrMainModule}`; } if (depsToExclude.includes(moduleName)) { return { path: path3, external: true }; } return null; }); } }), "nodeExternals"); // src/index.ts var src_default = nodeExternals; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { nodeExternals });