Skip to content

Commit

Permalink
fix: export cjs and es (agenda#1298)
Browse files Browse the repository at this point in the history
* fix: export cjs and es

* fix: export cjs and es renamings and add to doc

* fix: use index (=module) entrypoint for es.js

* docs: add migration note

* Add test for import vs require(). Make sure cjs can be required via destructuring. Update README a bit.

Co-authored-by: vasyl <[email protected]>
  • Loading branch information
simllll and koresar authored May 1, 2021
1 parent ae0a869 commit 849b32f
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ Install via NPM

You will also need a working [Mongo](https://www.mongodb.com/) database (v3) to point it to.

# CJS / Module Imports

for regular javascript code, just use the default entrypoint
```js
const Agenda = require('agenda');
```

For Typescript, Webpack or other module imports, use `agenda/es` entrypoint:
e.g.
```ts
import { Agenda } from 'agenda/es';
```
***NOTE***: If you're migrating from `@types/agenda` you also should change imports to `agenda/es`.
Instead of `import Agenda from 'agenda'` use `import Agenda from 'agenda/es'`.

# Example Usage

```js
Expand Down
1 change: 1 addition & 0 deletions es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/index')
5 changes: 5 additions & 0 deletions lib/cjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// old common js export (see index.ts for module exports)

import { Agenda } from "./agenda";
module.exports = Agenda;
module.exports.Agenda = Agenda;
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// module export, beware: cjs.ts is exported as main entry point!
export * from "./agenda";
export * from "./job";

Expand All @@ -7,4 +8,3 @@ export { JobOptions } from "./job/repeat-every";
import { Agenda } from "./agenda";
export { Agenda };
export default Agenda;

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "agenda",
"version": "4.1.2",
"description": "Light weight job scheduler for Node.js",
"main": "dist/index.js",
"main": "dist/cjs.js",
"types": "dist/index.d.ts",
"files": [
"dist"
Expand Down Expand Up @@ -65,6 +65,7 @@
"delay": "5.0.0",
"eslint": "7.18.0",
"eslint-config-prettier": "8.1.0",
"esm": "^3.2.25",
"expect.js": "0.3.1",
"husky": "5.1.1",
"jsdoc": "3.6.6",
Expand Down
11 changes: 11 additions & 0 deletions test/exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
describe("Exports", () => {
it("should export both default Agenda and { Agenda }", () => {
require("./imports/require.js");
require("./imports/require-es.js");

// eslint-disable-next-line @typescript-eslint/no-var-requires
const requireEsm = require("esm")(module /*, options*/);
requireEsm("./imports/import.js");
requireEsm("./imports/import-es.js");
});
});
5 changes: 5 additions & 0 deletions test/imports/import-es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import assert from "assert";

import Agenda1 from "../../es";
import { Agenda as Agenda2 } from "../../es";
assert.strictEqual(Agenda1.constructor, Agenda2.constructor); // comparing ctors, as Agenda1 is a JS Proxy in `esm`
5 changes: 5 additions & 0 deletions test/imports/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import assert from "assert";

import Agenda1 from "../..";
import { Agenda as Agenda2 } from "../..";
assert.strictEqual(Agenda1.constructor, Agenda2.constructor); // comparing ctors, as Agenda1 is a JS Proxy in `esm`
5 changes: 5 additions & 0 deletions test/imports/require-es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const assert = require("assert");

const Agenda1 = require("../../es").default;
const { Agenda: Agenda2 } = require("../../es");
assert.strictEqual(Agenda1, Agenda2);
5 changes: 5 additions & 0 deletions test/imports/require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const assert = require("assert");

const Agenda1 = require("../..");
const { Agenda: Agenda2 } = require("../..");
assert.strictEqual(Agenda1, Agenda2);

0 comments on commit 849b32f

Please sign in to comment.