@kya-os/mcp-i
Version:
The TypeScript MCP framework with identity features built-in
111 lines (110 loc) • 4.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWebpackConfig = getWebpackConfig;
const webpack_1 = require("webpack");
const path_1 = __importDefault(require("path"));
const constants_1 = require("../../utils/constants");
const compiler_context_1 = require("../compiler-context");
const get_entries_1 = require("./get-entries");
const get_injected_variables_1 = require("./get-injected-variables");
const resolve_tsconfig_paths_1 = require("./resolve-tsconfig-paths");
const plugins_1 = require("./plugins");
const fs_extra_1 = __importDefault(require("fs-extra"));
const get_externals_1 = require("./get-externals");
/** Creates the webpack configuration that xmcp will use to bundle the user's code */
function getWebpackConfig(xmcpConfig) {
const processFolder = process.cwd();
const { mode } = compiler_context_1.compilerContext.getContext();
const outputPath = xmcpConfig.experimental?.adapter
? constants_1.adapterOutputPath
: constants_1.distOutputPath;
const outputFilename = xmcpConfig.experimental?.adapter
? "index.js"
: "[name].js";
const config = {
mode,
watch: mode === "development",
devtool: false,
output: {
filename: outputFilename,
path: outputPath,
libraryTarget: "commonjs2",
},
target: "node",
externals: (0, get_externals_1.getExternals)(),
resolve: {
fallback: {
process: false,
},
alias: {
"node:process": "process",
"xmcp/headers": path_1.default.resolve(processFolder, ".xmcp/headers.js"),
...(0, resolve_tsconfig_paths_1.resolveTsconfigPathsToAlias)(),
},
extensions: [".tsx", ".ts", ".jsx", ".js", ".json"],
},
plugins: [new plugins_1.InjectRuntimePlugin(), new plugins_1.CreateTypeDefinitionPlugin()],
module: {
rules: [
{
test: /\.ts$/,
use: "swc-loader",
},
],
},
optimization: {
minimize: mode === "production",
splitChunks: false,
},
};
// Do not watch the adapter output folder, avoid infinite loop
if (mode === "development" && !xmcpConfig.experimental?.adapter) {
config.watchOptions = {
ignored: [constants_1.adapterOutputPath],
};
}
const providedPackages = {
// connects the user exports with our runtime
INJECTED_TOOLS: [
path_1.default.resolve(processFolder, ".xmcp/import-map.js"),
"tools",
],
INJECTED_MIDDLEWARE: [
path_1.default.resolve(processFolder, ".xmcp/import-map.js"),
"middleware",
],
};
// add entry points based on config
config.entry = (0, get_entries_1.getEntries)(xmcpConfig);
// add injected variables to config
config.plugins.push(new webpack_1.ProvidePlugin(providedPackages));
// add defined variables to config
const definedVariables = (0, get_injected_variables_1.getInjectedVariables)(xmcpConfig);
config.plugins.push(new webpack_1.DefinePlugin(definedVariables));
// add clean plugin
if (!xmcpConfig.experimental?.adapter) {
// not needed in adapter mode since it only outputs one file
// Simple cleanup plugin using fs-extra (replaces CleanWebpackPlugin)
config.plugins.push({
apply: (compiler) => {
compiler.hooks.beforeCompile.tap("CleanOutputPlugin", () => {
if (fs_extra_1.default.pathExistsSync(outputPath)) {
fs_extra_1.default.removeSync(outputPath);
}
});
},
});
}
// add shebang to CLI output on stdio mode
if (xmcpConfig.stdio) {
config.plugins.push(new webpack_1.BannerPlugin({
banner: "#!/usr/bin/env node",
raw: true,
include: /^stdio\.js$/,
}));
}
return config;
}