Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: esbuild plugin when requiring esm files #4774

Merged
merged 6 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] })
tlhunter marked this conversation as resolved.
Show resolved Hide resolved
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'
}
}
Loading