Skip to content

Commit 6ae16ee

Browse files
committed
chore: Switches to unbuild and updates exports for proper tree shaking
1 parent 1bf28de commit 6ae16ee

24 files changed

+1446
-77
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Until [this ava issue](https://github.com/avajs/ava/issues/2979) is resolved, we
4646

4747
### 🏁 E2E Testing
4848

49-
End to End tests are accepted. We use [mongo-memory-server](https://github.com/nodkz/mongodb-memory-server) in development to provide a MongoDB instance with working replication (which enables the oplog).
49+
End to End tests are accepted. Please use the `LokiAdapter` for any tests, as it does not mandate the external dependencies to be loaded.
5050

5151
## 📚 Updating Documentation
5252

@@ -64,7 +64,7 @@ To help land your contribution, please make sure of the following:
6464

6565
- Remember to be concise in your Conventional Commit. These will eventually be automatically rolled up into an auto-generated CHANGELOG file
6666
- If you modified anything in `src/`:
67-
- You verified the transpiled TypeScript with `yarn build` in the directory of whichever package you modified.
67+
- You verified the transpiled TypeScript with `yarn build` in the directory of whichever package you modified. This will also verify your CJS/ESM exports
6868
- Run `yarn test` to ensure all existing tests pass for that package, along with any new tests you would've written.
6969

7070
Thank you! 💕

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ The `Queue` object has a large number of emitted events available through `queue
156156
DocMQ works with several drivers, many of which are included in the `/drivers` directory. For development or non-production scenarios, we recommend the `MemoryDriver`, an in-memory driver that supports all of DocMQ's apis. When transitioning to production, you can pass a production driver in and DocMQ will work with no additional changes.
157157

158158
```ts
159+
import { Queue, MemoryDriver } from "docmq";
160+
import { MongoDriver } from "docmq/driver/mongo";
161+
159162
// for example, using the MongoDriver in production, while using
160163
// the less resource-intensive in-memory driver for development
161164
const driver =
@@ -166,12 +169,14 @@ const driver =
166169
const queue = new Queue(driver, "queueName");
167170
```
168171

169-
| Driver | `import` | Notes |
170-
| :------------ | :------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
171-
| **In Memory** | `MemoryDriver` | The default in-memory driver and (currently) a re-export of `LokiDriver` |
172-
| LokiJS | `LokiDriver` | A fast in-memory driver designed for non-production instances. |
173-
| MongoDB | `MongoDriver` | Currently, DocMQ requires a Mongo Client >= 4.2 for transaction support, and the mongo instance must be running in a Replica Set. This is because MongoDriver uses the OpLog to reduce polling. Requires [mongodb](https://www.npmjs.com/package/mongodb) peer dependency if using |
174-
| Postgres | `PGDriver` | We are slowly expanding our PG Test Matrix based on what GitHub allows. `LISTEN`/`NOTIFY` support is not available, and the driver will fall back to polling. Requires [pg](https://www.npmjs.com/package/pg) as a peer dependency if using |
172+
| Driver | `import` | Notes |
173+
| :------------ | :------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
174+
| **In Memory** | `import { MemoryDriver} from "docmq"` | The default in-memory driver and (currently) a re-export of `LokiDriver` |
175+
| LokiJS | `import { LokiDriver } from "docmq/driver/loki"` | A fast in-memory driver designed for non-production instances. |
176+
| MongoDB | `import { MongoDriver } from "docmq/driver/mongo"` | Currently, DocMQ requires a Mongo Client >= 4.2 for transaction support, and the mongo instance must be running in a Replica Set. This is because MongoDriver uses the OpLog to reduce polling. Requires [mongodb](https://www.npmjs.com/package/mongodb) peer dependency if using |
177+
| Postgres | `import { PGDriver } from "docmq/driver/postgres"` | We are slowly expanding our PG Test Matrix based on what GitHub allows. `LISTEN`/`NOTIFY` support is not available, and the driver will fall back to polling. Requires [pg](https://www.npmjs.com/package/pg) as a peer dependency if using |
178+
179+
If you need to write a custom driver, the core `BaseDriver` is available in the core `docmq` package.
175180

176181
## :pencil2: Contributing
177182

RELEASING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
This repository releases code using [release-it](https://github.com/release-it/release-it). New releases can be triggered from the root package via `yarn rel` which offers a guided process.
44

5+
As part of our build command, we perform a local npm install and verify that our CJS/ESM imports are working as expected.
6+
57
## Common Commands
68

79
- Begin a new for the next version `next` with `yarn rel <major|minor|patch> --preRelease=next`

build.config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineBuildConfig } from "unbuild";
2+
3+
export default defineBuildConfig({
4+
clean: true,
5+
declaration: true,
6+
rollup: {
7+
cjsBridge: true,
8+
emitCJS: true,
9+
},
10+
});

package.json

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,11 @@
99
"repository": "jakobo/docmq.git",
1010
"scripts": {
1111
"bench": "node --loader=ts-node/esm ./scripts/run-benchmark.ts",
12-
"build": "run-s clean 'build:all' 'pjson:all'",
13-
"build:all": "run-p 'build:x:**'",
14-
"build:x:cjs": "tsc -p tsconfig.cjs.json",
15-
"build:x:esm": "tsc -p tsconfig.esm.json",
16-
"build:x:types": "tsc -p tsconfig.types.json",
12+
"build": "run-s unbuild 'verify:*'",
1713
"clean": "shx rm -rf dist && shx mkdir dist",
1814
"dev": "run-s watch",
1915
"lint": "eslint .",
2016
"lint-staged": "lint-staged",
21-
"pjson:all": "run-p 'pjson:x:**'",
22-
"pjson:x:cjs": "shx echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json",
23-
"pjson:x:esm": "shx echo '{\"type\": \"module\"}' > ./dist/esm/package.json",
2417
"postinstall": "husky install",
2518
"postpack": "pinst --enable",
2619
"prepack": "pinst --disable",
@@ -29,10 +22,9 @@
2922
"rev": "release-it --preRelease",
3023
"syncpack": "syncpack",
3124
"test": "ava",
32-
"watch": "run-p 'watch:**'",
33-
"watch:cjs": "tsc -p tsconfig.cjs.json -w --preserveWatchOutput",
34-
"watch:esm": "tsc -p tsconfig.esm.json -w --preserveWatchOutput",
35-
"watch:types": "tsc -p tsconfig.types.json -w --preserveWatchOutput"
25+
"unbuild": "unbuild",
26+
"verify:cjs": "cd verify/cjs && npm install && node cjs.verify.js",
27+
"verify:esm": "cd verify/esm && npm install && node esm.verify.js"
3628
},
3729
"engines": {
3830
"node": ">=14.19.0"
@@ -84,7 +76,8 @@
8476
"testdouble": "^3.16.6",
8577
"ts-node": "^10.8.1",
8678
"tslib": "^2.4.0",
87-
"typescript": "^4.7.3"
79+
"typescript": "^4.7.3",
80+
"unbuild": "latest"
8881
},
8982
"peerDependencies": {
9083
"mongodb": "^4.7.0",
@@ -99,15 +92,33 @@
9992
}
10093
},
10194
"type": "module",
102-
"main": "dist/cjs/src/index.js",
103-
"module": "dist/esm/src/index.js",
104-
"types": "dist/types/src/index.d.ts",
95+
"main": "dist/index.cjs",
96+
"module": "dist/index.mjs",
97+
"types": "dist/index.d.ts",
10598
"exports": {
106-
"./package.json": "./package.json",
10799
".": {
108-
"types": "./dist/types/src/index.d.ts",
109-
"import": "./dist/esm/src/index.js",
110-
"require": "./dist/cjs/src/index.js"
100+
"types": "./dist/index.d.ts",
101+
"import": "./dist/index.mjs",
102+
"require": "./dist/index.cjs",
103+
"file": "./src/index.ts"
104+
},
105+
"./driver/mongo": {
106+
"types": "./dist/mongo.d.ts",
107+
"import": "./dist/mongo.mjs",
108+
"require": "./dist/mongo.cjs",
109+
"file": "./src/driver/mongo.ts"
110+
},
111+
"./driver/loki": {
112+
"types": "./dist/loki.d.ts",
113+
"import": "./dist/loki.mjs",
114+
"require": "./dist/loki.cjs",
115+
"file": "./src/driver/loki.ts"
116+
},
117+
"./driver/postgres": {
118+
"types": "./dist/postgres.d.ts",
119+
"import": "./dist/postgres.mjs",
120+
"require": "./dist/postgres.cjs",
121+
"file": "./src/driver/postgres.ts"
111122
}
112123
},
113124
"files": [

src/driver/loki.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { DateTime } from "luxon";
22
import Loki from "lokijs";
3-
import { serializeError } from "serialize-error";
43
import { v4 } from "uuid";
54
import { MaxAttemptsExceededError } from "../error.js";
65
import { QueueDoc } from "../types.js";
@@ -267,6 +266,9 @@ export class LokiDriver extends BaseDriver {
267266
`Exceeded the maximum number of retries (${doc.attempts.max}) for this job`
268267
);
269268

269+
// serialize-error is esm-only and must be await imported
270+
const serializeError = (await import("serialize-error")).serializeError;
271+
270272
const next = this._jobs
271273
.chain()
272274
.find({

src/driver/mongo.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
type ObjectId,
1010
type WithId,
1111
} from "mongodb";
12-
import { serializeError } from "serialize-error";
1312
import { v4 } from "uuid";
1413

1514
import {
@@ -398,6 +397,9 @@ export class MongoDriver extends BaseDriver {
398397
`Exceeded the maximum number of retries (${doc.attempts.max}) for this job`
399398
);
400399

400+
// serialize-error is esm-only and must be await imported
401+
const serializeError = (await import("serialize-error")).serializeError;
402+
401403
const next = await this._jobs.updateOne(
402404
{
403405
ack: ackVal,

src/driver/postrgres.ts renamed to src/driver/postgres.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { DateTime } from "luxon";
22
import pg from "pg";
3-
import { serializeError } from "serialize-error";
4-
53
import { MaxAttemptsExceededError } from "../error.js";
64
import { QueueDoc } from "../types.js";
75
import { BaseDriver } from "./base.js";
@@ -584,6 +582,9 @@ export class PgDriver extends BaseDriver {
584582
`Exceeded the maximum number of retries (${doc.attempts.max}) for this job`
585583
);
586584

585+
// serialize-error is esm-only and must be await imported
586+
const serializeError = (await import("serialize-error")).serializeError;
587+
587588
const client = await this._pool.connect();
588589
try {
589590
const res = await client.query<QueueRow>(

src/driver/postgres/migrations/.empty

Whitespace-only changes.

src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
export { Queue } from "./queue.js";
22
export { BaseDriver } from "./driver/base.js";
3-
export { MongoDriver } from "./driver/mongo.js";
4-
export { LokiDriver } from "./driver/loki.js";
53
export { LokiDriver as MemoryDriver } from "./driver/loki.js";
64

75
export * from "./error.js";

0 commit comments

Comments
 (0)