apache / apache/pulsar-client-node
Problems with using a bundler (esbuild, webpack, etc)
- Ngôn ngữ chính
- C++
- Star
- 164
- Fork
- 98
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
We haven't been able to get pulsar-client to work with a bundler like webpack or esbuild. The `pulsar.node` file is not picked up by the bundler and additional files in the library (.html, .cs) end up breaking the build. Even if the bundler is configured to ignore these html/cs files, it fails to export `pulsar.node` file from node_modules into the build directory.
An alternative path is to get webpack/esbuild to ignore `pulsar-client` completely and to manually copy over `pulsar.node` from node_modules. This ends up causing problems because [pulsar-binding.js](https://github.com/apache/pulsar-client-node/blob/master/src/pulsar-binding.js) explicitly looks for the package.json file and for mapbox/node-pre-gyp:
```
const binary = require('@mapbox/node-pre-gyp');
const bindingPath = binary.find(path.resolve(path.join(__dirname, '../package.json')));
```
So it seems like pulsar-client would need to be architected differently to work with webpack/esbuild.
We did finally find a workaround to the problem. Sharing it here for other people who run into this too: we use esbuild to build and package up our node.js app but we add an additional step that creates a simple package.json file with pulsar-client in the parent directory and then run `npm install`. This way we can continue using esbuild to package up our node app but externally inject pulsar-client as a dependency.
Here's an example build.js for esbuild:
```
const esbuild = require("esbuild");
const { copy } = require("esbuild-plugin-copy");
const fs = require("fs-extra");
const path = require("path");
const { execSync } = require("child_process");
// Ensure that native modules, like node-canvas, are bundled correctly
// This however does not work for pulsar-client, see the then() block below
const nativeNodeModulesPlugin = {
name: "native-node-modules",
setup(build) {
build.onResolve({ filter: /\.node$/, namespace: "file" }, (args) => ({
path: require.resolve(args.path, { paths: [args.resolveDir] }),
namespace: "node-file",
}));
build.onLoad({ filter: /.*/, namespace: "node-file" }, (args) => ({
contents: `
import path from ${JSON.stringify(args.path)}
try { module.exports = require(path) }
catch {}
`,
}));
build.onResolve({ filter: /\.node$/, namespace: "node-file" }, (args) => ({
path: args.path,
namespace: "file",
}));
const opts = build.initialOptions;
opts.loader = opts.loader || {};
opts.loader[".node"] = "file";
},
};
esbuild
.build({
entryPoints: ["src/index.ts"],
plugins: [nativeNodeModulesPlugin],
bundle: true,
platform: "node",
target: "node18",
outdir: "build/src",
tsconfig: "tsconfig.json",
sourcemap: true,
// Ignore pulsar-client and related dependencies. These will be installed in the build directory later.
external: ["*.html", "mock-aws-s3", "aws-sdk", "nock", "pulsar-client"],
})
.then(() => {
// pulsar-client does not work when bundled so we install it in the build
// directory as a dependency.
// Get the version of pulsar-client that we are using
const pulsarVersion = require("pulsar-client/package.json").version;
// Create a package.json file in the build directory with pulsar-client as a dependency
const packageJson = {
name: "external-deps",
version: "1.0.0",
dependencies: {
"pulsar-client": `^${pulsarVersion}`,
},
};
// Write the package.json file to the build directory and install the dependencies
fs.writeFileSync(path.join("build", "package.json"), JSON.stringify(packageJson, null, 2));
execSync("cd build && npm install --no-package-lock");
})
.catch(() => process.exit(1));
```
We then launch our app: `cd build && node ./src/index.js`
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện lỗi bundling với webpack hoặc esbuild, tập trung vào src/pulsar-binding.js và việc @mapbox/node-pre-gyp tìm ../package.json. Theo dõi cách pulsar.node và các tệp HTML/CS bổ sung được xử lý từ node_modules. Công việc được xem là hoàn tất khi một đường dẫn bundler đã được ghi lại hoặc kiểm thử có thể đóng gói và tải pulsar-client mà không cần workaround cài đặt bên ngoài.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- javascript, nodejs, webpack
- Lĩnh vực
- build-system
- Loại issue
- Lỗi
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100