From be71be5fa25e5fe9108cbbebe8a933a5d26a0d8d Mon Sep 17 00:00:00 2001 From: Dominique Pfister Date: Thu, 10 Apr 2025 13:21:33 +0200 Subject: [PATCH 1/2] fix(esbuild): avoid usingcreateRequire as import name --- src/bundler/ESBuildBundler.js | 15 +++++++++++---- src/template/node-index.mjs | 2 +- test/build.test.js | 8 ++++++++ test/setup-env.js | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/bundler/ESBuildBundler.js b/src/bundler/ESBuildBundler.js index e91aee91..8d1175ee 100644 --- a/src/bundler/ESBuildBundler.js +++ b/src/bundler/ESBuildBundler.js @@ -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'), }, @@ -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 })); diff --git a/src/template/node-index.mjs b/src/template/node-index.mjs index a52e0f65..d51a126d 100644 --- a/src/template/node-index.mjs +++ b/src/template/node-index.mjs @@ -22,4 +22,4 @@ export default { openwhisk, lambda, google, -} +}; diff --git a/test/build.test.js b/test/build.test.js index c5e05294..d3e51ca5 100644 --- a/test/build.test.js +++ b/test/build.test.js @@ -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.')); + }); }); diff --git a/test/setup-env.js b/test/setup-env.js index 9f025492..201654d6 100644 --- a/test/setup-env.js +++ b/test/setup-env.js @@ -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'; From f81eb1fac3fc0a374967b74fd3dc16ab0f2244b4 Mon Sep 17 00:00:00 2001 From: Dominique Pfister Date: Thu, 10 Apr 2025 13:39:03 +0200 Subject: [PATCH 2/2] fix: don't use HELIX_FETCH_FORCE_HTTP1 (other projects might use this) --- src/bundler/ESBuildBundler.js | 2 +- test/setup-env.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bundler/ESBuildBundler.js b/src/bundler/ESBuildBundler.js index 8d1175ee..c8c5a3c7 100644 --- a/src/bundler/ESBuildBundler.js +++ b/src/bundler/ESBuildBundler.js @@ -77,7 +77,7 @@ export default class ESBuildBundler extends BaseBundler { build.onResolve( { filter: /^@adobe\/helix-universal$/ }, (args) => { - const cwd = (process.env.HELIX_FETCH_FORCE_HTTP1 === 'true') + const cwd = (process.env.HELIX_DEPLOY_USE_LOCAL === 'true') // for testing use "our" @adobe/helix-universal dependency ? path.resolve(__dirname, '..', '..') // for production use @adobe/helix-universal (and its dependencies) diff --git a/test/setup-env.js b/test/setup-env.js index 201654d6..c769e835 100644 --- a/test/setup-env.js +++ b/test/setup-env.js @@ -15,6 +15,7 @@ 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'; +process.env.HELIX_DEPLOY_USE_LOCAL = 'true'; // eslint-disable-next-line no-underscore-dangle global.__rootdir = path.resolve(fileURLToPath(import.meta.url), '..', '..');