Skip to content

Commit

Permalink
feat: add ESM export; fixes mochajs#5211
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Oct 8, 2024
1 parent 1173da0 commit 2d45144
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ docs/images/supporters
docs/api
mocha.js
mocha.js.map
mocha.mjs
mocha.mjs.map
.karma/
!bin/mocha.js
!lib/mocha.js
Expand Down
3 changes: 2 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ module.exports = [
'out/**',
'test/integration/fixtures/**',
'.karma/**',
'mocha.js'
'mocha.js',
'mocha.mjs'
],
}
];
4 changes: 4 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ const chooseTestSuite = (cfg, value) => {
{
pattern: 'test/browser-specific/esm.spec.mjs',
type: 'module'
},
{
pattern: 'test/browser-specific/esm-build.spec.mjs',
type: 'module'
}
]
});
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
"mocha.css",
"mocha.js",
"mocha.js.map",
"mocha.mjs",
"mocha.mjs.map",
"browser-entry.js"
],
"browser": {
Expand Down
66 changes: 36 additions & 30 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,43 @@ import {visualizer} from 'rollup-plugin-visualizer';
import pickFromPackageJson from './scripts/pick-from-package-json';
import {version} from './package.json';

const config = {
input: './browser-entry.js',
output: {
file: './mocha.js',
format: 'umd',
sourcemap: true,
name: 'mocha',
banner: `// mocha@${version} in javascript ES2018`
},
plugins: [
json(),
pickFromPackageJson({
keys: ['name', 'version', 'homepage', 'notifyLogo']
}),
commonjs(),
globals(),
nodePolyfills(),
nodeResolve({
browser: true
})
],
onwarn: (warning, warn) => {
if (warning.code === 'CIRCULAR_DEPENDENCY') return;
function getConfig (format) {
const config = {
input: './browser-entry.js',
output: {
file: `./mocha${format === 'esm' ? '.mjs' : '.js'}`,
format,
sourcemap: true,
name: 'mocha',
banner: `// mocha@${version} in javascript ES2018`
},
plugins: [
json(),
pickFromPackageJson({
keys: ['name', 'version', 'homepage', 'notifyLogo']
}),
commonjs(),
globals(),
nodePolyfills(),
nodeResolve({
browser: true
})
],
onwarn: (warning, warn) => {
if (warning.code === 'CIRCULAR_DEPENDENCY') return;

// Use default for everything else
warn(warning);
}
};
// Use default for everything else
warn(warning);
}
};

if (!process.env.CI) {
config.plugins.push(visualizer());
if (!process.env.CI) {
config.plugins.push(visualizer());
}
return config;
}

export default config;
export default [
getConfig('umd'),
getConfig('esm')
];
9 changes: 9 additions & 0 deletions test/browser-specific/esm-build.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import './fixtures/esm-build.fixture.mjs';

it('should register a global if it did not fail', function () {
expect(window.MOCHA_IS_OK, 'to be ok');
});

it('should have a global Mocha', function () {
expect(window.Mocha, 'not to be', undefined);
});
3 changes: 3 additions & 0 deletions test/browser-specific/fixtures/esm-build.fixture.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable-next-line import/no-absolute-path */
import '/base/mocha.mjs';
window.MOCHA_IS_OK = true;

0 comments on commit 2d45144

Please sign in to comment.