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

refactor: cleanup dependencies and reduce bundle size #99

Closed
wants to merge 12 commits into from
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ use of `package.json` configuration.

### Scripts

`scripts` is a [glob](https://github.com/sindresorhus/globby)
`scripts` is a [glob](https://github.com/SuperchupuDev/tinyglobby)
or list of globs. Files specified as `scripts` will be compiled
using `v8::ScriptCompiler` and placed into executable without
sources. They must conform to the JS standards of those Node.js versions
you target (see [Targets](#targets)), i.e. be already transpiled.

### Assets

`assets` is a [glob](https://github.com/sindresorhus/globby)
`assets` is a [glob](https://github.com/SuperchupuDev/tinyglobby)
or list of globs. Files specified as `assets` will be packaged
into executable as raw content without modifications. Javascript
files may also be specified as `assets`. Their sources will
Expand Down
2 changes: 1 addition & 1 deletion lib/chmod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { chmod, stat } from 'fs-extra';
import { chmod, stat } from 'fs/promises';

export async function plusx(file: string) {
const s = await stat(file);
Expand Down
6 changes: 6 additions & 0 deletions lib/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import picocolors from 'picocolors';
import { WriteStream } from 'tty';

// makes color detection more accurate using node's own API for it
// https://github.com/alexeyraspopov/picocolors/issues/85
export const pc = picocolors.createColors(WriteStream.prototype.hasColors());
48 changes: 22 additions & 26 deletions lib/help.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import chalk from 'chalk';
import { pc } from './colors';

export default function help() {
// eslint-disable-next-line no-console
console.log(`
${chalk.bold('pkg')} [options] <input>
${pc.bold('pkg')} [options] <input>

${chalk.dim('Options:')}
${pc.dim('Options:')}

-h, --help output usage information
-v, --version output pkg version
Expand All @@ -23,29 +23,25 @@ export default function help() {
--no-dict comma-separated list of packages names to ignore dictionaries. Use --no-dict * to disable all dictionaries
-C, --compress [default=None] compression algorithm = Brotli or GZip

${chalk.dim('Examples:')}
${pc.dim('Examples:')}

${chalk.gray('–')} Makes executables for Linux, macOS and Windows
${chalk.cyan('$ pkg index.js')}
${chalk.gray('–')} Takes package.json from cwd and follows 'bin' entry
${chalk.cyan('$ pkg .')}
${chalk.gray('–')} Makes executable for particular target machine
${chalk.cyan('$ pkg -t node14-win-arm64 index.js')}
${chalk.gray('–')} Makes executables for target machines of your choice
${chalk.cyan('$ pkg -t node16-linux,node18-linux,node18-win index.js')}
${chalk.gray(
'–',
)} Bakes '--expose-gc' and '--max-heap-size=34' into executable
${chalk.cyan('$ pkg --options "expose-gc,max-heap-size=34" index.js')}
${chalk.gray('–')} Consider packageA and packageB to be public
${chalk.cyan('$ pkg --public-packages "packageA,packageB" index.js')}
${chalk.gray('–')} Consider all packages to be public
${chalk.cyan('$ pkg --public-packages "*" index.js')}
${chalk.gray('–')} Bakes '--expose-gc' into executable
${chalk.cyan('$ pkg --options expose-gc index.js')}
${chalk.gray(
'–',
)} reduce size of the data packed inside the executable with GZip
${chalk.cyan('$ pkg --compress GZip index.js')}
${pc.gray('–')} Makes executables for Linux, macOS and Windows
${pc.cyan('$ pkg index.js')}
${pc.gray('–')} Takes package.json from cwd and follows 'bin' entry
${pc.cyan('$ pkg .')}
${pc.gray('–')} Makes executable for particular target machine
${pc.cyan('$ pkg -t node14-win-arm64 index.js')}
${pc.gray('–')} Makes executables for target machines of your choice
${pc.cyan('$ pkg -t node16-linux,node18-linux,node18-win index.js')}
${pc.gray('–')} Bakes '--expose-gc' and '--max-heap-size=34' into executable
${pc.cyan('$ pkg --options "expose-gc,max-heap-size=34" index.js')}
${pc.gray('–')} Consider packageA and packageB to be public
${pc.cyan('$ pkg --public-packages "packageA,packageB" index.js')}
${pc.gray('–')} Consider all packages to be public
${pc.cyan('$ pkg --public-packages "*" index.js')}
${pc.gray('–')} Bakes '--expose-gc' into executable
${pc.cyan('$ pkg --options expose-gc index.js')}
${pc.gray('–')} reduce size of the data packed inside the executable with GZip
${pc.cyan('$ pkg --compress GZip index.js')}
`);
}
18 changes: 5 additions & 13 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
/* eslint-disable require-atomic-updates */

import assert from 'assert';
import {
existsSync,
mkdirp,
readFile,
remove,
stat,
readFileSync,
writeFileSync,
copyFileSync,
} from 'fs-extra';
import { existsSync, readFileSync, writeFileSync, copyFileSync } from 'fs';
import { mkdir, readFile, rm, stat } from 'fs/promises';
import minimist from 'minimist';
import { need, system } from '@yao-pkg/pkg-fetch';
import path from 'path';
Expand Down Expand Up @@ -574,7 +566,7 @@ export async function exec(argv2: string[]) {
// ad-hoc sign the base binary temporarily to generate bytecode
// due to the new mandatory signing requirement
const signedBinaryPath = `${f.binaryPath}-signed`;
await remove(signedBinaryPath);
await rm(signedBinaryPath, { recursive: true, force: true });
copyFileSync(f.binaryPath, signedBinaryPath);
try {
signMachOExecutable(signedBinaryPath);
Expand Down Expand Up @@ -665,14 +657,14 @@ export async function exec(argv2: string[]) {
for (const target of targets) {
if (target.output && existsSync(target.output)) {
if ((await stat(target.output)).isFile()) {
await remove(target.output);
await rm(target.output, { recursive: true, force: true });
} else {
throw wasReported('Refusing to overwrite non-file output', [
target.output,
]);
}
} else if (target.output) {
await mkdirp(path.dirname(target.output));
await mkdir(path.dirname(target.output), { recursive: true });
}

await producer({
Expand Down
15 changes: 8 additions & 7 deletions lib/packer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable complexity */

import assert from 'assert';
import fs from 'fs-extra';
import { readFileSync } from 'fs';
import path from 'path';

import {
Expand All @@ -17,16 +17,17 @@ import { log, wasReported } from './log';
import { FileRecord, FileRecords, SymLinks } from './types';

const { version } = JSON.parse(
fs.readFileSync(path.join(__dirname, '../package.json'), 'utf-8'),
readFileSync(path.join(__dirname, '../package.json'), 'utf-8'),
);

const bootstrapText = fs
.readFileSync(require.resolve('../prelude/bootstrap.js'), 'utf8')
.replace('%VERSION%', version);
const bootstrapText = readFileSync(
require.resolve('../prelude/bootstrap.js'),
'utf8',
).replace('%VERSION%', version);

const commonText = fs.readFileSync(require.resolve('./common'), 'utf8');
const commonText = readFileSync(require.resolve('./common'), 'utf8');

const diagnosticText = fs.readFileSync(
const diagnosticText = readFileSync(
require.resolve('../prelude/diagnostic.js'),
'utf8',
);
Expand Down
4 changes: 2 additions & 2 deletions lib/producer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createBrotliCompress, createGzip } from 'zlib';
import Multistream from 'multistream';
import assert from 'assert';
import { execFileSync } from 'child_process';
import fs from 'fs-extra';
import fs from 'fs';
import intoStream from 'into-stream';
import path from 'path';
import streamMeter from 'stream-meter';
Expand Down Expand Up @@ -291,7 +291,7 @@ function nativePrebuildInstall(target: Target, nodeFile: string) {
fs.copyFileSync(nodeFile, nativeFile);

// put the backed up file back
fs.moveSync(`${nodeFile}.bak`, nodeFile, { overwrite: true });
fs.renameSync(`${nodeFile}.bak`, nodeFile);

return nativeFile;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/refiner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import chalk from 'chalk';
import { pc } from './colors';
import {
STORE_LINKS,
retrieveDenominator,
Expand Down Expand Up @@ -43,7 +43,7 @@ function purgeTopDirectories(records: FileRecords) {

if (links3) {
delete records[file];
log.debug(chalk.cyan('Deleting record file :', file));
log.debug(pc.cyan(`Deleting record file: ${file}`));
found = true;
}
}
Expand Down
17 changes: 9 additions & 8 deletions lib/walker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* eslint-disable require-atomic-updates */

import assert from 'assert';
import fs from 'fs-extra';
import globby from 'globby';
import fs from 'fs/promises';
import path from 'path';
import chalk from 'chalk';
import { minimatch } from 'minimatch';
import { builtinModules } from 'module';

import picomatch from 'picomatch';
import { globSync } from 'tinyglobby';
import {
ALIAS_AS_RELATIVE,
ALIAS_AS_RESOLVABLE,
Expand All @@ -23,6 +21,7 @@ import {
toNormalizedRealPath,
} from './common';

import { pc } from './colors';
import { follow } from './follow';
import { log, wasReported } from './log';
import * as detector from './detector';
Expand Down Expand Up @@ -194,7 +193,7 @@ function upon(p: string, base: string) {
}

function collect(ps: string[]) {
return globby.sync(ps, { dot: true });
return globSync(ps, { dot: true });
}

function expandFiles(efs: string | string[], base: string) {
Expand Down Expand Up @@ -468,7 +467,9 @@ class Walker {
const { ignore } = pkgOptions.get();
if (ignore) {
// check if the file matches one of the ignore regex patterns
const match = ignore.some((pattern) => minimatch(realFile, pattern));
const match = ignore.some((pattern) =>
picomatch.isMatch(realFile, pattern),
);

if (match) {
log.debug(
Expand Down Expand Up @@ -818,7 +819,7 @@ class Walker {
`%2: ${record.file}`,
]);
} else {
log[level](`${chalk.yellow(failure.message)} in ${record.file}`);
log[level](`${pc.yellow(failure.message)} in ${record.file}`);
}

return;
Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,24 @@
"@babel/parser": "7.23.0",
"@babel/types": "7.23.0",
"@yao-pkg/pkg-fetch": "3.5.13",
"chalk": "^4.1.2",
"fs-extra": "^9.1.0",
"globby": "^11.1.0",
"into-stream": "^6.0.0",
"minimatch": "9.0.4",
"minimist": "^1.2.6",
"multistream": "^4.1.0",
"picocolors": "^1.1.0",
"picomatch": "^4.0.2",
"prebuild-install": "7.1.1",
"resolve": "^1.22.0",
"stream-meter": "^1.0.4"
"stream-meter": "^1.0.4",
"tinyglobby": "^0.2.9"
},
"devDependencies": {
"@babel/core": "7.23.0",
"@release-it/conventional-changelog": "7.0.2",
"@types/babel__generator": "7.6.5",
"@types/fs-extra": "9.0.13",
"@types/minimatch": "^5.1.2",
"@types/minimist": "1.2.2",
"@types/multistream": "4.1.0",
"@types/node": "14.18.20",
"@types/picomatch": "^3.0.1",
"@types/resolve": "1.20.2",
"@types/stream-meter": "0.0.22",
"@typescript-eslint/eslint-plugin": "6.7.4",
Expand Down
19 changes: 10 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

'use strict';

const chalk = require('chalk');
const globby = require('globby');
const path = require('path');
const pc = require('picocolors');
const { globSync } = require('tinyglobby');
const utils = require('./utils.js');
const host = 'node' + process.version.match(/^v(\d+)/)[1];
let target = process.argv[2] || 'host';
Expand Down Expand Up @@ -47,6 +47,7 @@ function joinAndForward(d) {
}

const list = [];
const ignore = [];

if (flavor.match(/^test/)) {
list.push(joinAndForward(`${flavor}/main.js`));
Expand All @@ -55,14 +56,14 @@ if (flavor.match(/^test/)) {
} else {
list.push(joinAndForward('**/main.js'));
if (flavor === 'no-npm') {
list.push('!' + joinAndForward('test-42-fetch-all'));
list.push('!' + joinAndForward('test-46-multi-arch'));
list.push('!' + joinAndForward('test-46-multi-arch-2'));
list.push('!' + joinAndForward('test-79-npm'));
ignore.push(joinAndForward('test-42-fetch-all'));
ignore.push(joinAndForward('test-46-multi-arch'));
ignore.push(joinAndForward('test-46-multi-arch-2'));
ignore.push(joinAndForward('test-79-npm'));
}
}

const files = globby.sync(list);
const files = globSync(list, { ignore });

files.sort().some(function (file) {
file = path.resolve(file);
Expand All @@ -73,8 +74,8 @@ files.sort().some(function (file) {
});
} catch (error) {
console.log();
console.log(`> ${chalk.red('Error!')} ${error.message}`);
console.log(`> ${chalk.red('Error!')} ${file} FAILED (in ${target})`);
console.log(`> ${pc.red('Error!')} ${error.message}`);
console.log(`> ${pc.red('Error!')} ${file} FAILED (in ${target})`);
process.exit(2);
}
console.log(file, 'ok');
Expand Down
13 changes: 8 additions & 5 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ const assert = require('assert');
const path = require('path');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const globby = require('globby');
const { execSync } = require('child_process');
const { spawnSync } = require('child_process');
const { globSync } = require('tinyglobby');
const { execSync, spawnSync } = require('child_process');
const { existsSync, statSync, copyFileSync, readdirSync } = require('fs');
const stableStringify = require('json-stable-stringify');

Expand Down Expand Up @@ -140,6 +139,10 @@ module.exports.pkg.sync = function (args, opts) {
args.unshift(binPath);
assert(es5, 'RUN BABEL FIRST!'); // args.unshift('-r', 'babel-register');
if (Array.isArray(opts)) opts = { stdio: opts };
// spawn uses process.env if no opts.env is provided, we need to manually add it if set
if (!opts) opts = { env: { NO_COLOR: '1', ...process.env } };
else if (!opts.env || !opts.env.NO_COLOR)
opts.env = { NO_COLOR: '1', ...process.env, ...opts.env };
try {
const ss = module.exports.spawn.sync;
return ss('node', args, opts);
Expand All @@ -157,11 +160,11 @@ module.exports.filesBefore = function (n) {
for (const ni of n) {
module.exports.vacuum.sync(ni);
}
return globby.sync('**/*', { nodir: true }).sort();
return globSync('**/*').sort();
};

module.exports.filesAfter = function (b, n) {
const a = globby.sync('**/*', { nodir: true }).sort();
const a = globSync('**/*').sort();
for (const bi of b) {
if (a.indexOf(bi) < 0) {
assert(false, `${bi} disappeared!?`);
Expand Down
Loading
Loading