Skip to content

Commit

Permalink
fix esbuild issue when requiring esm files
Browse files Browse the repository at this point in the history
  • Loading branch information
crysmags committed Oct 11, 2024
1 parent bd4aff5 commit 734655d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/datadog-esbuild/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ module.exports.setup = function (build) {

let pathToPackageJson
try {
pathToPackageJson = require.resolve(`${extracted.pkg}/package.json`, { paths: [args.resolveDir] })
pathToPackageJson = require.resolve(extracted.pkg, { paths: [args.resolveDir] })
pathToPackageJson = extractPackageAndModulePath(pathToPackageJson).pkgJson
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
if (!internal) {
Expand All @@ -111,7 +112,7 @@ module.exports.setup = function (build) {
}
}

const packageJson = require(pathToPackageJson)
const packageJson = JSON.parse(fs.readFileSync(pathToPackageJson).toString())

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const NM = 'node_modules/'
* For a given full path to a module,
* return the package name it belongs to and the local path to the module
* input: '/foo/node_modules/@co/stuff/foo/bar/baz.js'
* output: { pkg: '@co/stuff', path: 'foo/bar/baz.js' }
* output: { pkg: '@co/stuff', path: 'foo/bar/baz.js', pkgJson: '/foo/node_modules/@co/stuff/package.json' }
*/
module.exports = function extractPackageAndModulePath (fullPath) {
const nm = fullPath.lastIndexOf(NM)
Expand All @@ -17,17 +17,20 @@ module.exports = function extractPackageAndModulePath (fullPath) {
const subPath = fullPath.substring(nm + NM.length)
const firstSlash = subPath.indexOf('/')

const firstPath = fullPath.substring(fullPath[0], nm + NM.length)

if (subPath[0] === '@') {
const secondSlash = subPath.substring(firstSlash + 1).indexOf('/')

return {
pkg: subPath.substring(0, firstSlash + 1 + secondSlash),
path: subPath.substring(firstSlash + 1 + secondSlash + 1)
path: subPath.substring(firstSlash + 1 + secondSlash + 1),
pkgJson: firstPath + subPath.substring(0, firstSlash + 1 + secondSlash) + '/package.json'
}
}

return {
pkg: subPath.substring(0, firstSlash),
path: subPath.substring(firstSlash + 1)
path: subPath.substring(firstSlash + 1),
pkgJson: firstPath + subPath.substring(0, firstSlash) + '/package.json'
}
}

0 comments on commit 734655d

Please sign in to comment.