Skip to content

Commit 3a7bebd

Browse files
author
magne
committed
fix: build for node esm and cjs and example
1 parent 84c514e commit 3a7bebd

File tree

6 files changed

+121
-45
lines changed

6 files changed

+121
-45
lines changed

examples/package-lock.json

Lines changed: 44 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
6+
"type": "module",
67
"scripts": {
78
"start": "node index.js",
89
"rebuild-source": "cd .. && npm run build && cd - && npm install --force",
@@ -11,7 +12,8 @@
1112
"author": "",
1213
"license": "ISC",
1314
"dependencies": {
14-
"rabbitmq-amqp-js-client": "file:../."
15+
"rabbitmq-amqp-js-client": "file:../.",
16+
"rhea": "^3.0.4"
1517
},
1618
"engines": {
1719
"node": "22.x.x"

examples/websocket_example.js renamed to examples/socket_example.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
const rabbit = require("rabbitmq-amqp-js-client")
2-
const { randomUUID } = require("crypto")
1+
import { createEnvironment, createAmqpMessage, OutcomeState } from "rabbitmq-amqp-js-client"
2+
import { randomUUID } from "crypto"
33

4-
const rabbitUser = process.env.RABBITMQ_USER ?? "guest"
5-
const rabbitPassword = process.env.RABBITMQ_PASSWORD ?? "guest"
4+
const rabbitUser = process.env.RABBITMQ_USER ?? "rabbit"
5+
const rabbitPassword = process.env.RABBITMQ_PASSWORD ?? "rabbit"
66
const rabbitHost = process.env.RABBITMQ_HOSTNAME ?? "localhost"
7-
const rabbitPort = process.env.RABBITMQ_PORT ?? 15678
7+
const rabbitPort = process.env.RABBITMQ_PORT ?? 5672
88

99
async function main() {
1010
const testExchange = `test-exchange-${randomUUID()}`
1111
const testQueue = `test-queue-${randomUUID()}`
1212
const routingKey = `test-key-${randomUUID()}`
1313

1414
console.log("Creating the environment...")
15-
const environment = rabbit.createEnvironment({
15+
const environment = createEnvironment({
1616
host: rabbitHost,
1717
port: rabbitPort,
1818
username: rabbitUser,
1919
password: rabbitPassword,
20-
webSocket: WebSocket,
2120
})
2221

2322
console.log("Opening a connection...")
@@ -34,15 +33,15 @@ async function main() {
3433
console.log("Opening a publisher and publishing 10 messages...")
3534
const publisher = await connection.createPublisher({ exchange: { name: testExchange, routingKey: routingKey } })
3635
for (const i of Array(10).keys()) {
37-
const publishResult = await publisher.publish(rabbit.createAmqpMessage({ body: `Hello - ${i} - ` }))
36+
const publishResult = await publisher.publish(createAmqpMessage({ body: `Hello - ${i} - ` }))
3837
switch (publishResult.outcome) {
39-
case rabbit.OutcomeState.ACCEPTED:
38+
case OutcomeState.ACCEPTED:
4039
console.log("Message Accepted")
4140
break
42-
case rabbit.OutcomeState.RELEASED:
41+
case OutcomeState.RELEASED:
4342
console.log("Message Released")
4443
break
45-
case rabbit.OutcomeState.REJECTED:
44+
case OutcomeState.REJECTED:
4645
console.log("Message Rejected")
4746
break
4847
default:

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@
22
"name": "rabbitmq-amqp-js-client",
33
"version": "0.3.2",
44
"description": "Rabbit AMQP 1.0 client for JS/TS application",
5-
"main": "dist/index.cjs.js",
6-
"module": "dist/index.es.js",
5+
"main": "dist/index.cjs",
6+
"module": "dist/index.js",
77
"types": "dist/index.d.ts",
88
"type": "module",
99
"exports": {
1010
".": {
1111
"browser": "./dist/index.umd.js",
1212
"import": {
1313
"types": "./dist/index.d.ts",
14-
"default": "./dist/index.es.js"
14+
"default": "./dist/index.js"
1515
},
1616
"require": {
17-
"default": "./dist/index.cjs.js"
17+
"default": "./dist/index.cjs"
1818
},
19-
"default": "./dist/index.es.js"
19+
"default": "./dist/index.js"
2020
},
2121
"./dist/*": {
2222
"types": "./dist/*.d.ts",
23-
"import": "./dist/*.es.js",
24-
"require": "./dist/*.cjs.js"
23+
"import": "./dist/*.js",
24+
"require": "./dist/*.cjs"
2525
}
2626
},
2727
"scripts": {
2828
"test": "vitest run",
2929
"build": "npm run build:node && npm run build:browser",
30-
"build:node": "vite build --config vite.config.ts",
30+
"build:node": "vite build --config vite.config.cjs.ts && vite build --config vite.config.esm.ts",
3131
"build:browser": "vite build --config vite.config.browser.ts",
3232
"build:clean": "rm -rf dist && npm run build",
3333
"check": "npm run check-ts && npm run check-lint && npm run check-format && npm run check-spell",

vite.config.cjs.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { defineConfig } from "vite"
2+
import { resolve } from "path"
3+
4+
export default defineConfig({
5+
plugins: [],
6+
build: {
7+
lib: {
8+
entry: resolve(__dirname, "src/index.ts"),
9+
name: "RabbitmqAmqpClient",
10+
formats: ["cjs"],
11+
fileName: () => `index.cjs`,
12+
},
13+
rollupOptions: {
14+
external: ["rhea", "crypto"],
15+
output: {
16+
globals: {
17+
rhea: "rhea",
18+
},
19+
},
20+
},
21+
outDir: "dist",
22+
emptyOutDir: false,
23+
sourcemap: true,
24+
minify: false,
25+
target: "node16",
26+
},
27+
resolve: {
28+
alias: {
29+
"@": resolve(__dirname, "src"),
30+
},
31+
},
32+
})
Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
import { defineConfig } from "vite"
22
import { resolve } from "path"
3-
import { nodePolyfills } from "vite-plugin-node-polyfills"
43
import dts from "vite-plugin-dts"
54

65
export default defineConfig({
76
plugins: [
8-
nodePolyfills({
9-
globals: {
10-
Buffer: true,
11-
global: true,
12-
process: true,
13-
},
14-
protocolImports: true,
15-
}),
167
dts({
178
include: ["src/**/*"],
189
exclude: ["src/**/*.test.ts", "src/**/*.spec.ts", "test/**/*"],
@@ -25,29 +16,41 @@ export default defineConfig({
2516
lib: {
2617
entry: resolve(__dirname, "src/index.ts"),
2718
name: "RabbitmqAmqpClient",
28-
formats: ["es", "cjs"],
29-
fileName: (format) => `index.${format}.js`,
19+
formats: ["es"],
20+
fileName: () => `index.js`,
3021
},
3122
rollupOptions: {
32-
external: ["rhea"],
23+
external: [
24+
"crypto",
25+
// Node.js built-in modules that rhea needs
26+
"net",
27+
"tls",
28+
"os",
29+
"path",
30+
"util",
31+
"events",
32+
"stream",
33+
"buffer",
34+
"fs",
35+
"dns"
36+
],
3337
output: {
34-
globals: {
35-
rhea: "rhea",
36-
},
38+
globals: {},
3739
},
3840
},
3941
outDir: "dist",
40-
emptyOutDir: true,
42+
emptyOutDir: false,
4143
sourcemap: true,
4244
minify: false,
43-
target: ["node16", "es2020"],
44-
commonjsOptions: {
45-
include: [/node_modules/],
46-
},
45+
target: "node16",
4746
},
4847
resolve: {
4948
alias: {
5049
"@": resolve(__dirname, "src"),
5150
},
5251
},
53-
})
52+
define: {
53+
// Ensure we're building for Node.js, not browser
54+
global: 'globalThis',
55+
},
56+
})

0 commit comments

Comments
 (0)