feat: support esm (#1474)

This commit is contained in:
ZYSzys
2020-06-21 22:26:48 +08:00
committed by GitHub
parent b7d8c97f49
commit bbcde76f5c
4 changed files with 51 additions and 1 deletions

1
.gitignore vendored
View File

@@ -4,3 +4,4 @@ coverage
npm-debug.log
.idea
*.iml
dist

View File

@@ -3,6 +3,7 @@ node_js:
- 8
- 10
- 12
- 14
cache:
directories:
- wrk/bin
@@ -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:

View File

@@ -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": [
@@ -55,6 +64,7 @@
"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"
},
@@ -62,6 +72,7 @@
"node": "^4.8.4 || ^6.10.1 || ^7.10.1 || >= 8.1.4"
},
"files": [
"dist",
"lib"
]
}

36
test/load-with-esm.js Normal file
View File

@@ -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);
});
});