Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 4, 2021
1 parent ac55671 commit 8c3fd6e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 43 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
14 changes: 6 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@ The promise resolves when the DOM finishes loading or right away if the DOM has
@example
```
import domLoaded = require('dom-loaded');
import domLoaded from 'dom-loaded';
(async () => {
await domLoaded;
console.log('The DOM is now loaded.');
})();
await domLoaded;
console.log('The DOM is now loaded.');
```
*/
declare const domLoaded: Promise<void> & {
/**
Synchronously check if the DOM has already finished loading.
```
import domLoaded = require('dom-loaded');
import domLoaded from 'dom-loaded';
if (domLoaded.hasLoaded) {
console.log('The DOM has already finished loading.')
}
```
*/
hasLoaded: boolean;
readonly hasLoaded: boolean;
};

export = domLoaded;
export default domLoaded;
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const hasLoaded = () => document.readyState === 'interactive' || document.readyState === 'complete';

const domLoaded = new Promise(resolve => {
Expand All @@ -16,8 +14,8 @@ const domLoaded = new Promise(resolve => {
}
});

export default domLoaded;

Object.defineProperty(domLoaded, 'hasLoaded', {
get: () => hasLoaded()
});

module.exports = domLoaded;
6 changes: 3 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import domLoaded = require('.');
import {expectType, expectAssignable} from 'tsd';
import domLoaded from './index.js';

expectType<Promise<void>>(domLoaded);
expectAssignable<Promise<void>>(domLoaded);
expectType<boolean>(domLoaded.hasLoaded);
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
"description": "Check when the DOM has loaded like `DOMContentLoaded`",
"license": "MIT",
"repository": "sindresorhus/dom-loaded",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
"// TODO: Enable when JSOM support ESM - test": "xo && ava && tsd",
"test": "xo && tsd"
},
"files": [
"index.js",
Expand All @@ -24,18 +28,17 @@
"dom",
"document",
"ready",
"doc",
"complete",
"check",
"wait",
"domcontentloaded",
"readystate"
],
"devDependencies": {
"ava": "^1.4.1",
"jsdom": "^14.0.0",
"tsd": "^0.7.1",
"xo": "^0.24.0"
"ava": "^3.15.0",
"jsdom": "^16.5.3",
"tsd": "^0.14.0",
"xo": "^0.39.1"
},
"xo": {
"envs": [
Expand Down
12 changes: 3 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,21 @@
Unlike `DOMContentLoaded`, this also works when included after the DOM was loaded.


## Install

```
$ npm install dom-loaded
```


## Usage

```js
const domLoaded = require('dom-loaded');
import domLoaded from 'dom-loaded';

(async () => {
await domLoaded;
console.log('The DOM is now loaded.');
})();
await domLoaded;
console.log('The DOM is now loaded.');
```


## API

### domLoaded
Expand All @@ -38,7 +33,6 @@ Type: `boolean`

Synchronously check if the DOM has already finished loading.


## Related

- [element-ready](https://github.com/sindresorhus/element-ready) - Detect when an element is ready in the DOM
14 changes: 7 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs';
import fs from 'node:fs';
import test from 'ava';
import {JSDOM} from 'jsdom';

Expand Down Expand Up @@ -36,9 +36,9 @@ test('works when included after `DOMContentLoaded` fired', async t => {
runScripts: 'outside-only'
});

const loadedPromise = new Promise(resolve =>
window.document.addEventListener('DOMContentLoaded', resolve)
);
const loadedPromise = new Promise(resolve => {
window.document.addEventListener('DOMContentLoaded', resolve);
});

await loadedPromise;
window.eval(umdWrappedDomLoaded);
Expand All @@ -54,9 +54,9 @@ test('domLoaded.hasLoaded', async t => {
runScripts: 'outside-only'
});

const loadedPromise = new Promise(resolve =>
window.document.addEventListener('DOMContentLoaded', resolve)
);
const loadedPromise = new Promise(resolve => {
window.document.addEventListener('DOMContentLoaded', resolve);
});

await loadedPromise;
window.eval(umdWrappedDomLoaded);
Expand Down

0 comments on commit 8c3fd6e

Please sign in to comment.