-
Notifications
You must be signed in to change notification settings - Fork 22
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
Comments
It can be used by resolving it to an absolute path using 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],
}); |
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
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:
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?
The text was updated successfully, but these errors were encountered: