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

Can this tool be used with esbuild? #32

Closed
ezmiller opened this issue Apr 20, 2022 · 2 comments
Closed

Can this tool be used with esbuild? #32

ezmiller opened this issue Apr 20, 2022 · 2 comments

Comments

@ezmiller
Copy link

I tried to use this tool with esbuild to build a Cloudflare worker. I need to use a library that has dependencies on node libs like fs.

I tried:

import buildAlias form 'esbuild-plugin-alias`
import { env, nodeless } from 'unenv'

const { alias } = env(nodeless)

async function main() {
  ...
  const result = await build({
    ...
    plugins[buildAlias(alias)],
    ...
  })
}

main()

I ended up getting a bunch of warnings from esbuild b/c the esbuild requires absolute paths and the paths supplied in the nodeless presets are relative.

Any thoughts about how to get this to work?

@SegaraRai
Copy link

SegaraRai commented May 13, 2022

It can be used by resolving it to an absolute path using require.resolve.
We currently use a plugin like this one. (ESM style. may need some rewrite for CJS)

import { createRequire } from 'module';
import { join } from 'path';
import { build } from 'esbuild';
import { nodeless } from 'unenv';

/** @type {(plugin: import('esbuild').Plugin) => import('esbuild').Plugin} */
const definePlugin = (plugin) => plugin;

const nodelessPlugin = definePlugin({
  name: 'unenv-nodeless',
  setup(build) {
    const alias = nodeless.alias;
    const re = new RegExp(`^(${Object.keys(alias).join('|')})$`); // this should be escaped

    const require = createRequire(import.meta.url);
    const aliasAbsolute = Object.fromEntries(
      Object.entries(alias).map(([key, value]) => [
        key,
        require.resolve(value).replace(/\.cjs$/, '.mjs'),
      ])
    );

    build.onResolve(
      {
        filter: re,
      },
      (args) => {
        const result = aliasAbsolute[args.path];
        return result ? { path: result } : undefined;
      }
    );
  },
});

build({
  entryPoints: ['./src/index.ts'],
  outfile: 'dist/index_dev.js',
  platform: 'browser',
  bundle: true,
  format: 'esm',
  logLevel: 'info',
  plugins: [nodelessPlugin],
});

@pi0
Copy link
Member

pi0 commented Jun 18, 2024

We plan to add an unplugin to unenv for easier integration. Lets track via #263

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants