diff --git a/packages/macros/.gitignore b/packages/macros/.gitignore index 7b5197cc5..59f9bf706 100644 --- a/packages/macros/.gitignore +++ b/packages/macros/.gitignore @@ -7,3 +7,4 @@ /tests/**/*.map !/src/vendor/*.js !/src/addon/*.js +/src/addon/es-compat.d.ts diff --git a/packages/macros/src/addon/es-compat.js b/packages/macros/src/addon/es-compat.js index ddb4b6d1f..286ce2e0f 100644 --- a/packages/macros/src/addon/es-compat.js +++ b/packages/macros/src/addon/es-compat.js @@ -1,3 +1,3 @@ export default function esCompat(m) { - return m?.__esModule ? m : { default: m }; + return m?.__esModule ? m : { default: m, ...m }; } diff --git a/packages/macros/tests/runtime.test.ts b/packages/macros/tests/runtime.test.ts index 3f8508d76..aea5fdadc 100644 --- a/packages/macros/tests/runtime.test.ts +++ b/packages/macros/tests/runtime.test.ts @@ -9,6 +9,8 @@ import { moduleExists, } from '../src/index'; +import esc from '../src/addon/es-compat'; + const ERROR_REGEX = /this method is really implemented at compile time via a babel plugin. If you're seeing this exception, something went wrong/; @@ -53,3 +55,28 @@ describe(`type-only exports`, function () { expect(moduleExists).toThrow(ERROR_REGEX); }); }); + +describe(`es-compat`, function () { + test('ES module are untouched', function () { + let esm = { + __esModule: true, + default: class ESM {}, + named: function named() {}, + }; + + expect(esc(esm)).toEqual(esm); + }); + + test('CJS module are shimmed', function () { + let cjs = { + named: function named() {}, + another: function another() {}, + }; + + expect(esc(cjs).default.named).toEqual(cjs.named); + expect(esc(cjs).default.another).toEqual(cjs.another); + + expect(esc(cjs).named).toEqual(cjs.named); + expect(esc(cjs).another).toEqual(cjs.another); + }); +});