我决定使用esbuild
来打包我的express API。到目前为止一切都很好,除了保存swagger
UI的public
目录没有与应用程序的其余部分一起被打包。
我定义了一个静态路由作为终点,它服务于该文件夹。
app.use(express.static(`${root}/public`));
为了解决这个问题,我尝试了多种方法,例如手动将
public目录
复制到build目录
位置,但似乎没有起作用。我还尝试了使用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));
.html
文件添加到entryPoints数组中,这似乎可以完成工作。 - Terchila Marian