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

feat: support esm #1474

Merged
merged 2 commits into from Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});