Repository navigation
Fix dual ESM/CJS output, Azure Monitor disable flag #30
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
Hector Hernandez (hectorhdzg)
merged 3 commits into
microsoft:main
from
hectorhdzg:hectorhdzg/distro-fixes
Apr 22, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
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,59 @@ | ||
| // Post-build fixups for the CommonJS output. | ||
| // | ||
| // The ESM build (dist/esm/) works as-is. The CJS build (dist/commonjs/) | ||
| // needs two fixups: | ||
| // | ||
| // 1. A package.json with {"type":"commonjs"} so Node doesn't treat .js | ||
| // files as ESM (the root package.json has "type":"module"). | ||
| // | ||
| // 2. The module-cjs.cts polyfill must replace module.ts in the CJS output, | ||
| // because module.ts uses import.meta.url which is ESM-only. | ||
| // | ||
| // 3. The Azure Functions instrumentation import must be fixed. The ESM | ||
| // build uses a default import (required because the package is a webpack | ||
| // bundle and Node ESM can't extract named exports). In CJS, the | ||
| // __importDefault helper double-wraps the require result, so we replace | ||
| // it with a direct require + destructure. | ||
|
|
||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
|
|
||
| const cjsDir = path.join(__dirname, "..", "dist", "commonjs"); | ||
|
|
||
| // 1. CJS package.json marker | ||
| fs.writeFileSync(path.join(cjsDir, "package.json"), '{"type":"commonjs"}\n'); | ||
|
|
||
| // 2. module.ts → module-cjs.cts polyfill swap | ||
| const sharedDir = path.join(cjsDir, "shared"); | ||
| fs.copyFileSync( | ||
| path.join(sharedDir, "module-cjs.cjs"), | ||
| path.join(sharedDir, "module.js"), | ||
| ); | ||
| // Keep source maps consistent: copy the CJS map or remove the stale ESM one | ||
| const moduleCjsMapPath = path.join(sharedDir, "module-cjs.cjs.map"); | ||
| const moduleJsMapPath = path.join(sharedDir, "module.js.map"); | ||
| if (fs.existsSync(moduleCjsMapPath)) { | ||
| fs.copyFileSync(moduleCjsMapPath, moduleJsMapPath); | ||
| } else if (fs.existsSync(moduleJsMapPath)) { | ||
| fs.unlinkSync(moduleJsMapPath); | ||
| } | ||
|
|
||
| // 3. Fix Azure Functions default-import in CJS output | ||
| const handlerPath = path.join(cjsDir, "azureMonitor", "traces", "handler.js"); | ||
| const handler = fs.readFileSync(handlerPath, "utf8"); | ||
| const azureFunctionsImportPattern = | ||
| /const functions_opentelemetry_instrumentation_1 = __importDefault\(require\("@azure\/functions-opentelemetry-instrumentation"\)\);\s*const \{ AzureFunctionsInstrumentation \} = functions_opentelemetry_instrumentation_1\.default;/; | ||
| // Replace the __importDefault pattern with a direct require | ||
| const updatedHandler = handler.replace( | ||
| azureFunctionsImportPattern, | ||
| 'const { AzureFunctionsInstrumentation } = require("@azure/functions-opentelemetry-instrumentation");', | ||
| ); | ||
| if (updatedHandler === handler) { | ||
| throw new Error( | ||
| `Azure Functions CJS import fixup did not match expected output in ${handlerPath}. ` + | ||
| `The emitted CommonJS handler format may have changed; update scripts/fixup-cjs.cjs before publishing.`, | ||
| ); | ||
| } | ||
| fs.writeFileSync(handlerPath, updatedHandler); | ||
|
|
||
| console.log("CJS fixups applied."); | ||
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
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
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.