From 8c3fd6e1e0610d70ea36f11f3c5202728422dbea Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 4 May 2021 21:26:27 +0700 Subject: [PATCH] Require Node.js 12 and move to ESM --- .github/workflows/main.yml | 5 ++--- index.d.ts | 14 ++++++-------- index.js | 6 ++---- index.test-d.ts | 6 +++--- license | 2 +- package.json | 19 +++++++++++-------- readme.md | 12 +++--------- test.js | 14 +++++++------- 8 files changed, 35 insertions(+), 43 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18531b3..3b8aa86 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/index.d.ts b/index.d.ts index f027e0d..bc6f182 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,12 +5,10 @@ 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 & { @@ -18,14 +16,14 @@ declare const domLoaded: Promise & { 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; diff --git a/index.js b/index.js index a63b693..c256609 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,3 @@ -'use strict'; - const hasLoaded = () => document.readyState === 'interactive' || document.readyState === 'complete'; const domLoaded = new Promise(resolve => { @@ -16,8 +14,8 @@ const domLoaded = new Promise(resolve => { } }); +export default domLoaded; + Object.defineProperty(domLoaded, 'hasLoaded', { get: () => hasLoaded() }); - -module.exports = domLoaded; diff --git a/index.test-d.ts b/index.test-d.ts index d38a0fe..b66f825 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ -import {expectType} from 'tsd'; -import domLoaded = require('.'); +import {expectType, expectAssignable} from 'tsd'; +import domLoaded from './index.js'; -expectType>(domLoaded); +expectAssignable>(domLoaded); expectType(domLoaded.hasLoaded); diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (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: diff --git a/package.json b/package.json index 49aed98..50c781f 100644 --- a/package.json +++ b/package.json @@ -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", @@ -24,7 +28,6 @@ "dom", "document", "ready", - "doc", "complete", "check", "wait", @@ -32,10 +35,10 @@ "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": [ diff --git a/readme.md b/readme.md index b2797f0..3f7039c 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -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 diff --git a/test.js b/test.js index 3701f12..baf50eb 100644 --- a/test.js +++ b/test.js @@ -1,4 +1,4 @@ -import fs from 'fs'; +import fs from 'node:fs'; import test from 'ava'; import {JSDOM} from 'jsdom'; @@ -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); @@ -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);