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

feat(zip): make bestzip an off-by-default option #187

Merged
merged 1 commit into from
Sep 18, 2021
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ module.exports = (serverless) => {
};
```

### Native Zip

If you wish to use your system's `zip` executable to create archives (can be significantly faster when working with many large archives), set the `nativeZip` option:

```yml
custom:
esbuild:
nativeZip: true
```

**NOTE:*** This will produce non-deterministic archives which causes a Serverless deployment update on every deploy.

## Usage

### Automatic compilation
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"devDependencies": {
"@commitlint/cli": "^9.1.1",
"@commitlint/config-conventional": "^9.1.1",
"@types/archiver": "^5.1.1",
"@types/fs-extra": "^9.0.1",
"@types/jest": "^26.0.14",
"@types/node": "^12.12.38",
Expand All @@ -60,6 +61,7 @@
"typescript": "^4.0.3"
},
"dependencies": {
"archiver": "^5.3.0",
"bestzip": "^2.2.0",
"chokidar": "^3.4.3",
"esbuild": ">=0.8",
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ export interface PackagerOptions {
scripts?: string[] | string;
}

export interface Configuration extends Omit<BuildOptions, 'watch' | 'plugins'> {
export interface Configuration extends Omit<BuildOptions, 'nativeZip' | 'watch' | 'plugins'> {
packager: 'npm' | 'yarn';
packagePath: string;
exclude: string[];
nativeZip: boolean;
watch: WatchConfiguration;
plugins?: string;
keepOutputDirectory?: boolean;
Expand All @@ -49,6 +50,7 @@ const DEFAULT_BUILD_OPTIONS: Partial<Configuration> = {
target: 'node10',
external: [],
exclude: ['aws-sdk'],
nativeZip: false,
packager: 'npm',
watch: {
pattern: './**/*.(js|ts)',
Expand Down Expand Up @@ -256,6 +258,7 @@ export class EsbuildServerlessPlugin implements ServerlessPlugin {

// esbuild v0.7.0 introduced config options validation, so I have to delete plugin specific options from esbuild config.
delete config['exclude'];
delete config['nativeZip'];
delete config['packager'];
delete config['packagePath'];
delete config['watch'];
Expand Down
4 changes: 2 additions & 2 deletions src/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export async function pack(this: EsbuildServerlessPlugin) {
)(files);

const startZip = Date.now();
await zip(artifactPath, filesPathList);
await zip(artifactPath, filesPathList, this.buildOptions.nativeZip);
const { size } = fs.statSync(artifactPath);

this.serverless.cli.log(
Expand Down Expand Up @@ -157,7 +157,7 @@ export async function pack(this: EsbuildServerlessPlugin) {
}));

const startZip = Date.now();
await zip(artifactPath, filesPathList);
await zip(artifactPath, filesPathList, this.buildOptions.nativeZip);

const { size } = fs.statSync(artifactPath);

Expand Down
58 changes: 47 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { bestzip } from 'bestzip';
import * as archiver from 'archiver';
import * as childProcess from 'child_process';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as os from 'os';
import { join } from 'ramda';
import { IFiles, IFile } from './types';
import { IFiles } from './types';

export class SpawnError extends Error {
constructor(message: string, public stdout: string, public stderr: string) {
Expand Down Expand Up @@ -103,7 +104,11 @@ export const humanSize = (size: number) => {
return `${sanitized} ${['B', 'KB', 'MB', 'GB', 'TB'][i]}`;
};

export const zip = async (zipPath: string, filesPathList: IFiles) => {
export const zip = async (
zipPath: string,
filesPathList: IFiles,
useNativeZip = false
): Promise<void> => {
// create a temporary directory to hold the final zip structure
const tempDirName = `${path.basename(zipPath).slice(0, -4)}-${Date.now().toString()}`;
const tempDirPath = path.join(os.tmpdir(), tempDirName);
Expand All @@ -117,17 +122,48 @@ export const zip = async (zipPath: string, filesPathList: IFiles) => {
// prepare zip folder
fs.mkdirpSync(path.dirname(zipPath));

// zip the temporary directory
const result = await bestzip({
source: '*',
destination: zipPath,
cwd: tempDirPath,
});
if (useNativeZip) {
// zip the temporary directory
await bestzip({
source: '*',
destination: zipPath,
cwd: tempDirPath,
});

// delete the temporary folder
fs.rmdirSync(tempDirPath, { recursive: true });
} else {
const zip = archiver.create('zip');
const output = fs.createWriteStream(zipPath);

// write zip
output.on('open', () => {
zip.pipe(output);

filesPathList.forEach((file) => {
const stats = fs.statSync(file.rootPath);
if (stats.isDirectory()) return;

// delete the temporary folder
fs.rmdirSync(tempDirPath, { recursive: true });
zip.append(fs.readFileSync(file.rootPath), {
name: file.localPath,
mode: stats.mode,
date: new Date(0), // necessary to get the same hash when zipping the same content
});
});

return result;
zip.finalize();
});

return new Promise((resolve, reject) => {
output.on('close', () => {
// delete the temporary folder
fs.rmdirSync(tempDirPath, { recursive: true });

resolve();
});
zip.on('error', (err) => reject(err));
});
}
};

export function trimExtension(entry: string) {
Expand Down