|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/* eslint-disable no-console */ |
| 4 | + |
| 5 | +const NAMESPACE = 'datadog' |
| 6 | + |
| 7 | +const instrumented = Object.keys(require('../datadog-instrumentations/src/helpers/hooks.js')) |
| 8 | +const rawBuiltins = require('module').builtinModules |
| 9 | + |
| 10 | +warnIfUnsupported() |
| 11 | + |
| 12 | +const builtins = new Set() |
| 13 | + |
| 14 | +for (const builtin of rawBuiltins) { |
| 15 | + builtins.add(builtin) |
| 16 | + builtins.add(`node:${builtin}`) |
| 17 | +} |
| 18 | + |
| 19 | +const packagesOfInterest = new Set() |
| 20 | + |
| 21 | +const DEBUG = !!process.env.DD_TRACE_DEBUG |
| 22 | + |
| 23 | +// We don't want to handle any built-in packages via DCITM |
| 24 | +// Those packages will still be handled via RITM |
| 25 | +// Attempting to instrument them would fail as they have no package.json file |
| 26 | +for (const pkg of instrumented) { |
| 27 | + if (builtins.has(pkg)) continue |
| 28 | + if (pkg.startsWith('node:')) continue |
| 29 | + packagesOfInterest.add(pkg) |
| 30 | +} |
| 31 | + |
| 32 | +const DC_CHANNEL = 'dd-trace:bundledModuleLoadStart' |
| 33 | + |
| 34 | +module.exports.name = 'datadog-esbuild' |
| 35 | + |
| 36 | +module.exports.setup = function (build) { |
| 37 | + build.onResolve({ filter: /.*/ }, args => { |
| 38 | + const packageName = args.path |
| 39 | + |
| 40 | + if (args.namespace === 'file' && packagesOfInterest.has(packageName)) { |
| 41 | + // The file namespace is used when requiring files from disk in userland |
| 42 | + const pathToPackageJson = require.resolve(`${packageName}/package.json`, { paths: [ args.resolveDir ] }) |
| 43 | + const pkg = require(pathToPackageJson) |
| 44 | + |
| 45 | + if (DEBUG) { |
| 46 | + console.log(`resolve ${packageName}@${pkg.version}`) |
| 47 | + } |
| 48 | + |
| 49 | + // https://esbuild.github.io/plugins/#on-resolve-arguments |
| 50 | + return { |
| 51 | + path: packageName, |
| 52 | + namespace: NAMESPACE, |
| 53 | + pluginData: { |
| 54 | + version: pkg.version |
| 55 | + } |
| 56 | + } |
| 57 | + } else if (args.namespace === 'datadog') { |
| 58 | + // The datadog namespace is used when requiring files that are injected during the onLoad stage |
| 59 | + // see note in onLoad |
| 60 | + |
| 61 | + if (builtins.has(packageName)) return |
| 62 | + |
| 63 | + return { |
| 64 | + path: require.resolve(packageName, { paths: [ args.resolveDir ] }), |
| 65 | + namespace: 'file' |
| 66 | + } |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + build.onLoad({ filter: /.*/, namespace: NAMESPACE }, args => { |
| 71 | + if (DEBUG) { |
| 72 | + console.log(`load ${args.path}@${args.pluginData.version}`) |
| 73 | + } |
| 74 | + |
| 75 | + // JSON.stringify adds double quotes. For perf gain could simply add in quotes when we know it's safe. |
| 76 | + const contents = ` |
| 77 | + const dc = require('diagnostics_channel'); |
| 78 | + const ch = dc.channel(${JSON.stringify(DC_CHANNEL + ':' + args.path)}); |
| 79 | + const mod = require(${JSON.stringify(args.path)}); |
| 80 | + const payload = { |
| 81 | + module: mod, |
| 82 | + path: ${JSON.stringify(args.path)}, |
| 83 | + version: ${JSON.stringify(args.pluginData.version)} |
| 84 | + }; |
| 85 | + ch.publish(payload); |
| 86 | + module.exports = payload.module; |
| 87 | + ` |
| 88 | + // https://esbuild.github.io/plugins/#on-load-results |
| 89 | + return { |
| 90 | + contents, |
| 91 | + loader: 'js' |
| 92 | + } |
| 93 | + }) |
| 94 | +} |
| 95 | + |
| 96 | +function warnIfUnsupported () { |
| 97 | + const [major, minor] = process.versions.node.split('.').map(Number) |
| 98 | + if (major < 14 || (major === 14 && minor < 17)) { |
| 99 | + console.error('WARNING: Esbuild support isn\'t available for older versions of Node.js.') |
| 100 | + console.error(`Expected: Node.js >= v14.17. Actual: Node.js = ${process.version}.`) |
| 101 | + console.error('This application may build properly with this version of Node.js, but unless a') |
| 102 | + console.error('more recent version is used at runtime, third party packages won\'t be instrumented.') |
| 103 | + } |
| 104 | +} |
0 commit comments