Skip to content

Commit bbcde76

Browse files
author
ZYSzys
authored
feat: support esm (#1474)
1 parent b7d8c97 commit bbcde76

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ coverage
44
npm-debug.log
55
.idea
66
*.iml
7+
dist

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ node_js:
33
- 8
44
- 10
55
- 12
6+
- 14
67
cache:
78
directories:
89
- wrk/bin
@@ -13,6 +14,7 @@ before_script:
1314
- export PATH=$PATH:$PWD/wrk/bin/
1415
script:
1516
- npm run lint
17+
- npm run prepack
1618
- npm run test-cov
1719
- npm run bench
1820
after_script:

package.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
"version": "2.12.1",
44
"description": "Koa web app framework",
55
"main": "lib/application.js",
6+
"exports": {
7+
".": {
8+
"require": "./lib/application.js",
9+
"import": "./dist/koa.mjs"
10+
},
11+
"./": "./"
12+
},
613
"scripts": {
714
"test": "egg-bin test test",
815
"test-cov": "egg-bin cov test",
916
"lint": "eslint benchmarks lib test",
1017
"bench": "make -C benchmarks",
11-
"authors": "git log --format='%aN <%aE>' | sort -u > AUTHORS"
18+
"authors": "git log --format='%aN <%aE>' | sort -u > AUTHORS",
19+
"build": "gen-esm-wrapper . ./dist/koa.mjs",
20+
"prepack": "npm run build"
1221
},
1322
"repository": "koajs/koa",
1423
"keywords": [
@@ -55,13 +64,15 @@
5564
"eslint-plugin-node": "^10.0.0",
5665
"eslint-plugin-promise": "^4.2.1",
5766
"eslint-plugin-standard": "^4.0.1",
67+
"gen-esm-wrapper": "^1.0.6",
5868
"mm": "^2.5.0",
5969
"supertest": "^3.1.0"
6070
},
6171
"engines": {
6272
"node": "^4.8.4 || ^6.10.1 || ^7.10.1 || >= 8.1.4"
6373
},
6474
"files": [
75+
"dist",
6576
"lib"
6677
]
6778
}

test/load-with-esm.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const assert = require('assert');
2+
3+
let importESM = () => {};
4+
5+
describe('Load with esm', () => {
6+
before(function(){
7+
// ESM support is flagged on v12.x.
8+
const majorVersion = +process.version.split('.')[0].slice(1);
9+
if (majorVersion < 12) {
10+
this.skip();
11+
} else {
12+
// eslint-disable-next-line no-eval
13+
importESM = eval('(specifier) => import(specifier)');
14+
}
15+
});
16+
17+
it('should default export koa', async() => {
18+
const exported = await importESM('koa');
19+
const required = require('../');
20+
assert.strictEqual(exported.default, required);
21+
});
22+
23+
it('should match exports own property names', async() => {
24+
const exported = new Set(Object.getOwnPropertyNames(await importESM('koa')));
25+
const required = new Set(Object.getOwnPropertyNames(require('../')));
26+
27+
// Remove constructor properties + default export.
28+
for (const k of ['prototype', 'length', 'name']) {
29+
required.delete(k);
30+
}
31+
exported.delete('default');
32+
33+
assert.strictEqual(exported.size, required.size);
34+
assert.strictEqual([...exported].every(property => required.has(property)), true);
35+
});
36+
});

0 commit comments

Comments
 (0)