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 1 commit
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

function load() {
// Recursively load one or more directories passed as arguments.
Expand Down Expand Up @@ -32,7 +34,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 All @@ -59,7 +61,7 @@ function extend(target, source) {
}
}

module.exports = load(
export default load(
'api',
'browsers',
'css',
Expand Down
17 changes: 9 additions & 8 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": ">=10.0.0"
},
Expand Down Expand Up @@ -41,15 +42,15 @@
"scripts": {
"confluence": "node ./node_modules/mdn-confluence/main/generate.es6.js --output-dir=. --bcd-module=./index.js",
"mocha": "mocha \"*/**.test.js\"",
"lint": "node test/lint",
"fix": "node scripts/fix",
"mirror": "node scripts/mirror",
"stats": "node scripts/statistics",
"release-build": "node scripts/release-build",
"release-pulls": "node scripts/release-pulls",
"release-stats": "node scripts/release-stats",
"lint": "node test/lint.js",
"fix": "node scripts/fix.js",
"mirror": "node scripts/mirror.js",
"stats": "node scripts/statistics.js",
"release-build": "node scripts/release-build.js",
"release-pulls": "node scripts/release-pulls.js",
"release-stats": "node scripts/release-stats.js",
"show-errors": "npm test 1> /dev/null",
"test": "npm run lint && npm run mocha",
"traverse": "node scripts/traverse"
"traverse": "node scripts/traverse.js"
}
}
4 changes: 2 additions & 2 deletions schemas/browsers-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ 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;
lucacasonato marked this conversation as resolved.
Show resolved Hide resolved
// "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;
12 changes: 7 additions & 5 deletions scripts/diff-features.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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 yargs from 'yargs';

function main({ ref1, ref2, format }) {
let refA, refB;
Expand Down Expand Up @@ -73,7 +74,7 @@ function printMarkdown({ added, removed }) {
}
}

const { argv } = yargs.command(
const { argv } = yargs().command(
'$0 [ref1] [ref2]',
'Compare the set of features at refA and refB',
yargs => {
Expand All @@ -99,6 +100,7 @@ const { argv } = yargs.command(
},
);

if (require.main === module) {
const self = fileURLToPath(import.meta.url);
if (process.argv[1] === self) {
main(argv);
}
23 changes: 15 additions & 8 deletions scripts/enumerate-features.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const yargs = require('yargs');
import yargs from 'yargs';

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

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 +14,12 @@ function enumerateFeatures(dataFrom) {
const feats = [];

const walker = dataFrom
? walk(undefined, require(path.join(process.cwd(), dataFrom)))
? walk(
undefined,
JSON.parse(
fs.readFileSync(path.join(process.cwd(), dataFrom), 'utf-8'),
queengooborg marked this conversation as resolved.
Show resolved Hide resolved
),
)
: walk();

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

const { argv } = yargs.command(
const { argv } = yargs().command(
'$0 [dest]',
'Write a JSON-formatted list of feature paths',
yargs => {
Expand All @@ -41,8 +47,9 @@ const { argv } = yargs.command(
},
);

if (require.main === module) {
const self = fileURLToPath(import.meta.url);
if (process.argv[1] === self) {
main(argv);
}

module.exports = enumerateFeatures;
export default enumerateFeatures;
19 changes: 11 additions & 8 deletions scripts/fix-browser-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
* @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';

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

/** Determines if the OS is Windows */
const IS_WINDOWS = platform() === 'win32';
Expand Down Expand Up @@ -53,14 +55,15 @@ const fixBrowserOrder = filename => {
}
};

if (require.main === module) {
const self = fileURLToPath(import.meta.url);
if (process.argv[1] === self) {
/**
* @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;
23 changes: 13 additions & 10 deletions scripts/fix-feature-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@
* @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';

import compareFeatures from './compare-features.js';

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

/** Determines if the OS is Windows */
const IS_WINDOWS = platform() === 'win32';

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

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

if (require.main === module) {
const self = fileURLToPath(import.meta.url);
if (process.argv[1] === self) {
/**
* @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 +106,4 @@ if (require.main === module) {
}
}

module.exports = fixFeatureOrder;
export default fixFeatureOrder;
6 changes: 2 additions & 4 deletions scripts/fix-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */

'use strict';

const { exec } = require('child_process');
import { exec } from 'node:child_process';

const format = () => {
exec('npx prettier --write "**/*.js" "**/*.ts" "**/*.md"');
};

module.exports = format;
export default format;
18 changes: 10 additions & 8 deletions scripts/fix.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
'use strict';
const fs = require('fs');
const path = require('path');
const fixBrowserOrder = require('./fix-browser-order');
const fixFeatureOrder = require('./fix-feature-order');
const format = require('./fix-format');
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import fixBrowserOrder from './fix-browser-order.js';
import fixFeatureOrder from './fix-feature-order.js';
import format from './fix-format.js';

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

/**
* @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
4 changes: 1 addition & 3 deletions scripts/migrations/001-sort-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';

const { exec } = require('child_process');
import { exec } from 'node:child_process';

exec('node scripts/fix-feature-order.js');
19 changes: 11 additions & 8 deletions scripts/migrations/002-remove-webview-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */

'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';

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

/** Determines if the OS is Windows */
const IS_WINDOWS = platform() === 'win32';
Expand Down Expand Up @@ -58,14 +60,15 @@ const fixWebViewFlags = filename => {
}
};

if (require.main === module) {
const self = fileURLToPath(import.meta.url);
if (process.argv[1] === self) {
/**
* @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 @@ -106,4 +109,4 @@ if (require.main === module) {
}
}

module.exports = { removeWebViewFlags, fixWebViewFlags };
export { fixWebViewFlags, removeWebViewFlags };
Loading