Skip to content

fix(esbuild): avoid using createRequire as import name #799

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 2 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 11 additions & 4 deletions src/bundler/ESBuildBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export default class ESBuildBundler extends BaseBundler {
],
banner: {
js: [
'import { createRequire } from "module";',
'const require = createRequire(import.meta.url);',
'import { createRequire as hedyCreateRequire } from "module";',
'const require = hedyCreateRequire(import.meta.url);',
'',
].join('\n'),
},
Expand All @@ -74,10 +74,17 @@ export default class ESBuildBundler extends BaseBundler {
name: 'alias-main',
setup: (build) => {
build.onResolve({ filter: /^\.\/main\.js$/ }, () => ({ path: cfg.file }));
// use @adobe/helix-universal in the calling service, not ours
build.onResolve(
{ filter: /^@adobe\/helix-universal$/ },
(args) => ({ path: path.resolve(cfg.cwd, 'node_modules', args.path, 'src', 'index.js') }),
(args) => {
const cwd = (process.env.HELIX_FETCH_FORCE_HTTP1 === 'true')
// for testing use "our" @adobe/helix-universal dependency
? path.resolve(__dirname, '..', '..')
// for production use @adobe/helix-universal (and its dependencies)
// in the calling service
: cfg.cwd;
return { path: path.resolve(cwd, 'node_modules', args.path, 'src', 'index.js') };
},
);
cfg.externals.forEach((filter) => {
build.onResolve({ filter }, (args) => ({ path: args.path, external: true }));
Expand Down
2 changes: 1 addition & 1 deletion src/template/node-index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export default {
openwhisk,
lambda,
google,
}
};
8 changes: 8 additions & 0 deletions test/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,12 @@ describe('Build Test', () => {
it('generates the bundle (esm, webpack) fails', async () => {
await assert.rejects(generate(['--esm']), Error('Webpack bundler does not support ESM builds.'));
}).timeout(5000);

it('generates the bundle (esm, esbuild)', async () => {
await generate(['--bundler=esbuild', '--esm'], PROJECT_SIMPLE_ESM);
});

it('generates the bundle (esbuild) fails', async () => {
await assert.rejects(generate(['--bundler=esbuild']), Error('ESBuild bundler only supports ESM builds.'));
});
Comment on lines +171 to +177
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finally added tests for esbuild bundler

});
2 changes: 1 addition & 1 deletion test/setup-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
// eslint-disable-next-line no-console
import path from 'path';
import { fileURLToPath } from 'url';

// eslint-disable-next-line no-console
console.log('Forcing HTTP/1.1 for @adobe/fetch');
process.env.HELIX_FETCH_FORCE_HTTP1 = 'true';

Expand Down