Skip to content

Commit

Permalink
feat: use esbuild-visualizer programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
adriencaccia committed Jan 28, 2023
1 parent a63f7ed commit 1d2d60f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 87 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A Serverless plugin to analyze the bundle of a lambda function.
Install with

```bash
yarn add --dev serverless-analyze-bundle-plugin esbuild-visualizer
yarn add --dev serverless-analyze-bundle-plugin
```

Add `serverless-analyze-bundle-plugin` to your serverless plugins.
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
"bugs": "https://github.com/adriencaccia/serverless-analyze-bundle-plugin/issues",
"dependencies": {
"@babel/runtime": "^7.18.6",
"check-node-version": "^4.2.1",
"node-stream-zip": "^1.15.0"
"esbuild-visualizer": "^0.4.0",
"node-stream-zip": "^1.15.0",
"open": "^8.4.0"
},
"devDependencies": {
"@babel/cli": "^7.18.6",
Expand Down Expand Up @@ -62,7 +63,6 @@
},
"homepage": "https://github.com/adriencaccia/serverless-analyze-bundle-plugin#readme",
"peerDependencies": {
"esbuild-visualizer": "^0.3.1",
"serverless": "^2.60 || ^3"
},
"repository": "adriencaccia/serverless-analyze-bundle-plugin.git",
Expand Down
56 changes: 18 additions & 38 deletions plugin/bundleVisualizer.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
import check from 'check-node-version';
import { exec } from 'child_process';
import { readdirSync, statSync } from 'fs';
import { tmpdir } from 'os';
import { Metadata, visualizer } from 'esbuild-visualizer';
import { mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'fs';
import { mkdir } from 'fs/promises';
import { join, normalize, parse } from 'path';
import { FunctionDefinitionHandler } from 'serverless';
import StreamZip from 'node-stream-zip';

import opn from 'open';
import { tmpdir } from 'os';
import { dirname, join, normalize, parse } from 'path';
import { FunctionDefinitionHandler } from 'serverless';
import type { ServerlessAnalyzeBundlePlugin } from './serverlessAnalyzeBundle';

const pExec = (command: string) =>
new Promise((resolve, reject) => {
// eslint-disable-next-line max-params
exec(command, (error, stdout, stderr) => {
if (stderr) {
console.error(`stderr: ${stderr}`);
}

if (error) {
reject(error);
}

resolve(stdout);
});
});

const getAllFiles = function (dirPath: string, arrayOfFilesInput: string[] = []) {
const files = readdirSync(dirPath);

Expand Down Expand Up @@ -87,23 +70,20 @@ async function bundleVisualizer(this: ServerlessAnalyzeBundlePlugin): Promise<vo
return;
}

const commandArray = [
'--metadata',
metafileName,
'--filename',
`${TEMP_DIR_LOCATION}/${functionName}.html`,
'--open',
];

check({ yarn: '>=2' }, err => {
if (err !== null) {
commandArray.unshift('node_modules/.bin/esbuild-visualizer');
} else {
commandArray.unshift('yarn', 'esbuild-visualizer');
}
const textContent = readFileSync(metafileName, { encoding: 'utf-8' });
const jsonContent = JSON.parse(textContent) as Metadata;

void pExec(commandArray.join(' '));
const fileContent = await visualizer(jsonContent, {
title: `${functionName} function bundle visualizer `,
template: 'treemap',
});

const filename = `${TEMP_DIR_LOCATION}/${functionName}.html`;

mkdirSync(dirname(filename), { recursive: true });
writeFileSync(filename, fileContent);

await opn(filename);
}

export default bundleVisualizer;
113 changes: 68 additions & 45 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2842,39 +2842,13 @@ __metadata:
languageName: node
linkType: hard

"chalk@npm:^3.0.0":
version: 3.0.0
resolution: "chalk@npm:3.0.0"
dependencies:
ansi-styles: ^4.1.0
supports-color: ^7.1.0
checksum: 8e3ddf3981c4da405ddbd7d9c8d91944ddf6e33d6837756979f7840a29272a69a5189ecae0ff84006750d6d1e92368d413335eab4db5476db6e6703a1d1e0505
languageName: node
linkType: hard

"chardet@npm:^0.7.0":
version: 0.7.0
resolution: "chardet@npm:0.7.0"
checksum: 6fd5da1f5d18ff5712c1e0aed41da200d7c51c28f11b36ee3c7b483f3696dabc08927fc6b227735eb8f0e1215c9a8abd8154637f3eff8cada5959df7f58b024d
languageName: node
linkType: hard

"check-node-version@npm:^4.2.1":
version: 4.2.1
resolution: "check-node-version@npm:4.2.1"
dependencies:
chalk: ^3.0.0
map-values: ^1.0.1
minimist: ^1.2.0
object-filter: ^1.0.2
run-parallel: ^1.1.4
semver: ^6.3.0
bin:
check-node-version: bin.js
checksum: a559236ce202125decfc51ffa9be5cfa46f2b791e0c0e7e41c306fb7c0729a4641669228c4b95749e5f518608609ba6cf3b7963116b4e2d49bd45e18c6bd9cb4
languageName: node
linkType: hard

"child-process-ext@npm:^2.1.1":
version: 2.1.1
resolution: "child-process-ext@npm:2.1.1"
Expand Down Expand Up @@ -3022,6 +2996,17 @@ __metadata:
languageName: node
linkType: hard

"cliui@npm:^8.0.1":
version: 8.0.1
resolution: "cliui@npm:8.0.1"
dependencies:
string-width: ^4.2.0
strip-ansi: ^6.0.1
wrap-ansi: ^7.0.0
checksum: 79648b3b0045f2e285b76fb2e24e207c6db44323581e421c3acbd0e86454cba1b37aea976ab50195a49e7384b871e6dfb2247ad7dec53c02454ac6497394cb56
languageName: node
linkType: hard

"clone-response@npm:^1.0.2":
version: 1.0.2
resolution: "clone-response@npm:1.0.2"
Expand Down Expand Up @@ -3749,6 +3734,13 @@ __metadata:
languageName: node
linkType: hard

"define-lazy-prop@npm:^2.0.0":
version: 2.0.0
resolution: "define-lazy-prop@npm:2.0.0"
checksum: 0115fdb065e0490918ba271d7339c42453d209d4cb619dfe635870d906731eff3e1ade8028bb461ea27ce8264ec5e22c6980612d332895977e89c1bbc80fcee2
languageName: node
linkType: hard

"define-properties@npm:^1.1.3, define-properties@npm:^1.1.4":
version: 1.1.4
resolution: "define-properties@npm:1.1.4"
Expand Down Expand Up @@ -4068,6 +4060,18 @@ __metadata:
languageName: node
linkType: hard

"esbuild-visualizer@npm:^0.4.0":
version: 0.4.0
resolution: "esbuild-visualizer@npm:0.4.0"
dependencies:
open: ^8.4.0
yargs: ^17.6.2
bin:
esbuild-visualizer: dist/bin/cli.js
checksum: 115347f3d0a51eda47e5c525097932daff5d8ea64dd99944e7934757abebbad78ae825c554482d1079a1a4ef3991dada30395cb35bea7dec2e3a243a320899f5
languageName: node
linkType: hard

"escalade@npm:^3.1.1":
version: 3.1.1
resolution: "escalade@npm:3.1.1"
Expand Down Expand Up @@ -5514,7 +5518,7 @@ __metadata:
languageName: node
linkType: hard

"is-docker@npm:^2.0.0, is-docker@npm:^2.2.1":
"is-docker@npm:^2.0.0, is-docker@npm:^2.1.1, is-docker@npm:^2.2.1":
version: 2.2.1
resolution: "is-docker@npm:2.2.1"
bin:
Expand Down Expand Up @@ -5716,7 +5720,7 @@ __metadata:
languageName: node
linkType: hard

"is-wsl@npm:^2.1.1":
"is-wsl@npm:^2.1.1, is-wsl@npm:^2.2.0":
version: 2.2.0
resolution: "is-wsl@npm:2.2.0"
dependencies:
Expand Down Expand Up @@ -6329,13 +6333,6 @@ __metadata:
languageName: node
linkType: hard

"map-values@npm:^1.0.1":
version: 1.0.1
resolution: "map-values@npm:1.0.1"
checksum: 0ab60d451f4708861641dbb965e27b645c717c68083ff5927b7ad8473bd0bfb6069a561b6a6d83fa3a757fdb55b5772cfc07fd39a9956194f3b1532ad18478e3
languageName: node
linkType: hard

"memoizee@npm:^0.4.14, memoizee@npm:^0.4.15":
version: 0.4.15
resolution: "memoizee@npm:0.4.15"
Expand Down Expand Up @@ -6812,13 +6809,6 @@ __metadata:
languageName: node
linkType: hard

"object-filter@npm:^1.0.2":
version: 1.0.2
resolution: "object-filter@npm:1.0.2"
checksum: 9542bb51d33b76353587c6e49e3e1d7d8e8c00484b19c6b3d63c2bc7777873e50b842f9588cd506dcb138add559ee7a5dafb7951f6e8df40af1a9558c08412cb
languageName: node
linkType: hard

"object-hash@npm:^2.2.0":
version: 2.2.0
resolution: "object-hash@npm:2.2.0"
Expand Down Expand Up @@ -6891,6 +6881,17 @@ __metadata:
languageName: node
linkType: hard

"open@npm:^8.4.0":
version: 8.4.0
resolution: "open@npm:8.4.0"
dependencies:
define-lazy-prop: ^2.0.0
is-docker: ^2.1.1
is-wsl: ^2.2.0
checksum: e9545bec64cdbf30a0c35c1bdc310344adf8428a117f7d8df3c0af0a0a24c513b304916a6d9b11db0190ff7225c2d578885080b761ed46a3d5f6f1eebb98b63c
languageName: node
linkType: hard

"optionator@npm:^0.9.1":
version: 0.9.1
resolution: "optionator@npm:0.9.1"
Expand Down Expand Up @@ -7756,7 +7757,7 @@ __metadata:
languageName: node
linkType: hard

"run-parallel@npm:^1.1.4, run-parallel@npm:^1.1.9":
"run-parallel@npm:^1.1.9":
version: 1.2.0
resolution: "run-parallel@npm:1.2.0"
dependencies:
Expand Down Expand Up @@ -7877,23 +7878,23 @@ __metadata:
"@typescript-eslint/parser": ^5.30.6
"@zerollup/ts-transform-paths": ^1.7.18
babel-plugin-module-resolver: ^4.1.0
check-node-version: ^4.2.1
concurrently: ^7.2.2
esbuild-visualizer: ^0.4.0
eslint: ^8.19.0
eslint-config-prettier: ^8.5.0
eslint-plugin-import: ^2.26.0
eslint-plugin-prettier: ^4.2.1
husky: ^8.0.1
lint-staged: ^12.5.0
node-stream-zip: ^1.15.0
open: ^8.4.0
prettier: ^2.7.1
serverless: ^3.21.0
standard-version: ^9.5.0
syncpack: ^8.2.4
ttypescript: ^1.5.13
typescript: ^4.7.4
peerDependencies:
esbuild-visualizer: ^0.3.1
serverless: ^2.60 || ^3
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -9310,6 +9311,13 @@ __metadata:
languageName: node
linkType: hard

"yargs-parser@npm:^21.1.1":
version: 21.1.1
resolution: "yargs-parser@npm:21.1.1"
checksum: ed2d96a616a9e3e1cc7d204c62ecc61f7aaab633dcbfab2c6df50f7f87b393993fe6640d017759fe112d0cb1e0119f2b4150a87305cc873fd90831c6a58ccf1c
languageName: node
linkType: hard

"yargs@npm:^16.0.0, yargs@npm:^16.2.0":
version: 16.2.0
resolution: "yargs@npm:16.2.0"
Expand Down Expand Up @@ -9355,6 +9363,21 @@ __metadata:
languageName: node
linkType: hard

"yargs@npm:^17.6.2":
version: 17.6.2
resolution: "yargs@npm:17.6.2"
dependencies:
cliui: ^8.0.1
escalade: ^3.1.1
get-caller-file: ^2.0.5
require-directory: ^2.1.1
string-width: ^4.2.3
y18n: ^5.0.5
yargs-parser: ^21.1.1
checksum: 47da1b0d854fa16d45a3ded57b716b013b2179022352a5f7467409da5a04a1eef5b3b3d97a2dfc13e8bbe5f2ffc0afe3bc6a4a72f8254e60f5a4bd7947138643
languageName: node
linkType: hard

"yauzl@npm:^2.4.2":
version: 2.10.0
resolution: "yauzl@npm:2.10.0"
Expand Down

0 comments on commit 1d2d60f

Please sign in to comment.