From bbcde76f5cb5b67bbcd3201791cf0ef648fd3a8b Mon Sep 17 00:00:00 2001 From: ZYSzys Date: Sun, 21 Jun 2020 22:26:48 +0800 Subject: [PATCH] feat: support esm (#1474) --- .gitignore | 1 + .travis.yml | 2 ++ package.json | 13 ++++++++++++- test/load-with-esm.js | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 test/load-with-esm.js diff --git a/.gitignore b/.gitignore index 8abbc16ea..cd2f2f2cb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ coverage npm-debug.log .idea *.iml +dist diff --git a/.travis.yml b/.travis.yml index f72a2167d..5a41e7870 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/package.json b/package.json index 4e1cc2f5c..ba9e074d9 100644 --- a/package.json +++ b/package.json @@ -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" ] } diff --git a/test/load-with-esm.js b/test/load-with-esm.js new file mode 100644 index 000000000..6e564a4a8 --- /dev/null +++ b/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); + }); +});