Skip to content

Commit

Permalink
feat: add ignore option to ignore files from build (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikescops authored Jun 4, 2024
1 parent a217727 commit 54ae1ee
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 32 deletions.
36 changes: 26 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,23 @@ pkg [options] <input>

Examples:

Makes executables for Linux, macOS and Windows
- Makes executables for Linux, macOS and Windows
$ pkg index.js
Takes package.json from cwd and follows 'bin' entry
- Takes package.json from cwd and follows 'bin' entry
$ pkg .
Makes executable for particular target machine
- Makes executable for particular target machine
$ pkg -t node16-win-arm64 index.js
Makes executables for target machines of your choice
- Makes executables for target machines of your choice
$ pkg -t node16-linux,node18-linux,node16-win index.js
Bakes '--expose-gc' and '--max-heap-size=34' into executable
- Bakes '--expose-gc' and '--max-heap-size=34' into executable
$ pkg --options "expose-gc,max-heap-size=34" index.js
Consider packageA and packageB to be public
- Consider packageA and packageB to be public
$ pkg --public-packages "packageA,packageB" index.js
Consider all packages to be public
- Consider all packages to be public
$ pkg --public-packages "*" index.js
Bakes '--expose-gc' into executable
- Bakes '--expose-gc' into executable
$ pkg --options expose-gc index.js
reduce size of the data packed inside the executable with GZip
- reduce size of the data packed inside the executable with GZip
$ pkg --compress GZip index.js
```

Expand Down Expand Up @@ -181,6 +181,22 @@ See also
[Detecting assets in source code](#detecting-assets-in-source-code) and
[Snapshot filesystem](#snapshot-filesystem).

### Ignore files

`ignore` is a list of globs. Files matching the paths specified as `ignore`
will be excluded from the final executable.

This is useful when you want to exclude some files from the final executable,
like tests, documentation or build files that could have been included by a dependency.

```json
"pkg": {
"ignore": [ "**/*/dependency-name/build.c" ]
}
```

To see if you have unwanted files in your executable, read the [Exploring virtual file system embedded in debug mode](#exploring-virtual-file-system-embedded-in-debug-mode) section.

### Options

Node.js application can be called with runtime options
Expand Down Expand Up @@ -413,7 +429,7 @@ printenv | grep NODE

## Advanced

### exploring virtual file system embedded in debug mode
### Exploring virtual file system embedded in debug mode

When you are using the `--debug` flag when building your executable,
`pkg` add the ability to display the content of the virtual file system
Expand Down
3 changes: 3 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import walk, { Marker, WalkerParams } from './walker';
import { Target, NodeTarget, SymLinks } from './types';
import { CompressType } from './compress_type';
import { patchMachOExecutable, signMachOExecutable } from './mach-o';
import pkgOptions from './options';

const { version } = JSON.parse(
readFileSync(path.join(__dirname, '../package.json'), 'utf-8'),
Expand Down Expand Up @@ -598,12 +599,14 @@ export async function exec(argv2: string[]) {
let marker: Marker;

if (configJson) {
pkgOptions.set(configJson?.pkg);
marker = {
config: configJson,
base: path.dirname(config),
configPath: config,
};
} else {
pkgOptions.set(inputJson?.pkg);
marker = {
config: inputJson || {}, // not `inputBin` because only `input`
base: path.dirname(input), // is the place for `inputJson`
Expand Down
23 changes: 23 additions & 0 deletions lib/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { PkgOptions } from './types';

class Options {
private options: PkgOptions;

constructor() {
this.options = {
dictionary: {},
};
}

public set(options: PkgOptions): void {
this.options = options ?? this.options;
}

public get(): PkgOptions {
return this.options;
}
}

const options = new Options();

export default options;
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface PkgOptions {
scripts?: string[];
log?: (logger: typeof log, context: Record<string, string>) => void;
assets?: string[];
ignore?: string[];
deployFiles?: string[];
patches?: Patches;
dictionary: ConfigDictionary;
Expand Down
21 changes: 20 additions & 1 deletion lib/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import isCore from 'is-core-module';
import globby from 'globby';
import path from 'path';
import chalk from 'chalk';
import { minimatch } from 'minimatch';

import {
ALIAS_AS_RELATIVE,
Expand Down Expand Up @@ -33,6 +34,7 @@ import {
PackageJson,
SymLinks,
} from './types';
import pkgOptions from './options';

export interface Marker {
hasDictionary?: boolean;
Expand Down Expand Up @@ -450,6 +452,19 @@ class Walker {
assert(typeof task.file === 'string');
const realFile = toNormalizedRealPath(task.file);

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));

if (match) {
log.debug(
`Ignoring file: ${realFile} due to top level config ignore pattern`,
);
return;
}
}

if (realFile === task.file) {
this.append(task);
return;
Expand Down Expand Up @@ -747,7 +762,11 @@ class Walker {

const catchPackageFilter = (config: PackageJson, base: string) => {
const newPackage = newPackages[newPackages.length - 1];
newPackage.marker = { config, configPath: newPackage.packageJson, base };
newPackage.marker = {
config,
configPath: newPackage.packageJson,
base,
};
};

let newFile = '';
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"globby": "^11.1.0",
"into-stream": "^6.0.0",
"is-core-module": "2.9.0",
"minimatch": "9.0.4",
"minimist": "^1.2.6",
"multistream": "^4.1.0",
"prebuild-install": "7.1.1",
Expand Down
11 changes: 6 additions & 5 deletions test/test-10-pnpm/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ if (utils.shouldSkipPnpm()) {

assert(__dirname === process.cwd());

const isWindows = process.platform === 'win32';
const target = process.argv[2] || 'host';
const input = './test.js';
const output = './test-output.exe';
Expand All @@ -23,14 +24,14 @@ console.log('target = ', target);
utils.vacuum.sync('./node_modules');
utils.vacuum.sync('./pnpm-lock.yaml');

const npmlog = utils.exec.sync('npm install -g pnpm@8');
console.log('npm log :', npmlog);

// launch `pnpm install`
const pnpmlog = utils.spawn.sync(
path.join(
path.dirname(process.argv[0]),
'npx' + (process.platform === 'win32' ? '.cmd' : ''),
),
path.join(path.dirname(process.argv[0]), 'npx' + (isWindows ? '.cmd' : '')),
['pnpm', 'install'],
{ cwd: path.dirname(output), expect: 0 },
{ cwd: path.dirname(output), expect: 0, shell: isWindows },
);
console.log('pnpm log :', pnpmlog);

Expand Down
11 changes: 6 additions & 5 deletions test/test-11-pnpm/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if (utils.shouldSkipPnpm()) {
console.log(__dirname, process.cwd());
assert(__dirname === process.cwd());

const isWindows = process.platform === 'win32';
const target = process.argv[2] || 'host';
const input = './test.js';
const output = './test-output.exe';
Expand All @@ -24,14 +25,14 @@ console.log('target = ', target);
utils.vacuum.sync('./node_modules');
utils.vacuum.sync('./pnpm-lock.yaml');

const npmlog = utils.exec.sync('npm install -g pnpm@8');
console.log('npm log :', npmlog);

// launch `pnpm install`
const pnpmlog = utils.spawn.sync(
path.join(
path.dirname(process.argv[0]),
'npx' + (process.platform === 'win32' ? '.cmd' : ''),
),
path.join(path.dirname(process.argv[0]), 'npx' + (isWindows ? '.cmd' : '')),
['pnpm', 'install'],
{ cwd: path.dirname(output), expect: 0 },
{ cwd: path.dirname(output), expect: 0, shell: isWindows },
);
console.log('pnpm log :', pnpmlog);

Expand Down
1 change: 1 addition & 0 deletions test/test-50-ignore-files/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!node_modules
47 changes: 47 additions & 0 deletions test/test-50-ignore-files/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env node

'use strict';

const path = require('path');
const assert = require('assert');
const utils = require('../utils.js');
const standard = 'stdout';

assert(!module.parent);
assert(__dirname === process.cwd());

const target = process.argv[2] || 'host';
const output = './test-output.exe';

let left, right;
utils.mkdirp.sync(path.dirname(output));

left = utils.spawn.sync('node', ['test-x-index.js']);

const inspect =
standard === 'stdout'
? ['inherit', 'pipe', 'inherit']
: ['inherit', 'inherit', 'pipe'];

const log = utils.pkg.sync(
['--target', target, '--output', output, '.', '--debug'],
inspect,
);

assert(
log.indexOf('useless.c due to top level config ignore pattern') > 0,
'useless.c file is not ignored',
);
assert(
log.indexOf(
'needed.c is added to queue',
'needed.c file is not added to queue',
),
);

right = utils.spawn.sync('./' + path.basename(output), [], {
cwd: path.dirname(output),
});

assert.strictEqual(left, right);
utils.vacuum.sync(output);
3 changes: 3 additions & 0 deletions test/test-50-ignore-files/node_modules/delta/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/test-50-ignore-files/node_modules/delta/needed.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/test-50-ignore-files/node_modules/delta/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/test-50-ignore-files/node_modules/delta/useless.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions test/test-50-ignore-files/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"bin": "test-x-index.js",
"license": "MIT",
"dependencies": {
"delta": "*"
},
"pkg": {
"ignore": [
"**/*/node_modules/delta/useless.c"
]
}
}
5 changes: 5 additions & 0 deletions test/test-50-ignore-files/test-x-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

var dataPath = 'delta';
require(dataPath);
console.log(global.FOO);
20 changes: 9 additions & 11 deletions test/test-80-compression-node-opcua/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const assert = require('assert');
const utils = require('../utils.js');
const pkgJson = require('./package.json');

const isWindows = process.platform === 'win32';
const buildDir = 'build';

assert(!module.parent);
Expand All @@ -32,14 +33,14 @@ function clean() {
// remove any possible left-over
clean();

const npmlog = utils.exec.sync('npm install -g pnpm@8');
console.log('npm log :', npmlog);

// launch `pnpm install`
const pnpmlog = utils.spawn.sync(
path.join(
path.dirname(process.argv[0]),
'npx' + (process.platform === 'win32' ? '.cmd' : ''),
),
path.join(path.dirname(process.argv[0]), 'npx' + (isWindows ? '.cmd' : '')),
['pnpm', 'install'],
{ cwd: path.dirname(__filename), expect: 0 },
{ cwd: path.dirname(__filename), expect: 0, shell: isWindows },
);
console.log('pnpm log :', pnpmlog);

Expand All @@ -54,7 +55,7 @@ assert(
/* eslint-disable no-unused-vars */
const input = 'package.json';
const target = process.argv[2] || 'host';
const ext = process.platform === 'win32' ? '.exe' : '';
const ext = isWindows ? '.exe' : '';
const outputRef = path.join(buildDir, 'test-output-empty' + ext);
const outputNone = path.join(buildDir, 'test-output-None' + ext);
const outputGZip = path.join(buildDir, 'test-output-GZip' + ext);
Expand Down Expand Up @@ -95,18 +96,15 @@ function pkgCompress(compressMode, output) {

function esbuildBuild(entryPoint) {
const log = utils.spawn.sync(
path.join(
path.dirname(process.argv[0]),
'npx' + (process.platform === 'win32' ? '.cmd' : ''),
),
path.join(path.dirname(process.argv[0]), 'npx' + (isWindows ? '.cmd' : '')),
[
'esbuild',
entryPoint,
'--bundle',
'--outfile=' + path.join(buildDir, pkgJson.main),
'--platform=node',
],
{ cwd: __dirname, expect: 0 },
{ cwd: __dirname, expect: 0, shell: isWindows },
);

console.log(log);
Expand Down
Loading

0 comments on commit 54ae1ee

Please sign in to comment.