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

ESM all the things #12161

Merged
merged 27 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1c63552
ESM all the things
lucacasonato Aug 23, 2021
9bc2ce2
Merge branch 'main' into esm_all_the_things
queengooborg Apr 26, 2022
4074a80
Fix formatting
queengooborg Apr 26, 2022
d7aeb66
Add missing import
queengooborg Apr 26, 2022
9ffd02f
Fix remaining files
queengooborg Apr 26, 2022
4b957ee
Install es-main package
queengooborg Apr 26, 2022
f4c1bc0
Utilize es-main package
queengooborg Apr 26, 2022
ba8ff3c
Import yargs hideBin() helper
queengooborg Apr 26, 2022
32708b2
Fix exports
queengooborg Apr 26, 2022
d1ff939
Wrap yargs/executable script parts within esMain
queengooborg Apr 26, 2022
3e29fe6
Fix yargs
queengooborg Apr 26, 2022
3d33917
Fix imports
queengooborg Apr 26, 2022
2fee2f2
Move main function down in diff-features
queengooborg Apr 26, 2022
7f3b30b
Fix exports for diff-features
queengooborg Apr 26, 2022
dd208db
Fix release-notes script
queengooborg Apr 26, 2022
1de266c
Fix enumerate-features
queengooborg Apr 26, 2022
3b94eaa
Merge branch 'main' into esm_all_the_things
queengooborg May 4, 2022
ba01cda
review comments
lucacasonato May 5, 2022
7fd90a3
Merge branch 'main' into esm_all_the_things
queengooborg May 18, 2022
3bd064d
Fix imports and exports
queengooborg May 18, 2022
a00de8a
Fix imports/exports, cleanup code, cleanup diff, fix scripts
queengooborg May 18, 2022
f4db6ed
Fix import
queengooborg May 18, 2022
9be1335
Use import assertion for browser-specs (we're using NodeJS v16)
queengooborg May 18, 2022
35f0457
Fix ESLint
queengooborg May 18, 2022
6acf25a
Merge branch 'main' into esm_all_the_things
queengooborg May 18, 2022
c6c687c
Fix unittest
queengooborg May 18, 2022
66d49fa
Fix unittest (again)
queengooborg May 18, 2022
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ npm install @mdn/browser-compat-data
## Usage

```js
const bcd = require('@mdn/browser-compat-data');
import bcd from '@mdn/browser-compat-data';
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we revert this change, since BCD isn't ESM-ready yet?

Copy link
Contributor

Choose a reason for hiding this comment

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

Just to clarify, this change would have to wait until the first ESM-compatible version of BCD is released, right?

PS: I guess that will be a major version bump?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's correct, see #15775 for the ESM-ready public builds!

bcd.css.properties.background;
// returns a compat data object (see schema)
```
Expand Down
7 changes: 2 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
/// <reference path="./types.d.ts"/>

import { CompatData } from './types';

// This is necessary to have intellisense in projects which
// import data from this package.
declare const compatData: CompatData;
export = compatData;
export default CompatData;
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';
const fs = require('fs');
const path = require('path');
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
caugner marked this conversation as resolved.
Show resolved Hide resolved

const dirname = fileURLToPath(new URL('.', import.meta.url));
caugner marked this conversation as resolved.
Show resolved Hide resolved

class DuplicateCompatError extends Error {
constructor(message) {
Expand Down Expand Up @@ -39,7 +41,7 @@ function load() {
}

for (dir of arguments) {
caugner marked this conversation as resolved.
Show resolved Hide resolved
dir = path.resolve(__dirname, dir);
dir = path.resolve(dirname, dir);
lucacasonato marked this conversation as resolved.
Show resolved Hide resolved
fs.readdirSync(dir).forEach(processFilename);
}

Expand Down Expand Up @@ -70,7 +72,7 @@ function extend(target, source, feature = '') {
}
}

module.exports = load(
export default load(
'api',
'browsers',
'css',
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Browser compatibility data provided by MDN Web Docs",
"main": "index.js",
"types": "index.d.ts",
"type": "module",
queengooborg marked this conversation as resolved.
Show resolved Hide resolved
"engines": {
"node": ">=16.0.0"
},
Expand Down Expand Up @@ -34,6 +35,7 @@
"browser-specs": "~3.10.0",
"chalk": "~4.1.0",
"compare-versions": "~4.1.1",
"es-main": "~1.0.2",
"mdn-confluence": "~2.2.2",
"mocha": "~10.0.0",
"ora": "~5.4.0",
Expand Down
9 changes: 7 additions & 2 deletions schemas/browsers-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ The release objects consist of the following properties:
This structure is exported for consumers of `@mdn/browser-compat-data`:

```js
> const compat = require('@mdn/browser-compat-data');
> compat.browsers.firefox.releases['1.5'].status;
import bcd from '@mdn/browser-compat-data';
bcd.browsers.firefox.releases['1.5'].status; // "retired"
```

```js
> const bcd = require('@mdn/browser-compat-data');
> bcd.browsers.firefox.releases['1.5'].status;
// "retired"
```
4 changes: 1 addition & 3 deletions scripts/compare-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */

'use strict';

/**
*
* Sort a list of features based upon a specific order:
Expand Down Expand Up @@ -36,4 +34,4 @@ const compareFeatures = (a, b) => {
return a.localeCompare(b, 'en');
};

module.exports = compareFeatures;
export default compareFeatures;
5 changes: 2 additions & 3 deletions scripts/compare-features.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */

'use strict';
const assert = require('assert').strict;
import assert from 'node:assert/strict';

const compareFeatures = require('./compare-features');
import compareFeatures from './compare-features.js';

describe('compare-features script', () => {
it('`compareFeatures()` works correctly', () => {
Expand Down
37 changes: 19 additions & 18 deletions scripts/diff-features.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
const { execSync } = require('child_process');
const fs = require('fs');
import { execSync } from 'node:child_process';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';

const yargs = require('yargs');
import esMain from 'es-main';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

function main({ ref1, ref2, format, github }) {
const results = diff({ ref1, ref2, github });

if (format === 'markdown') {
printMarkdown(results);
} else {
console.log(JSON.stringify(results, undefined, 2));
}
}

function diff({ ref1, ref2, github }) {
export default function diff({ ref1, ref2, github }) {
let refA, refB;

if (ref1 === undefined && ref2 === undefined) {
Expand Down Expand Up @@ -133,8 +126,18 @@ function printMarkdown({ added, removed }) {
}
}

if (require.main === module) {
const { argv } = yargs.command(
function main({ ref1, ref2, format, github }) {
const results = diff({ ref1, ref2, github });

if (format === 'markdown') {
printMarkdown(results);
} else {
console.log(JSON.stringify(results, undefined, 2));
}
}

if (esMain(import.meta)) {
const { argv } = yargs(hideBin(process.argv)).command(
'$0 [ref1] [ref2]',
'Compare the set of features at refA and refB',
(yargs) => {
Expand Down Expand Up @@ -166,5 +169,3 @@ if (require.main === module) {

main(argv);
}

module.exports = diff;
49 changes: 26 additions & 23 deletions scripts/enumerate-features.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const yargs = require('yargs');
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const fs = require('fs');
const path = require('path');
import esMain from 'es-main';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

const { walk } = require('../utils');
import { walk } from '../utils/index.js';

function main({ dest, dataFrom }) {
fs.writeFileSync(dest, JSON.stringify(enumerateFeatures(dataFrom)));
Expand All @@ -13,7 +16,7 @@ function enumerateFeatures(dataFrom) {
const feats = [];

const walker = dataFrom
? walk(undefined, require(path.join(process.cwd(), dataFrom)))
? walk(undefined, import(path.join(process.cwd(), dataFrom, 'index.js')))
: walk();

for (const { path, compat } of walker) {
Expand All @@ -25,24 +28,24 @@ function enumerateFeatures(dataFrom) {
return feats;
}

const { argv } = yargs.command(
'$0 [dest]',
'Write a JSON-formatted list of feature paths',
(yargs) => {
yargs
.positional('dest', {
default: '.features.json',
description: 'File destination',
})
.option('data-from', {
nargs: 1,
description: 'Require compat data from an alternate path',
});
},
);

if (require.main === module) {
if (esMain(import.meta)) {
const { argv } = yargs(hideBin(process.argv)).command(
'$0 [dest]',
'Write a JSON-formatted list of feature paths',
(yargs) => {
yargs
.positional('dest', {
default: '.features.json',
description: 'File destination',
})
.option('data-from', {
nargs: 1,
description: 'Require compat data from an alternate path',
});
},
);

main(argv);
}

module.exports = enumerateFeatures;
export default enumerateFeatures;
23 changes: 13 additions & 10 deletions scripts/fix-browser-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
* @returns {*} The new value
*/

'use strict';
const fs = require('fs');
const path = require('path');
const { platform } = require('os');
import fs from 'node:fs';
import path from 'node:path';
import { platform } from 'node:os';
import { fileURLToPath } from 'node:url';

/** Determines if the OS is Windows */
const IS_WINDOWS = platform() === 'win32';
import esMain from 'es-main';

import { IS_WINDOWS } from '../test/utils.js';

const dirname = fileURLToPath(new URL('.', import.meta.url));

const orderSupportBlock = (key, value) => {
if (key === '__compat') {
Expand Down Expand Up @@ -53,14 +56,14 @@ const fixBrowserOrder = (filename) => {
}
};

if (require.main === module) {
if (esMain(import.meta)) {
/**
* @param {string[]} files
*/
function load(...files) {
for (let file of files) {
if (file.indexOf(__dirname) !== 0) {
file = path.resolve(__dirname, '..', file);
if (file.indexOf(dirname) !== 0) {
caugner marked this conversation as resolved.
Show resolved Hide resolved
file = path.resolve(dirname, '..', file);
}

if (!fs.existsSync(file)) {
Expand Down Expand Up @@ -101,4 +104,4 @@ if (require.main === module) {
}
}

module.exports = fixBrowserOrder;
export default fixBrowserOrder;
24 changes: 13 additions & 11 deletions scripts/fix-feature-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
* @returns {*} The new value
*/

'use strict';
const fs = require('fs');
const path = require('path');
const { platform } = require('os');
import fs from 'node:fs';
import path from 'node:path';
import { platform } from 'node:os';
import { fileURLToPath } from 'node:url';

/** Determines if the OS is Windows */
const IS_WINDOWS = platform() === 'win32';
import esMain from 'es-main';

const compareFeatures = require('./compare-features');
import compareFeatures from './compare-features.js';
import { IS_WINDOWS } from '../test/utils.js';

const dirname = fileURLToPath(new URL('.', import.meta.url));

function orderFeatures(key, value) {
if (value instanceof Object && '__compat' in value) {
Expand Down Expand Up @@ -55,14 +57,14 @@ const fixFeatureOrder = (filename) => {
}
};

if (require.main === module) {
if (esMain(import.meta)) {
/**
* @param {string[]} files
*/
function load(...files) {
for (let file of files) {
if (file.indexOf(__dirname) !== 0) {
file = path.resolve(__dirname, '..', file);
if (file.indexOf(dirname) !== 0) {
file = path.resolve(dirname, '..', file);
}

if (!fs.existsSync(file)) {
Expand Down Expand Up @@ -103,4 +105,4 @@ if (require.main === module) {
}
}

module.exports = fixFeatureOrder;
export default fixFeatureOrder;
Loading