Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ESM export #5221

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ docs/images/supporters
docs/api
mocha.js
mocha.js.map
esm/mocha.js
esm/mocha.js.map
esm/package.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh 😬 I didn't think adding support for ESM would require an entirely new output file. This file is pretty huge: 600k+ characters, 620K on disk. We can't add such a large addition to package size for all users.

I had assumed the entry point would be a minimal shim, i.e.:

import "./mocha.js"

const { mocha } = globalThis;

const describe = mocha.describe.bind(mocha);
const it = mocha.it.bind(mocha);
const setup = mocha.setup.bind(mocha);

export { describe, it, setup };

Is that not doable for some reason?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I'm unfortunately dealing with some health problems, so I think I will just nix this PR.

.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',
'esm/mocha.js'
],
}
];
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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"node": ">= 14.0.0"
},
"scripts": {
"build": "rollup -c ./rollup.config.js",
"build": "rollup -c ./rollup.config.js && echo '{\"type\":\"module\"}' > esm/package.json",
"clean": "rimraf mocha.js mocha.js.map",
"docs-clean": "rimraf docs/_site docs/api",
"docs-watch": "eleventy --serve",
Expand Down Expand Up @@ -177,6 +177,9 @@
"mocha.css",
"mocha.js",
"mocha.js.map",
"esm/mocha.js",
"esm/mocha.js.map",
"esm/package.json",
"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: format === 'esm' ? './esm/mocha.js' : './mocha.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')
];
13 changes: 13 additions & 0 deletions test/browser-specific/esm-build.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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);
});

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