chialab / rna

🚀 Build tools for modern web modules and applications.

Home Page:https://chialab.github.io/rna/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

dhdaines opened this issue · comments

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.

Hello @dhdaines! Thanks for reporting!
Could you check if the latest version of the plugin correctly saves the file in the output directory?

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Hi! Sorry for the very late reply! The latest version seemed to have the same behaviour when I tried it ... two months ago (sorry again) ... not sure if there has been another one since then? I will have some time today or tomorrow to recheck this.