Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CommonJS entry point for backwards compatibility #981

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/nodejs-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Enable Corepack
run: corepack enable

Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ A MusicBrainz-API-client for reading and submitting metadata.
Module: version 8 migrated from [CommonJS](https://en.wikipedia.org/wiki/CommonJS) to [pure ECMAScript Module (ESM)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
The distributed JavaScript codebase is compliant with the [ECMAScript 2020 (11th Edition)](https://en.wikipedia.org/wiki/ECMAScript_version_history#11th_Edition_%E2%80%93_ECMAScript_2020) standard.

> [!NOTE]
> See also [CommonJS backward compatibility](#commonjs-backward-compatibility)

### Requirements
- Node.js: Requires [Node.js version 16](https://nodejs.org/en/about/previous-releases) or higher.
- Browser: Can be used in browser environments when bundled with a module bundler (not actively tested).
Expand Down Expand Up @@ -68,6 +71,9 @@ const mbApi = new MusicBrainzApi({
});
```

> [!NOTE]
> See also [CommonJS backward compatibility](#commonjs-backward-compatibility)

### Configuration Options

```js
Expand Down Expand Up @@ -465,3 +471,29 @@ async function getCoverArt(releaseGroupMbid, coverType = '') {

```

## CommonJS backward compatibility

For legacy CommonJS projects needing to load the `music-metadata` ESM module, you can use the `loadMusicMetadata` function:
```js
const { loadMusicBrainzApi } = require('musicbrainz-api');

(async () => {

// Dynamically loads the ESM module in a CommonJS project
const {MusicBrainzApi} = await loadMusicBrainzApi();

const mbApi = new MusicBrainzApi({
appName: 'my-app',
appVersion: '0.1.0',
appContactInfo: 'user@mail.org',
});

const releaseList = await mbApi.search('release', {query: {barcode: 602537479870}});
for(const release of releaseList.releases) {
console.log(release.title);
}
})();
```

> [!NOTE]
> The `loadMusicMetadata` function is experimental.
5 changes: 5 additions & 0 deletions lib/default.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// CommonJS entry point
"use strict";
module.exports = {
loadMusicBrainzApi: () => import('./index.js'),
};
5 changes: 5 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
export * from './coverartarchive-api.js';
export * from './musicbrainz-api.js';

/**
* CommonJS (only) function to load `musicbrainz-api` ESM module
*/
export declare function loadMusicBrainzApi(): Promise<typeof import('musicbrainz-api')>;
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
"name": "musicbrainz-api",
"version": "0.18.2",
"description": "MusicBrainz API client for reading and submitting metadata",
"exports": "./lib/index.js",
"exports": {
"import": "./lib/index.js",
"require": "./lib/default.cjs"
},
"types": "lib/index.d.ts",
"files": [
"lib/**/*.js",
"lib/**/*.d.ts"
"lib/**/*.d.ts",
"lib/default.cjs"
],
"type": "module",
"author": {
Expand Down
Loading