diff --git a/src/bundler/ESBuildBundler.js b/src/bundler/ESBuildBundler.js index e91aee91..c8c5a3c7 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_DEPLOY_USE_LOCAL === '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..c769e835 100644 --- a/test/setup-env.js +++ b/test/setup-env.js @@ -9,12 +9,13 @@ * 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'; +process.env.HELIX_DEPLOY_USE_LOCAL = 'true'; // eslint-disable-next-line no-underscore-dangle global.__rootdir = path.resolve(fileURLToPath(import.meta.url), '..', '..');