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 support for esm named exports #325

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: node_js

node_js:
- node
- 14
- 12
- 10

Expand Down
50 changes: 50 additions & 0 deletions esm-wrapper.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import semver from './index.js'

export const {
re,
src,
tokens,
SEMVER_SPEC_VERSION,
SemVer,
compareIdentifiers,
rcompareIdentifiers,
parse,
valid,
clean,
inc,
diff,
major,
minor,
patch,
prerelease,
compare,
rcompare,
compareLoose,
compareBuild,
sort,
rsort,
gt,
lt,
eq,
neq,
gte,
lte,
cmp,
coerce,
Comparator,
Range,
satisfies,
toComparators,
maxSatisfying,
minSatisfying,
minVersion,
validRange,
outside,
gtr,
ltr,
intersects,
simplifyRange,
subset
} = semver;

export default semver
5 changes: 4 additions & 1 deletion map.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
module.exports = testFile => testFile.replace(/test\//, '')
module.exports = testFile => {
const base = testFile.replace(/test\//, '').replace(/\.m?js$/, '')
return [`${base}.js`, `${base}.mjs`]
}
25 changes: 24 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
"version": "7.3.5",
"description": "The semantic version parser used by npm.",
"main": "index.js",
"exports": {
".": {
"require": "./index.js",
"import": "./esm-wrapper.mjs"
},
"./bin/*": "./bin/*",
"./classes/*": "./classes/*",
"./functions/*": "./functions/*",
"./internal/*": "./internal/*",
"./ranges/*": "./ranges/*",
"./bin/": "./bin/",
"./classes/": "./classes/",
"./functions/": "./functions/",
"./internal/": "./internal/",
"./ranges/": "./ranges/",
"./index.js": "./index.js",
"./index": "./index.js",
"./preload.js": "./preload.js",
"./preload": "./preload.js",
"./package.json": "./package.json",
"./package": "./package.json"
},
"scripts": {
"test": "tap",
"snap": "tap",
Expand Down Expand Up @@ -30,7 +52,8 @@
],
"tap": {
"check-coverage": true,
"coverage-map": "map.js"
"coverage-map": "map.js",
"test-ignore": "test/esm-wrapper.mjs"
},
"engines": {
"node": ">=10"
Expand Down
12 changes: 12 additions & 0 deletions test/esm-wrapper.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// test/esm/test-exports.mjs
import {createRequire} from 'module'
import t from 'tap'

import * as semver from 'semver'

const require = createRequire(import.meta.url)

const cjs = require('semver')

t.equal(cjs, semver.default)
t.same(cjs, Object.fromEntries(Object.entries(semver).filter(nvp => nvp[0] !== 'default')))
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ t.match(Object.getOwnPropertyDescriptor(semver, 'SEMVER_SPEC_VERSION'), {
configurable: true,
enumerable: true
}, 'just a normal value property')

if (semver.gte(process.version, '14.0.0')) {
t.spawn(process.execPath, require.resolve('./esm-wrapper.mjs'))
}