generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 15
Refactor Lambda Layer bundling #173
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
Merged
ezhang6811
merged 10 commits into
aws-observability:main
from
ezhang6811:zhaez/refactor-webpack
Apr 4, 2025
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9a801d0
use wrapper as webpack entry point
ezhang6811 d1969a2
clean up ts-loader configs
ezhang6811 6e0dfea
clean up old webpack code
ezhang6811 bfdc09a
minor changes
ezhang6811 023e55a
remove unused package
ezhang6811 46977dd
remove loader scripts
ezhang6811 a0b9ef0
minor fix
ezhang6811 dcf88bf
refactor npm scripts
ezhang6811 6d428c7
Merge remote-tracking branch 'upstream/main' into zhaez/refactor-webpack
ezhang6811 abcf76c
add comment for import-in-the-middle dependency
ezhang6811 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
aws-distro-opentelemetry-node-autoinstrumentation/tsconfig.webpack.json
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -euf -o pipefail | ||
|
|
||
| # Space separated list of external NPM packages | ||
| EXTERNAL_PACKAGES=( "import-in-the-middle" ) | ||
|
|
||
| for EXTERNAL_PACKAGE in "${EXTERNAL_PACKAGES[@]}" | ||
| do | ||
| echo "Installing external package $EXTERNAL_PACKAGE ..." | ||
|
|
||
| PACKAGE_VERSION=$(npm query "#$EXTERNAL_PACKAGE" \ | ||
| | grep version \ | ||
| | head -1 \ | ||
| | awk -F: '{ print $2 }' \ | ||
| | sed 's/[",]//g') | ||
|
|
||
| echo "Resolved version of the external package $EXTERNAL_PACKAGE: $PACKAGE_VERSION" | ||
|
|
||
| npm install "$EXTERNAL_PACKAGE@$PACKAGE_VERSION" --prefix ./build/workspace/nodejs --production --ignore-scripts | ||
|
|
||
| echo "Installed external package $EXTERNAL_PACKAGE" | ||
| done | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| const WRAPPER_INIT_START_TIME = Date.now(); | ||
| await import('./wrapper.js'); | ||
| console.log('OpenTelemetry wrapper init completed in', Date.now() - WRAPPER_INIT_START_TIME, 'ms'); | ||
|
|
||
| const LOADER_INIT_START_TIME = Date.now(); | ||
| await import('./loader.mjs'); | ||
| console.log('OpenTelemetry loader init completed in', Date.now() - LOADER_INIT_START_TIME, 'ms'); |
jj22ee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { register } from 'module'; | ||
| import * as path from 'path'; | ||
| import * as fs from 'fs'; | ||
|
|
||
| function _hasFolderPackageJsonTypeModule(folder) { | ||
| if (folder.endsWith('/node_modules')) { | ||
| return false; | ||
| } | ||
| let pj = path.join(folder, '/package.json'); | ||
| if (fs.existsSync(pj)) { | ||
| try { | ||
| let pkg = JSON.parse(fs.readFileSync(pj).toString()); | ||
| if (pkg) { | ||
| if (pkg.type === 'module') { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| } catch (e) { | ||
| console.warn(`${pj} cannot be read, it will be ignored for ES module detection purposes.`, e); | ||
| return false; | ||
| } | ||
| } | ||
| if (folder === '/') { | ||
| return false; | ||
| } | ||
| return _hasFolderPackageJsonTypeModule(path.resolve(folder, '..')); | ||
| } | ||
|
|
||
| function _hasPackageJsonTypeModule(file) { | ||
| let jsPath = file + '.js'; | ||
| if (fs.existsSync(jsPath)) { | ||
| return _hasFolderPackageJsonTypeModule(path.resolve(path.dirname(jsPath))); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| function _resolveHandlerFileName() { | ||
| const taskRoot = process.env.LAMBDA_TASK_ROOT; | ||
| const handlerDef = process.env._HANDLER; | ||
| if (!taskRoot || !handlerDef) { | ||
| return null; | ||
| } | ||
| const handler = path.basename(handlerDef); | ||
| const moduleRoot = handlerDef.substr(0, handlerDef.length - handler.length); | ||
| const [module, _] = handler.split('.', 2); | ||
| return path.resolve(taskRoot, moduleRoot, module); | ||
| } | ||
|
|
||
| function _isHandlerAnESModule() { | ||
| try { | ||
| const handlerFileName = _resolveHandlerFileName(); | ||
| if (!handlerFileName) { | ||
| return false; | ||
| } | ||
| if (fs.existsSync(handlerFileName + '.mjs')) { | ||
| return true; | ||
| } else if (fs.existsSync(handlerFileName + '.cjs')) { | ||
| return false; | ||
| } else { | ||
| return _hasPackageJsonTypeModule(handlerFileName); | ||
| } | ||
| } catch (e) { | ||
| console.error('Unknown error occurred while checking whether handler is an ES module', e); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| let registered = false; | ||
|
|
||
| export function registerLoader() { | ||
| if (!registered) { | ||
| register('import-in-the-middle/hook.mjs', import.meta.url); | ||
| registered = true; | ||
| } | ||
| } | ||
|
|
||
| if (_isHandlerAnESModule()) { | ||
| /* | ||
| We could activate ESM loader hook of the "import-in-the-middle" library, | ||
| - by "--loader=import-in-the-middle/hook.mjs" Node CLI option, but "--loader" option has been deprecated | ||
| - or by "--import=import-in-the-middle/hook.mjs" Node CLI option, but in this case, | ||
| there will always be "import-in-the-middle" hook initialization overhead even for non-ESM (CommonJS) modules | ||
|
|
||
| Hence, instead, we initialize "import-in-the-middle" hook only for ES (EcmaScript) based user handlers | ||
| to prevent redundant "import-in-the-middle" hook initialization overhead during coldstart | ||
| of the CommonJS based user handlers. | ||
| */ | ||
| registerLoader(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "extends": "../../tsconfig.esm", | ||
| "compilerOptions": { | ||
| "rootDir": ".", | ||
| "outDir": "build", | ||
| }, | ||
| "include": [ | ||
| "src/**/*.ts", | ||
| "test/**/*.ts" | ||
jj22ee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ] | ||
| } | ||
10 changes: 7 additions & 3 deletions
10
...ode-autoinstrumentation/webpack.config.js → ...da-layer/packages/layer/webpack.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "moduleResolution": "node", | ||
| "module": "es2020", | ||
| "target": "es2020", | ||
| "allowUnreachableCode": false, | ||
| "allowUnusedLabels": false, | ||
| "declaration": true, | ||
| "declarationMap": true, | ||
| "esModuleInterop": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "noEmitOnError": true, | ||
| "noFallthroughCasesInSwitch": true, | ||
| "noImplicitReturns": true, | ||
| "noUnusedLocals": true, | ||
| "pretty": true, | ||
| "sourceMap": true, | ||
| "strict": true, | ||
| "strictNullChecks": true, | ||
| "incremental": true, | ||
| "newLine": "LF" | ||
| }, | ||
| "exclude": [ | ||
| "node_modules" | ||
| ] | ||
| } | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.