Skip to content

Commit

Permalink
Import bundled Insect in index.js
Browse files Browse the repository at this point in the history
Fixes sharkdp#310, and provides a start for sharkdp#306.

This improves the startup time on my machine significantly. I'll let the
benchmark results speak for themselves:

Before commit:

| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `Startup time (insect '1+1')` | 513.9 ± 24.4 | 496.4 | 581.2 | 1.00 |

| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `Startup time, stdin mode (insect < computations-1.ins)` | 535.5 ± 14.2 | 524.6 | 572.7 | 1.00 |

| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `Evaluation speed (insect < computations-160.ins)` | 1502.1 ± 42.4 | 1466.2 | 1580.7 | 1.00 |

After commit:

| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `Startup time (insect '1+1')` | 192.4 ± 18.5 | 185.4 | 259.1 | 1.00 |

| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `Startup time, stdin mode (insect < computations-1.ins)` | 208.1 ± 2.0 | 204.8 | 212.2 | 1.00 |

| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `Evaluation speed (insect < computations-160.ins)` | 1153.0 ± 17.6 | 1136.6 | 1198.3 | 1.00 |

Also, this commit causes `output` to not be published to NPM, which
makes for a much smaller installation size: Insect 5.7.0 on NPM has an
unpacked size of 29.2 MBs, but `npm publish --dry-run` with this commit
reports 378.8 kB. `.npmignore` was also removed, as the `files` property
in `package.json` includes only the files we want, so `.npmignore` does
basically nothing. Read
https://docs.npmjs.com/cli/v8/configuring-npm/package-json/#files for
more information.

This commit additionally makes a small modification to `index.js` to
make it work with Node 12. Unfortunately, at least one of our
dependencies (`clipboardy`) does not support Node 10, so I could not
make `index.js` work with Node 10.
  • Loading branch information
triallax committed Jul 20, 2022
1 parent 4215000 commit 3944a20
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/.spago/
/.psc*
/.psa*
/insect.js
/docs/insect.1
/web/*.js
/web/terminal.css
Expand Down
14 changes: 0 additions & 14 deletions .npmignore

This file was deleted.

39 changes: 21 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import * as Insect from "./output/Insect/index.js";
import * as Insect from "./insect.js";
import * as path from "path";

var insectEnv = Insect.initialEnvironment;
Expand Down Expand Up @@ -45,24 +45,27 @@ if (process.argv.length >= 4) {
}

if (process.env.INSECT_NO_RC !== "true") {
var [os, lineReader] = await Promise.all([import("os"), import("line-reader")]);
// Node 12 does not support top-level await.
(async function() {
var [os, lineReader] = await Promise.all([import("os"), import("line-reader")]);

var rcFile = path.join(os.homedir(), ".insectrc");
lineReader.eachLine(rcFile, function (line) {
var res = runInsect(Insect.fmtPlain, line);
// We really only care when it breaks
if (res && res.msgType === "error") {
console.error(res.msg);
process.exit(1);
}
}, function (err) {
// If the file doesn't exist, that's fine
if (err && err.code !== "ENOENT") {
throw err;
} else {
startInsect();
}
});
var rcFile = path.join(os.homedir(), ".insectrc");
lineReader.eachLine(rcFile, function (line) {
var res = runInsect(Insect.fmtPlain, line);
// We really only care when it breaks
if (res && res.msgType === "error") {
console.error(res.msg);
process.exit(1);
}
}, function (err) {
// If the file doesn't exist, that's fine
if (err && err.code !== "ENOENT") {
throw err;
} else {
startInsect();
}
});
})();
} else {
startInsect();
}
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@
"test": "test"
},
"files": [
"output",
"insect.js",
"web/media/insect.png"
],
"scripts": {
"setup": "npm run build",
"start": "node copy && run-p server esbuild:watch",
"esbuild:watch": "spago build -w --then 'npm run esbuild'",
"esbuild": "esbuild output/Insect/index.js --bundle --minify --outfile=web/insect.js --target=es6 --global-name=Insect",
"esbuild:watch": "spago build -w --then 'npm run esbuild:browser'",
"esbuild": "npm run esbuild:browser && npm run esbuild:node",
"esbuild:browser": "esbuild output/Insect/index.js --bundle --minify --outfile=web/insect.js --target=es6 --global-name=Insect",
"esbuild:node": "esbuild output/Insect/index.js --bundle --minify --outfile=insect.js --target=node12 --platform=node --format=esm --external:./node_modules/*",
"build": "spago build && npm run esbuild && node copy",
"prepack": "npm run build",
"prepack": "spago build && npm run esbuild:node",
"server": "live-server web --open",
"test": "spago test"
},
Expand Down

0 comments on commit 3944a20

Please sign in to comment.