From b5472f4cbb87349becae36b4a9ad5f76a825abb8 Mon Sep 17 00:00:00 2001 From: miwnwski Date: Mon, 4 Jan 2021 16:10:24 +0100 Subject: [PATCH] fix: make ESM transpiled CommonJS play nice for TS folks, fix #1513 (#1518) --- lib/application.js | 9 +++++++++ test/load-with-esm.js | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/application.js b/lib/application.js index e01019796..6849d73fd 100644 --- a/lib/application.js +++ b/lib/application.js @@ -211,6 +211,15 @@ module.exports = class Application extends Emitter { const msg = err.stack || err.toString(); console.error(`\n${msg.replace(/^/gm, ' ')}\n`); } + + /** + * Help TS users comply to CommonJS, ESM, bundler mismatch. + * @see https://github.com/koajs/koa/issues/1513 + */ + + static get default() { + return Application; + } }; /** diff --git a/test/load-with-esm.js b/test/load-with-esm.js index 6e564a4a8..87bffda35 100644 --- a/test/load-with-esm.js +++ b/test/load-with-esm.js @@ -28,9 +28,22 @@ describe('Load with esm', () => { for (const k of ['prototype', 'length', 'name']) { required.delete(k); } - exported.delete('default'); + + // Commented out to "fix" CommonJS, ESM, bundling issue. + // @see https://github.com/koajs/koa/issues/1513 + // exported.delete('default'); assert.strictEqual(exported.size, required.size); assert.strictEqual([...exported].every(property => required.has(property)), true); }); + + it('CommonJS exports default property', async() => { + const required = require('../'); + assert.strictEqual(required.hasOwnProperty('default'), true); + }); + + it('CommonJS exports default property referencing self', async() => { + const required = require('../'); + assert.strictEqual(required.default, required); + }); });