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

Convert Node scripts to module scripts #1507

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 2 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -46,7 +45,7 @@
}
},
{
"files": ["gulpfile.js", "tests/karma.config.js"],
"files": ["tests/karma.config.cjs"],
"rules": {
"@typescript-eslint/no-var-requires": "off"
}
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.yalc/
.yarn/
build/
coverage/
src/vendor/
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand All @@ -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);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": "[email protected]"
Expand Down
File renamed without changes.
3 changes: 0 additions & 3 deletions tools/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
"env": {
"node": true
},
"parserOptions": {
"sourceType": "script"
},
"rules": {
"@typescript-eslint/no-var-requires": "off",
"no-console": "off"
Expand Down
11 changes: 7 additions & 4 deletions tools/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
* file and the package environment.
*/

'use strict';
import * as fs from 'node:fs';
import * as path from 'node:path';
Copy link
Member Author

Choose a reason for hiding this comment

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

While writing this I realized we didn't have a convention about whether to use the node: prefix or not and whether it was considered preferred practice. nodejs/node#38343 suggests that using the prefix for built-in modules is preferred.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the prefix makes sense.


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 => {
Expand Down Expand Up @@ -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),
Expand Down
9 changes: 5 additions & 4 deletions tools/template-context-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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({
Expand Down