Skip to content

Commit 1bbca64

Browse files
authoredMar 27, 2023
Build script: support Deno runtime (darkreader#10786)
- adds Deno runtime as an alternative to Node.js - requires Deno canary and supports only building (not tests) - built output is the same between Node.js and Deno (which means build is reproducible)
1 parent d4bb681 commit 1bbca64

8 files changed

+86
-12
lines changed
 

‎.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ coverage/
3333
# V8 Profiler logs
3434
#-----------------------------------
3535
isolate-*-v8.log
36+
37+
# Deno
38+
#-----------------------------------
39+
deno.lock
40+
deno.json

‎README.md

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Read more about contributing to Dark Reader in [CONTRIBUTING.md](https://github.
2020

2121
## Building for use
2222

23+
Dark Reader build script requires a JavaScript runtime, either NodeJS or Deno. We recommend using NodeJS, Deno support is experimental.
24+
25+
### Building with NodeJS
26+
2327
You can install the extension from a file.
2428
Install [Node.js](https://nodejs.org/) (we recommend LTS or higher, but any version at or above 15 will work). Download the source code (or check out from git).
2529
Open the terminal in the root folder and run:
@@ -31,6 +35,12 @@ This will create a `build/release/darkreader-chrome.zip` file for use in a Chrom
3135

3236
You can customize build process by passing flags to build script. To see all flags, run `npm run build -- --help`.
3337

38+
### Building with Deno
39+
40+
You can build Dark Reader with alternative runtime called [Deno](https://deno.land/). For this run `deno:bootstrap` script (e.g., via `npm run deno:bootstrap` or manually copy the command from `package.json`). Then run the same commands described above.
41+
42+
Please note that if you encounter error `Too many open files (os error 24)`, then you should use the newer version of Deno (preferably built from source or canary).
43+
3444
## Using Dark Reader for a website
3545

3646
You can use Dark Reader to enable dark mode on your website!

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"test:unit": "jest --config=tests/unit/jest.config.mjs",
2929
"test:unit:debug": "node --inspect-brk ./node_modules/jest/bin/jest --config=tests/unit/jest.config.mjs --runInBand --no-cache --watch",
3030
"test:update-snapshots": "npm run test -- --updateSnapshot && npm run test:project -- --updateSnapshot",
31-
"translate-line": "node ./tasks/translate.js --line"
31+
"translate-line": "node ./tasks/translate.js --line",
32+
"deno:bootstrap": "deno run --allow-read=./ --allow-sys=uid --allow-write=deno.json tasks/deno.js"
3233
},
3334
"main": "darkreader.js",
3435
"repository": {

‎tasks/bundle-manifest.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,11 @@
22
import path from './paths.js';
33
import * as reload from './reload.js';
44
import {createTask} from './task.js';
5-
import {readFile, writeFile} from './utils.js';
5+
import {readJSON, writeJSON} from './utils.js';
66
const {PLATFORM, getDestDir} = path;
77

88
const srcDir = 'src';
99

10-
async function readJSON(path) {
11-
const file = await readFile(path);
12-
return JSON.parse(file);
13-
}
14-
15-
async function writeJSON(path, json) {
16-
const content = JSON.stringify(json, null, 4);
17-
return await writeFile(path, content);
18-
}
19-
2010
async function patchManifest(platform, debug, watch, test) {
2111
const manifest = await readJSON(`${srcDir}/manifest.json`);
2212
const manifestPatch = platform === PLATFORM.CHROME ? {} : await readJSON(`${srcDir}/manifest-${platform}.json`);

‎tasks/deno.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {readJSON, writeJSON} from './utils.js';
2+
import {resolve} from 'node:path';
3+
4+
function resolvePath(path) {
5+
return resolve(import.meta.url.replace('file:/', ''), '../../', path);
6+
}
7+
8+
function createImports(dependencies) {
9+
const imports = {};
10+
for (const name in dependencies) {
11+
imports[name] = `npm:${name}@${dependencies[name]}`;
12+
}
13+
return imports;
14+
}
15+
16+
function createTasks(scripts) {
17+
const tasks = {};
18+
for (const name in scripts) {
19+
const command = scripts[name];
20+
tasks[name] = command
21+
.replace('--max-old-space-size=3072', '')
22+
.replace('node ', 'deno run -A ')
23+
.replaceAll('npm run ', 'deno task ');
24+
}
25+
return tasks;
26+
}
27+
28+
async function writeDenoJSON() {
29+
const packageJSON = resolvePath('package.json');
30+
const denoJSON = await resolvePath('deno.json');
31+
const pkg = await readJSON(packageJSON);
32+
33+
if (pkg.dependencies) {
34+
console.error('TODO: support dependencies key in createImports()');
35+
}
36+
const imports = createImports(pkg.devDependencies);
37+
38+
const tasks = createTasks(pkg.scripts);
39+
40+
writeJSON(denoJSON, {imports, tasks});
41+
}
42+
43+
async function main() {
44+
await writeDenoJSON();
45+
}
46+
47+
main();

‎tasks/log.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {createWriteStream} from 'node:fs';
2+
import process from 'node:process';
23
import {WebSocketServer} from 'ws';
34
import {createTask} from './task.js';
45
import {log} from './utils.js';

‎tasks/reload.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import process from 'node:process';
12
import {WebSocketServer} from 'ws';
23
import {log} from './utils.js';
34

‎tasks/utils.js

+19
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ export async function writeFile(dest, content) {
9292
await fs.writeFile(dest, content, 'utf8');
9393
}
9494

95+
/**
96+
* @param {string} path
97+
* @returns {Promise<Object>}
98+
*/
99+
export async function readJSON(path) {
100+
const file = await readFile(path);
101+
return JSON.parse(file);
102+
}
103+
104+
/**
105+
* @param {string} dest
106+
* @param {string} content
107+
* @returns {Promise<void>}
108+
*/
109+
export async function writeJSON(dest, content) {
110+
const string = JSON.stringify(content, null, 4);
111+
return await writeFile(dest, string);
112+
}
113+
95114
/**
96115
* @param {string | string[]} patterns
97116
* @returns {Promise<string[]>}

0 commit comments

Comments
 (0)
Please sign in to comment.