esbuild构建不会捆绑公共目录。

3

我决定使用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
以上的解决方案仍然依赖于位于根目录上的“public”目录。 - Terchila Marian
2个回答

1

0
只需将@fastify/swagger和@fastify/swagger-ui添加到外部即可。
const esbuild = require("esbuild");

esbuild.build({
  entryPoints: ["src/app.ts"],
  platform: "node",
  bundle: true,
  minify: true,
  outfile: "built/index.js",
  external: ["@fastify/swagger", "@fastify/swagger-ui"],
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接