Skip to content

Commit

Permalink
fix: make ESM transpiled CommonJS play nice for TS folks, fix #1513 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
miwnwski committed Jan 4, 2021
1 parent b4398f5 commit b5472f4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

/**
Expand Down
15 changes: 14 additions & 1 deletion test/load-with-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit b5472f4

Please sign in to comment.