Skip to content

Commit

Permalink
feat: support esm (#1474)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZYSzys committed Jun 21, 2020
1 parent b7d8c97 commit bbcde76
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ coverage
npm-debug.log
.idea
*.iml
dist
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -3,6 +3,7 @@ node_js:
- 8
- 10
- 12
- 14
cache:
directories:
- wrk/bin
Expand All @@ -13,6 +14,7 @@ before_script:
- export PATH=$PATH:$PWD/wrk/bin/
script:
- npm run lint
- npm run prepack
- npm run test-cov
- npm run bench
after_script:
Expand Down
13 changes: 12 additions & 1 deletion package.json
Expand Up @@ -3,12 +3,21 @@
"version": "2.12.1",
"description": "Koa web app framework",
"main": "lib/application.js",
"exports": {
".": {
"require": "./lib/application.js",
"import": "./dist/koa.mjs"
},
"./": "./"
},
"scripts": {
"test": "egg-bin test test",
"test-cov": "egg-bin cov test",
"lint": "eslint benchmarks lib test",
"bench": "make -C benchmarks",
"authors": "git log --format='%aN <%aE>' | sort -u > AUTHORS"
"authors": "git log --format='%aN <%aE>' | sort -u > AUTHORS",
"build": "gen-esm-wrapper . ./dist/koa.mjs",
"prepack": "npm run build"
},
"repository": "koajs/koa",
"keywords": [
Expand Down Expand Up @@ -55,13 +64,15 @@
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"gen-esm-wrapper": "^1.0.6",
"mm": "^2.5.0",
"supertest": "^3.1.0"
},
"engines": {
"node": "^4.8.4 || ^6.10.1 || ^7.10.1 || >= 8.1.4"
},
"files": [
"dist",
"lib"
]
}
36 changes: 36 additions & 0 deletions test/load-with-esm.js
@@ -0,0 +1,36 @@
const assert = require('assert');

let importESM = () => {};

describe('Load with esm', () => {
before(function(){
// ESM support is flagged on v12.x.
const majorVersion = +process.version.split('.')[0].slice(1);
if (majorVersion < 12) {
this.skip();
} else {
// eslint-disable-next-line no-eval
importESM = eval('(specifier) => import(specifier)');
}
});

it('should default export koa', async() => {
const exported = await importESM('koa');
const required = require('../');
assert.strictEqual(exported.default, required);
});

it('should match exports own property names', async() => {
const exported = new Set(Object.getOwnPropertyNames(await importESM('koa')));
const required = new Set(Object.getOwnPropertyNames(require('../')));

// Remove constructor properties + default export.
for (const k of ['prototype', 'length', 'name']) {
required.delete(k);
}
exported.delete('default');

assert.strictEqual(exported.size, required.size);
assert.strictEqual([...exported].every(property => required.has(property)), true);
});
});

0 comments on commit bbcde76

Please sign in to comment.