1
1
const NAMESPACE = 'datadog'
2
2
3
3
const instrumented = Object . keys ( require ( '../datadog-instrumentations/src/helpers/hooks.js' ) )
4
- const builtins = new Set ( require ( 'module' ) . builtinModules )
5
- const packages = new Set ( )
4
+ const rawBuiltins = require ( 'module' ) . builtinModules
5
+
6
+ const builtins = new Set ( )
7
+
8
+ for ( const builtin of rawBuiltins ) {
9
+ builtins . add ( builtin )
10
+ builtins . add ( `node:${ builtin } ` )
11
+ }
12
+
13
+ const packagesOfInterest = new Set ( )
6
14
7
15
const DEBUG = ! ! process . env . DD_TRACE_DEBUG
8
16
@@ -12,7 +20,7 @@ const DEBUG = !!process.env.DD_TRACE_DEBUG
12
20
for ( const pkg of instrumented ) {
13
21
if ( builtins . has ( pkg ) ) continue
14
22
if ( pkg . startsWith ( 'node:' ) ) continue
15
- packages . add ( pkg )
23
+ packagesOfInterest . add ( pkg )
16
24
}
17
25
18
26
const DC_CHANNEL = 'dd-trace:bundledModuleLoadStart'
@@ -23,7 +31,7 @@ module.exports.setup = function (build) {
23
31
build . onResolve ( { filter : / .* / } , args => {
24
32
const packageName = args . path
25
33
26
- if ( args . namespace === 'file' && packages . has ( packageName ) ) {
34
+ if ( args . namespace === 'file' && packagesOfInterest . has ( packageName ) ) {
27
35
// The file namespace is used when requiring files from disk in userland
28
36
const pathToPackageJson = require . resolve ( `${ packageName } /package.json` , { paths : [ args . resolveDir ] } )
29
37
const pkg = require ( pathToPackageJson )
@@ -45,7 +53,7 @@ module.exports.setup = function (build) {
45
53
// The datadog namespace is used when requiring files that are injected during the onLoad stage
46
54
// see note in onLoad
47
55
48
- if ( packageName . startsWith ( 'node:' ) ) return
56
+ if ( builtins . has ( packageName ) ) return
49
57
50
58
return {
51
59
path : require . resolve ( packageName , { paths : [ args . resolveDir ] } ) ,
@@ -59,8 +67,7 @@ module.exports.setup = function (build) {
59
67
// eslint-disable-next-line no-console
60
68
console . log ( `load ${ args . path } @${ args . pluginData . version } ` )
61
69
}
62
- // TODO: relying on prefixing internal packages with `node:` in this intermediary module for now.
63
- // If this causes an issue we'll need to update the logic for determining if a module is internal.
70
+
64
71
// JSON.stringify adds double quotes. For perf gain could simply add in quotes when we know it's safe.
65
72
const contents = `
66
73
const dc = require('node:diagnostics_channel');
@@ -71,29 +78,13 @@ module.exports.setup = function (build) {
71
78
path: ${ JSON . stringify ( args . path ) } ,
72
79
version: ${ JSON . stringify ( args . pluginData . version ) }
73
80
};
74
- // if (!ch.hasSubscribers) console.error('missing subscriber! ${ JSON . stringify ( DC_CHANNEL + ':' + args . path ) } ');
75
- ch.publish(payload); // allow subscriber to mutate payload
81
+ ch.publish(payload);
76
82
module.exports = payload.module;
77
- // module.exports.__DATADOG_VERSION = ${ JSON . stringify ( args . pluginData . version ) } ;
78
83
`
79
84
// https://esbuild.github.io/plugins/#on-load-results
80
85
return {
81
86
contents,
82
87
loader : 'js'
83
88
}
84
89
} )
85
- }
86
-
87
- /**
88
- * This could be a convenience to expose a list of known externals to the application.
89
- * Devs are free to use this list, ignore it, or merge it with their application-specific list.
90
- * TODO: Sadly, esbuild does not allow unanticipated keys on the exported object.
91
- * Could do `{ plugin: { name, setup }, knownExternals }`
92
- * and have app call `plugins: [require('dd-trace/esbuild').plugin]` instead...
93
- */
94
- // module.exports.knownExternals = [
95
- // 'pg-native', // peer dep
96
- // 'graphql/language/visitor',
97
- // 'graphql/language/printer',
98
- // 'graphql/utilities',
99
- // ];
90
+ }
0 commit comments