-
Notifications
You must be signed in to change notification settings - Fork 339
Improve support for esbuild + esm with DD_BUILD_ESM env var #5888
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d5c2f4b
Add failing test for esbuild esm
uurien bf4d4f5
Possible approach to fix __dirname and __filename in esbuild + esm
uurien 06baf01
Fix lint
uurien 021718c
Merge branch 'master' into ugaitz/fix-esbuild-esm
uurien 819001e
Use safer var names
uurien 224ff11
Correct support to process.env.DD_BUILD_ESM
uurien 5d8de00
Restore accidentally changed dependency
uurien 62d1d8c
Use globals instead of const and stop using env var
uurien 1fae9cc
Update packages/datadog-esbuild/index.js
uurien cd57f71
Add more tests
uurien cc50961
More tests
uurien 2d61d2e
improve test
uurien 185e83f
Update integration-tests/esbuild/index.spec.js
tlhunter 5b172f0
Fixing tests
uurien f6fdae9
fix test
uurien 13e26af
fix lint
uurien b38d6e1
Use ESM to use top level await and remove catches with exit
uurien 7fd2099
Fix
uurien File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
integration-tests/esbuild/build-and-run.esm-patched-const-banner.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env node | ||
/* eslint-disable no-console */ | ||
|
||
import esbuild from 'esbuild' | ||
import { spawnSync } from 'child_process' | ||
import commonConfig from './build.esm.common-config.js' | ||
|
||
await esbuild.build({ | ||
...commonConfig, | ||
banner: { | ||
js: `import { createRequire } from 'module'; | ||
import { fileURLToPath } from 'url'; | ||
import { dirname } from 'path'; | ||
const require = createRequire(import.meta.url); | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename);` | ||
} | ||
}) | ||
|
||
const { status, stdout, stderr } = spawnSync('node', ['out.mjs']) | ||
if (stdout.length) { | ||
console.log(stdout.toString()) | ||
} | ||
if (stderr.length) { | ||
console.error(stderr.toString()) | ||
} | ||
if (status) { | ||
throw new Error('generated script failed to run') | ||
} | ||
console.log('ok') |
30 changes: 30 additions & 0 deletions
30
integration-tests/esbuild/build-and-run.esm-patched-global-banner.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env node | ||
/* eslint-disable no-console */ | ||
|
||
import esbuild from 'esbuild' | ||
import { spawnSync } from 'child_process' | ||
import commonConfig from './build.esm.common-config.js' | ||
|
||
await esbuild.build({ | ||
...commonConfig, | ||
banner: { | ||
js: `import { createRequire } from 'module'; | ||
import { fileURLToPath } from 'url'; | ||
import { dirname } from 'path'; | ||
globalThis.require ??= createRequire(import.meta.url); | ||
globalThis.__filename ??= fileURLToPath(import.meta.url); | ||
globalThis.__dirname ??= dirname(globalThis.__filename);` | ||
} | ||
}) | ||
|
||
const { status, stdout, stderr } = spawnSync('node', ['out.mjs']) | ||
if (stdout.length) { | ||
console.log(stdout.toString()) | ||
} | ||
if (stderr.length) { | ||
console.error(stderr.toString()) | ||
} | ||
if (status) { | ||
throw new Error('generated script failed to run') | ||
} | ||
console.log('ok') |
23 changes: 23 additions & 0 deletions
23
integration-tests/esbuild/build-and-run.esm-relying-in-extension.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env node | ||
/* eslint-disable no-console */ | ||
|
||
import esbuild from 'esbuild' | ||
import { spawnSync } from 'child_process' | ||
import commonConfig from './build.esm.common-config.js' | ||
|
||
await esbuild.build({ | ||
...commonConfig, | ||
format: undefined | ||
}) | ||
|
||
const { status, stdout, stderr } = spawnSync('node', ['out.mjs']) | ||
if (stdout.length) { | ||
console.log(stdout.toString()) | ||
} | ||
if (stderr.length) { | ||
console.error(stderr.toString()) | ||
} | ||
if (status) { | ||
throw new Error('generated script failed to run') | ||
} | ||
console.log('ok') |
27 changes: 27 additions & 0 deletions
27
integration-tests/esbuild/build-and-run.esm-relying-in-format.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env node | ||
/* eslint-disable no-console */ | ||
|
||
import esbuild from 'esbuild' | ||
import { spawnSync } from 'child_process' | ||
import { renameSync } from 'fs' | ||
import commonConfig from './build.esm.common-config.js' | ||
|
||
await esbuild.build({ | ||
...commonConfig, | ||
outfile: 'out.js' | ||
}) | ||
|
||
// to force being executed as module | ||
renameSync('./out.js', './out.mjs') | ||
|
||
const { status, stdout, stderr } = spawnSync('node', ['out.mjs']) | ||
if (stdout.length) { | ||
console.log(stdout.toString()) | ||
} | ||
if (stderr.length) { | ||
console.error(stderr.toString()) | ||
} | ||
if (status) { | ||
throw new Error('generated script failed to run') | ||
} | ||
console.log('ok') |
27 changes: 27 additions & 0 deletions
27
integration-tests/esbuild/build-and-run.esm-relying-in-out-extension.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env node | ||
/* eslint-disable no-console */ | ||
|
||
import esbuild from 'esbuild' | ||
import { spawnSync } from 'child_process' | ||
import commonConfig from './build.esm.common-config.js' | ||
|
||
// output => basic-test.mjs | ||
await esbuild.build({ | ||
...commonConfig, | ||
outfile: undefined, | ||
format: undefined, | ||
outdir: './', | ||
outExtension: { '.js': '.mjs' } | ||
}) | ||
|
||
const { status, stdout, stderr } = spawnSync('node', ['basic-test.mjs']) | ||
if (stdout.length) { | ||
console.log(stdout.toString()) | ||
} | ||
if (stderr.length) { | ||
console.error(stderr.toString()) | ||
} | ||
if (status) { | ||
throw new Error('generated script failed to run') | ||
} | ||
console.log('ok') |
25 changes: 25 additions & 0 deletions
25
integration-tests/esbuild/build-and-run.esm-unrelated-js-banner.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env node | ||
/* eslint-disable no-console */ | ||
|
||
import esbuild from 'esbuild' | ||
import { spawnSync } from 'child_process' | ||
import commonConfig from './build.esm.common-config.js' | ||
|
||
await esbuild.build({ | ||
...commonConfig, | ||
banner: { | ||
js: '/* js test */' | ||
} | ||
}) | ||
|
||
const { status, stdout, stderr } = spawnSync('node', ['out.mjs']) | ||
if (stdout.length) { | ||
console.log(stdout.toString()) | ||
} | ||
if (stderr.length) { | ||
console.error(stderr.toString()) | ||
} | ||
if (status) { | ||
throw new Error('generated script failed to run') | ||
} | ||
console.log('ok') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const ddPlugin = require('../../esbuild') | ||
module.exports = { | ||
format: 'esm', | ||
entryPoints: ['basic-test.js'], | ||
bundle: true, | ||
outfile: 'out.mjs', | ||
plugins: [ddPlugin], | ||
platform: 'node', | ||
target: ['node18'], | ||
external: [ | ||
// dead code paths introduced by knex | ||
'pg', | ||
'mysql2', | ||
'better-sqlite3', | ||
'sqlite3', | ||
'mysql', | ||
'oracledb', | ||
'pg-query-stream', | ||
'tedious' | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env node | ||
|
||
import esbuild from 'esbuild' | ||
|
||
import commonConfig from './build.esm.common-config.js' | ||
|
||
await esbuild.build(commonConfig) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we move the cleanup to the file that runs the test by using a finally block, we'd not have to list them here.
That feels a tad cleaner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some tests that are checking the content of the files, like this one: https://github.com/DataDog/dd-trace-js/pull/5888/files/13e26af2feb6a6d3a14d1677cdbebe29bfd08a07
I don't want to move these checks to the file running the tests, imo the test logic is better here. imho build & run are more than enough to have in separate files.