Skip to content

Commit

Permalink
Add CommonJS Backward compatibility entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Aug 29, 2024
1 parent 39dc126 commit 6d8ebf1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ The [`music-metadata`](https://github.com/Borewit/music-metadata) module is idea
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)
This module requires a [Node.js ≥ 16](https://nodejs.org/en/about/previous-releases) engine.
It can also be used in a browser environment when bundled with a module bundler.

Expand Down Expand Up @@ -764,6 +767,23 @@ Dependency list:
- [@tokenizer-token](https://github.com/Borewit/tokenizer-token)
- [peek-readable](https://github.com/Borewit/peek-readable)

## CommonJS Backward compatibility

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

(async () => {
// Dynamically loads the ESM module in a CommonJS project
const mm = await loadMusicMetadata();

const metadata = await mm.parseFile('/path/to/your/file');
})();

```

> [!NOTE]
> The `loadMusicMetadata` function is experimental and is not currently covered by any TypeScript typings.
## Frequently Asked Questions

Expand Down
5 changes: 5 additions & 0 deletions lib/default.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// CommonJS core (default) entry point
"use strict";
module.exports = {
loadMusicMetadata: () => import('./core.js'),
};
5 changes: 5 additions & 0 deletions lib/node.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// CommonJS Node entry point
"use strict";
module.exports = {
loadMusicMetadata: () => import('./index.js'),
};
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@
"type": "module",
"exports": {
".": {
"node": "./lib/index.js",
"default": "./lib/core.js"
"node": {
"import": "./lib/index.js",
"require": "./lib/node.cjs",
"types": "./lib/index.d.ts"
},
"default": {
"import": "./lib/core.js",
"require": "./lib/default.cjs",
"types": "./lib/core.d.ts"
}
}
},
"types": "lib/index.d.ts",
"files": [
"lib/**/*.js",
"lib/**/*.d.ts"
"lib/**/*.d.ts",
"lib/*.cjs"
],
"keywords": [
"music",
Expand Down

0 comments on commit 6d8ebf1

Please sign in to comment.