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: reduce sourcemaps and visualize dep tree #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ node_modules
.idea
dist/
.serverless/

.esbuild-visualizer
10 changes: 8 additions & 2 deletions cdk/lib/lambda-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export class LambdaStack extends cdk.Stack {
)
),
runtime: lambda.Runtime.NODEJS_18_X,
functionName: 'users-cdk-boilerplate'
functionName: 'users-cdk-boilerplate',
environment: {
NODE_OPTIONS: '--enable-source-maps',
}
});

const productsLambda = new lambda.Function(this, 'products-lambda', {
Expand All @@ -32,7 +35,10 @@ export class LambdaStack extends cdk.Stack {
)
),
runtime: lambda.Runtime.NODEJS_18_X,
functionName: 'products-cdk-boilerplate'
functionName: 'products-cdk-boilerplate',
environment: {
NODE_OPTIONS: '--enable-source-maps',
}
});
}
}
1 change: 1 addition & 0 deletions cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"watch": "tsc -w",
"test": "jest",
"cdk": "cdk",
"diff": "cdk diff",
"deploy": "cdk deploy"
},
"devDependencies": {
Expand Down
102 changes: 89 additions & 13 deletions esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as fs from 'fs';
import * as path from 'path';
import {build} from 'esbuild';
import {build, Plugin} from 'esbuild';
import {visualizer, TemplateType} from "esbuild-visualizer";
import * as open from "open";

const functionsDir = 'src';
const outDir = 'dist';
Expand All @@ -9,16 +11,90 @@ const entryPoints = fs
.filter(entry => entry !== 'common')
.map(entry => `${functionsDir}/${entry}/handler.ts`);

build({
entryPoints,
bundle: true,
outdir: path.join(__dirname, outDir),
outbase: functionsDir,
platform: 'node',
sourcemap: 'external',
write: true,
tsconfig: './tsconfig.json',
minify: true,
keepNames: false,

export type EsbuildFunctionBundlerOptions = {
/**
* Defaults to cdk.out/esbuild-visualizer
*/
outputDir?: string,

/**
* Defaults to "treemap"
*/
template?: TemplateType,

/**
* Open the HTML file after bundling
*/
open?: boolean
}

function esBuildPluginShrinkSourceMap(): Plugin
{
//https://github.com/evanw/esbuild/issues/1685#issuecomment-944916409
return {
name: 'excludeVendorFromSourceMap',
setup(build) {
build.onLoad({ filter: /node_modules/ }, args => {
if (args.path.endsWith('.js') && !args.path.endsWith('.json'))
return {
contents: fs.readFileSync(args.path, 'utf8')
+ '\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==',
loader: 'default',
}
else
return
})
},
}
}

(async () => {
for (const entryPoint of entryPoints)
{
const resp = await build({
entryPoints: [entryPoint],
bundle: true,
outdir: path.join(__dirname, outDir),
outbase: functionsDir,
platform: 'node',
sourcemap: 'external',
write: true,
tsconfig: './tsconfig.json',
minify: true,
keepNames: false,
plugins: [
esBuildPluginShrinkSourceMap(),
],
metafile: true,
});

const bundlerDefaults: EsbuildFunctionBundlerOptions = {
outputDir: ".esbuild-visualizer",
template: "treemap"
};

/* Analyze Bundle */
// fs.writeFileSync('meta.json', JSON.stringify(result.metafile));
// let text = await esbuild.analyzeMetafile(result.metafile, {verbose: true, color: true});
// console.log(text);
const htmlContent = await visualizer(resp.metafile, {
title: entryPoint,
template: bundlerDefaults.template!,
});


const outputFile = path.join(__dirname, bundlerDefaults.outputDir!, entryPoint + ".html")
const outputDir = path.dirname(outputFile)
if(!fs.existsSync(outputDir))
fs.mkdirSync(outputDir, {recursive: true});

fs.writeFileSync(outputFile, htmlContent, {});

await open(outputFile);
}
})().catch(e => {
console.error(e);
// eslint-disable-next-line no-process-exit
}).catch(() => process.exit(1));
process.exit(1);
});
118 changes: 113 additions & 5 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"prebuild": "rm -rf dist",
"build": "tsc --outDir dist && rm -rf dist && ts-node esbuild.ts",
"postbuild": "ts-node postbuild.ts",
"cdk-diff": "npm run build && pushd cdk && npm i && npm run diff && popd",
"cdk-deploy": "npm run build && pushd cdk && npm i && npm run deploy && popd",
"serverless-deploy": "npm run build && serverless deploy"
},
Expand All @@ -17,13 +18,15 @@
"@types/aws-lambda": "^8.10.125",
"@types/lambda-log": "^3.0.2",
"esbuild": "^0.19.5",
"esbuild-visualizer": "^0.4.1",
"serverless": "^3.36.0",
"serverless-better-credentials": "^2.0.0",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
"dependencies": {
"aws-lambda": "^1.0.7",
"lambda-log": "^3.1.0"
"lambda-log": "^3.1.0",
"open": "^8.4.2"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should be a dev dependency

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be yes, I mainly opened it to show what is possible. From the blog article, it seemed that you don't want to ship the sourcemaps with? So my idea with this PR wasn't that it will get merged, just to show that it is possible. Else if merged you would have to change parts of the article?

If not and you do want to merge it, by all means :). Please feel free to make the small change above or I can as well.

Nice article btw :). Also FYI, I created two issues that will/might turn into PRs upstream on the CDK:

}
}
2 changes: 1 addition & 1 deletion postbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fs.readdirSync(path.join(__dirname, functionsDir))
'ls -lah',
`pushd dist/${entry}`,
`echo "zipping ${entry} lambda"`,
`zip -R ${entry}.zip *.js`,
`zip -R ${entry}.zip *.js *.map`,
'popd',
];
execSync(commands.join(' && '), {
Expand Down