Skip to content

Commit 734655d

Browse files
committed
fix esbuild issue when requiring esm files
1 parent bd4aff5 commit 734655d

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

packages/datadog-esbuild/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ module.exports.setup = function (build) {
9696

9797
let pathToPackageJson
9898
try {
99-
pathToPackageJson = require.resolve(`${extracted.pkg}/package.json`, { paths: [args.resolveDir] })
99+
pathToPackageJson = require.resolve(extracted.pkg, { paths: [args.resolveDir] })
100+
pathToPackageJson = extractPackageAndModulePath(pathToPackageJson).pkgJson
100101
} catch (err) {
101102
if (err.code === 'MODULE_NOT_FOUND') {
102103
if (!internal) {
@@ -111,7 +112,7 @@ module.exports.setup = function (build) {
111112
}
112113
}
113114

114-
const packageJson = require(pathToPackageJson)
115+
const packageJson = JSON.parse(fs.readFileSync(pathToPackageJson).toString())
115116

116117
if (DEBUG) console.log(`RESOLVE: ${args.path}@${packageJson.version}`)
117118

packages/datadog-instrumentations/src/utils/src/extract-package-and-module-path.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const NM = 'node_modules/'
66
* For a given full path to a module,
77
* return the package name it belongs to and the local path to the module
88
* input: '/foo/node_modules/@co/stuff/foo/bar/baz.js'
9-
* output: { pkg: '@co/stuff', path: 'foo/bar/baz.js' }
9+
* output: { pkg: '@co/stuff', path: 'foo/bar/baz.js', pkgJson: '/foo/node_modules/@co/stuff/package.json' }
1010
*/
1111
module.exports = function extractPackageAndModulePath (fullPath) {
1212
const nm = fullPath.lastIndexOf(NM)
@@ -17,17 +17,20 @@ module.exports = function extractPackageAndModulePath (fullPath) {
1717
const subPath = fullPath.substring(nm + NM.length)
1818
const firstSlash = subPath.indexOf('/')
1919

20+
const firstPath = fullPath.substring(fullPath[0], nm + NM.length)
21+
2022
if (subPath[0] === '@') {
2123
const secondSlash = subPath.substring(firstSlash + 1).indexOf('/')
22-
2324
return {
2425
pkg: subPath.substring(0, firstSlash + 1 + secondSlash),
25-
path: subPath.substring(firstSlash + 1 + secondSlash + 1)
26+
path: subPath.substring(firstSlash + 1 + secondSlash + 1),
27+
pkgJson: firstPath + subPath.substring(0, firstSlash + 1 + secondSlash) + '/package.json'
2628
}
2729
}
2830

2931
return {
3032
pkg: subPath.substring(0, firstSlash),
31-
path: subPath.substring(firstSlash + 1)
33+
path: subPath.substring(firstSlash + 1),
34+
pkgJson: firstPath + subPath.substring(0, firstSlash) + '/package.json'
3235
}
3336
}

0 commit comments

Comments
 (0)