-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(profiling): Conditionally shim cjs globals (#13267)
The shims should only be applied if the globals are not present, else it results in double decl and a runtime error. The profiling SDK should gracefully handle env where the shims are already provided. I couldn't find a way to modify the shim as it is hardcoded in the plugin we are using so I went with the replace plugin approach and a placeholder value #poormansmacros.
- Loading branch information
Showing
5 changed files
with
79 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
29 changes: 29 additions & 0 deletions
29
dev-packages/e2e-tests/test-applications/node-profiling/build.shimmed.mjs
This file contains 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,29 @@ | ||
// Because bundlers can now predetermine a static set of binaries we need to ensure those binaries | ||
// actually exists, else we risk a compile time error when bundling the package. This could happen | ||
// if we added a new binary in cpu_profiler.ts, but forgot to prebuild binaries for it. Because CI | ||
// only runs integration and unit tests, this change would be missed and could end up in a release. | ||
// Therefor, once all binaries are precompiled in CI and tests pass, run esbuild with bundle:true | ||
// which will copy all binaries to the outfile folder and throw if any of them are missing. | ||
import esbuild from 'esbuild'; | ||
|
||
console.log('Running build using esbuild version', esbuild.version); | ||
|
||
esbuild.buildSync({ | ||
platform: 'node', | ||
entryPoints: ['./index.ts'], | ||
outfile: './dist/index.shimmed.mjs', | ||
target: 'esnext', | ||
format: 'esm', | ||
bundle: true, | ||
loader: { '.node': 'copy' }, | ||
banner: { | ||
js: ` | ||
import { dirname } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { createRequire } from 'node:module'; | ||
const require = createRequire(import.meta.url); | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
`, | ||
}, | ||
}); |
This file contains 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 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 |
---|---|---|
@@ -1,12 +1,49 @@ | ||
import commonjs from '@rollup/plugin-commonjs'; | ||
import esmshim from '@rollup/plugin-esm-shim'; | ||
import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollup-utils'; | ||
|
||
export default makeNPMConfigVariants( | ||
export const ESMShim = ` | ||
import cjsUrl from 'node:url'; | ||
import cjsPath from 'node:path'; | ||
import cjsModule from 'node:module'; | ||
if(typeof __filename === 'undefined'){ | ||
globalThis.__filename = cjsUrl.fileURLToPath(import.meta.url); | ||
} | ||
if(typeof __dirname === 'undefined'){ | ||
globalThis.__dirname = cjsPath.dirname(__filename); | ||
} | ||
if(typeof require === 'undefined'){ | ||
globalThis.require = cjsModule.createRequire(import.meta.url); | ||
} | ||
`; | ||
|
||
function makeESMShimPlugin(shim) { | ||
return { | ||
transform(code) { | ||
const SHIM_REGEXP = /\/\/ #START_SENTRY_ESM_SHIM[\s\S]*?\/\/ #END_SENTRY_ESM_SHIM/; | ||
return code.replace(SHIM_REGEXP, shim); | ||
}, | ||
}; | ||
} | ||
|
||
const variants = makeNPMConfigVariants( | ||
makeBaseNPMConfig({ | ||
packageSpecificConfig: { | ||
output: { dir: 'lib', preserveModules: false }, | ||
plugins: [commonjs(), esmshim()], | ||
plugins: [commonjs()], | ||
}, | ||
}), | ||
); | ||
|
||
for (const variant of variants) { | ||
if (variant.output.format === 'esm') { | ||
variant.plugins.push(makeESMShimPlugin(ESMShim)); | ||
} else { | ||
// Remove the ESM shim comment | ||
variant.plugins.push(makeESMShimPlugin('')); | ||
} | ||
} | ||
|
||
export default variants; |
This file contains 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