Skip to content

Unable to specify an output directory with esbuild-plugin-meta-urlΒ #135

@dhdaines

Description

@dhdaines

Hi! The esbuild-plugin-meta-url is quite useful when moving from webpack to esbuild. Unfortunately it seems to default to a behaviour of reproducing the relative path to the referenced files, and trying to recreate that path, where webpack would have renamed them (in some configurable fashion) and placed them in the output directory or a configurable assets directory.

The reason this is desireable is that, well, first, it avoids having weirdly long paths embedded in your bundle in the case where the files came from somewhere deep in node_modules, but second, if the files happen to come from somewhere outside the working directory (as will be the case when using npm link), the plugin will build a directory somewhere else, possibly a place you don't want it to, or where you don't even have write permission. For example, if you have index.js:

const img = new URL('../foo/logo.png', import.meta.url).href;
console.log(`URL is ${img}`);

and build.mjs:

import esbuild from 'esbuild';
import metaUrlPlugin from '@chialab/esbuild-plugin-meta-url';

await esbuild.build({
  entryPoints: ["index.js"],
  bundle: true,
  outdir: "dist",
  format: "esm",
  plugins: [
    metaUrlPlugin(),
  ],
});

Then, on running build.js, you will create these files:

$ node build.mjs 
$ find . -mmin -1
./dist/index.js
./foo/logo.png

That's probably not what you want. This may seem kind of contrived, but as mentioned earlier, it will happen in the case where you use npm link, and in any case it would be nice to be able to collect all your assets in a folder called, perhaps, dist/assets.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions