esbuild-plugin-node-externals
Version:
ESBuild plugin for node externals.
117 lines (109 loc) • 3.96 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/lib/find-package-path.ts
import path2 from "path";
import findUp from "find-up";
// src/lib/is-in-dir.ts
import path from "path";
var isInDirectory = /* @__PURE__ */ __name((parent, child) => {
const relativePath = path.relative(parent, child);
return !relativePath.startsWith("..") && !path.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 = findUp.sync(".git", {
type: "directory"
});
const gitRootPath = gitDirectoryPath === void 0 ? void 0 : path2.dirname(gitDirectoryPath);
let cwd = process.cwd();
let packagePath;
const packagePaths = [];
while ((packagePath = findUp.sync("package.json", {
type: "file",
cwd
})) && isInGitDirectory(packagePath, gitRootPath)) {
packagePaths.push(packagePath);
cwd = path2.dirname(path2.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
import jsonfile from "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 = jsonfile.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;
export {
src_default as default,
nodeExternals
};