From 595d1f00389978f7b0f82397f635f3242347eb28 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Mon, 15 Apr 2024 11:54:23 +0100 Subject: [PATCH] Convert Node scripts to module scripts This reduces the need for variation in eg. linting rules by making all JS files modules where possible. Note that Karma does not support this. See https://github.com/karma-runner/karma/issues/3677. --- .eslintrc | 5 ++--- .prettierignore | 1 + Makefile | 4 ++-- gulpfile.js | 12 +++++------- package.json | 3 ++- tests/{karma.config.js => karma.config.cjs} | 0 tools/.eslintrc | 3 --- tools/settings.js | 11 +++++++---- tools/template-context-app.js | 9 +++++---- 9 files changed, 24 insertions(+), 24 deletions(-) rename tests/{karma.config.js => karma.config.cjs} (100%) diff --git a/.eslintrc b/.eslintrc index dc6cd238..8f2ac846 100644 --- a/.eslintrc +++ b/.eslintrc @@ -32,8 +32,7 @@ "overrides": [ { "files": [ - "gulpfile.js", - "tests/karma.config.js", + "tests/karma.config.cjs", // Entry points which currently get loaded as non-module scripts. "src/help/index.js", @@ -46,7 +45,7 @@ } }, { - "files": ["gulpfile.js", "tests/karma.config.js"], + "files": ["tests/karma.config.cjs"], "rules": { "@typescript-eslint/no-var-requires": "off" } diff --git a/.prettierignore b/.prettierignore index 93e42857..6a609a24 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,4 +1,5 @@ .yalc/ +.yarn/ build/ coverage/ src/vendor/ diff --git a/Makefile b/Makefile index 0853ac8b..5b248245 100644 --- a/Makefile +++ b/Makefile @@ -93,11 +93,11 @@ lint: node_modules/.uptodate build/settings.json .PHONY: checkformatting checkformatting: node_modules/.uptodate - $(PRETTIER) --check '**/*.{ts,js}' + $(PRETTIER) --check '**/*.{ts,js,cjs}' .PHONY: format format: node_modules/.uptodate - $(PRETTIER) --list-different --write '**/*.{ts,js}' + $(PRETTIER) --list-different --write '**/*.{ts,js,cjs}' .PHONY: test test: node_modules/.uptodate diff --git a/gulpfile.js b/gulpfile.js index 9413e980..4cdc9a96 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -2,11 +2,9 @@ /* eslint-env node */ // @ts-nocheck -'use strict'; +import { spawn } from 'node:child_process'; -const { spawn } = require('child_process'); - -const { parallel, watch } = require('gulp'); +import * as gulp from 'gulp'; function build(cb) { const make = spawn('make', ['build'], { stdio: 'inherit' }); @@ -20,11 +18,11 @@ function build(cb) { } function watchClient() { - watch('node_modules/hypothesis', { events: 'all' }, build); + gulp.watch('node_modules/hypothesis', { events: 'all' }, build); } function watchSrc() { - watch('src', { events: 'all' }, build); + gulp.watch('src', { events: 'all' }, build); } -exports.watch = parallel(build, watchClient, watchSrc); +export const watch = gulp.parallel(build, watchClient, watchSrc); diff --git a/package.json b/package.json index 4d5ca775..48747376 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "hypothesis-browser-extension", "version": "1.1483.0", "private": true, + "type": "module", "description": "Annotate with anyone, anywhere.", "license": "BSD-2-Clause", "homepage": "https://hypothes.is", @@ -50,7 +51,7 @@ }, "browserslist": "chrome 85, firefox 75", "scripts": { - "test": "rollup -c rollup-tests.config.mjs && karma start tests/karma.config.js --single-run", + "test": "rollup -c rollup-tests.config.mjs && karma start tests/karma.config.cjs --single-run", "typecheck": "tsc --build src/tsconfig.json" }, "packageManager": "yarn@3.6.1" diff --git a/tests/karma.config.js b/tests/karma.config.cjs similarity index 100% rename from tests/karma.config.js rename to tests/karma.config.cjs diff --git a/tools/.eslintrc b/tools/.eslintrc index 8effbd4d..a6330d4f 100644 --- a/tools/.eslintrc +++ b/tools/.eslintrc @@ -2,9 +2,6 @@ "env": { "node": true }, - "parserOptions": { - "sourceType": "script" - }, "rules": { "@typescript-eslint/no-var-requires": "off", "no-console": "off" diff --git a/tools/settings.js b/tools/settings.js index 715ce1eb..db2fe03d 100755 --- a/tools/settings.js +++ b/tools/settings.js @@ -5,10 +5,11 @@ * file and the package environment. */ -'use strict'; +import * as fs from 'node:fs'; +import * as path from 'node:path'; -const path = require('path'); -const gitDescribeSync = require('git-describe').gitDescribeSync; +import gitDescribe from 'git-describe'; +const { gitDescribeSync } = gitDescribe; // Suppress (expected) EPIPE errors on STDOUT process.stdout.on('error', err => { @@ -50,7 +51,9 @@ if (process.argv.length !== 3) { process.exit(1); } -const settings = require(path.join(process.cwd(), process.argv[2])); +const settings = JSON.parse( + fs.readFileSync(path.join(process.cwd(), process.argv[2])), +); const settingsOut = { ...settings, ...getVersion(settings.buildType), diff --git a/tools/template-context-app.js b/tools/template-context-app.js index 60a0f046..85aa8b22 100755 --- a/tools/template-context-app.js +++ b/tools/template-context-app.js @@ -5,9 +5,8 @@ * `app.html` file. */ -'use strict'; - -const path = require('path'); +import * as fs from 'node:fs'; +import * as path from 'node:path'; function appSettings(settings) { let result = {}; @@ -37,7 +36,9 @@ if (process.argv.length !== 3) { process.exit(1); } -const settings = require(path.join(process.cwd(), process.argv[2])); +const settings = JSON.parse( + fs.readFileSync(path.join(process.cwd(), process.argv[2])), +); console.log( JSON.stringify({