我决定使用esbuild捆绑我的快递API。到目前为止,除了包含public UI的swagger目录没有绑定到应用程序的其余部分之外,一切都很好。我有一个端点定义为服务于该文件夹的静态路由。
app.use(express.static(`${root}/public`));为了克服这个问题,我尝试了多种方法,例如手动将public directory复制到build dir位置。这似乎行不通。我也尝试过插件esbuild-plugin-public-directory,它也没有工作。
然后,我将public/index.html添加到entryPoints配置中,这也不起作用。
我现在的问题是,我做错了什么?在互联网上,我似乎没有发现任何特别有用的东西来帮助我克服这个问题。
这是我目前正在使用的esbuild。
const publicDir = require("esbuild-plugin-public-directory");
const OUTDIR = "build";
const envPlugin = {
name: "env",
setup(build) {
// Intercept import paths called "env" so esbuild doesn't attempt
// to map them to a file system location. Tag them with the "env-ns"
// namespace to reserve them for this plugin.
build.onResolve({ filter: /^env$/ }, (args) => ({
path: args.path,
namespace: "env-ns",
}));
// Load paths tagged with the "env-ns" namespace and behave as if
// they point to a JSON file containing the environment variables.
build.onLoad({ filter: /.*/, namespace: "env-ns" }, () => ({
contents: JSON.stringify(process.env),
loader: "json",
}));
},
};
require("esbuild")
.build({
entryPoints: ["server/start.ts", "public/index.html"],
platform: "node",
bundle: true,
minify: false,
platform: "node",
logLevel: "info",
sourcemap: false,
target: "node12",
loader: {
'.html': 'text',
},
outdir: "build",
plugins: [envPlugin, publicDir()],
})
.then(() => {
fs.copyFileSync("server/common/api.yml", `${OUTDIR}/api.yml`);
console.log(`Successfully built, output directed to the ${OUTDIR} directory`);
})
.catch(() => process.exit(1));发布于 2022-09-23 14:00:49
如果使用@fastify/swagger的话,我试过了。只需将@fastify/swagger放在esbuild build.external中即可。
buid({external: ["esnext", "@fastify/swagger"],})否则的话,你可能会摆出一副自以为是的样子。参考资料:https://docs.devland.is/repository/openapi#configuring-swaggerui-dependencies-for-esbuild
https://stackoverflow.com/questions/73785604
复制相似问题