Skip to content

Commit c054e25

Browse files
salomaosnfflovell
andcommitted
Add dual support for ESM and CommonJS
- Convert from CommonJS to ESM - Generate CommonJS from ESM at build time - Test using CommonJS - Remains compatible with Node.js 14 onwards Co-authored-by: Lovell Fuller <[email protected]>
1 parent 686e441 commit c054e25

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ coverage
22
node_modules
33
package-lock.json
44
.nyc_output
5+
lib/limax.cjs

lib/index.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./limax.cjs").default;

lib/limax.js renamed to lib/limax.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33

44
'use strict';
55

6-
const speakingurl = require('speakingurl');
7-
const hepburn = require('hepburn');
8-
const deburr = require('lodash.deburr');
9-
const { pinyin } = require('pinyin-pro');
6+
import speakingurl from 'speakingurl';
7+
import hepburn from 'hepburn';
8+
import deburr from 'lodash.deburr';
9+
import { pinyin } from 'pinyin-pro';
1010

1111
function customCharsAsArray (custom) {
1212
custom = custom || [];
1313
return Array.isArray(custom) ? custom : Object.keys(custom);
1414
}
1515

16-
module.exports = function (text, opt) {
16+
export default function limax (text, opt) {
1717
const options = typeof opt === 'string'
1818
? { separator: opt }
1919
: opt || {};
@@ -70,4 +70,4 @@ module.exports = function (text, opt) {
7070
return mergeDigitSuffixes
7171
? slug.replace(/([^0-9])-([0-9])/g, '$1$2')
7272
: slug;
73-
};
73+
}

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"description": "Node.js module to generate URL slugs. Another one? This one cares about i18n and transliterates non-Latin scripts to conform to the RFC3986 standard. Mostly API-compatible with similar modules.",
77
"repository": {
88
"type": "git",
9-
"url": "git://github.com/lovell/limax"
9+
"url": "git://github.com/lovell/limax.git"
1010
},
1111
"engines": {
1212
"node": ">=14"
@@ -16,8 +16,14 @@
1616
"index.d.ts",
1717
"lib/"
1818
],
19+
"exports": {
20+
"import": "./lib/limax.mjs",
21+
"require": "./lib/index.cjs"
22+
},
23+
"type": "module",
1924
"scripts": {
20-
"test": "semistandard && nyc --reporter=html ava",
25+
"build": "esbuild lib/limax.mjs --format=cjs --outfile=lib/limax.cjs",
26+
"test": "semistandard lib/limax.mjs && npm run build && nyc --reporter=html ava test/unit.cjs",
2127
"changelog": "conventional-changelog -i CHANGELOG.md -s"
2228
},
2329
"keywords": [
@@ -55,6 +61,7 @@
5561
"devDependencies": {
5662
"ava": "^5.2.0",
5763
"conventional-changelog-cli": "^2.2.2",
64+
"esbuild": "^0.25.10",
5865
"nyc": "^15.1.0",
5966
"semistandard": "^16.0.1"
6067
}

test/unit.js renamed to test/unit.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'use strict';
55

66
const ava = require('ava');
7-
const limax = require('../lib/limax');
7+
const limax = require('../lib/index');
88

99
const tests = {
1010
'i ♥ latin': 'i-love-latin',

0 commit comments

Comments
 (0)