evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
37.92k stars 1.13k forks source link

Bundling with HTML #3618

Open NotArpit opened 7 months ago

NotArpit commented 7 months ago

I am currently trying to bundle and minify my service - however a package listed as a transitive dependency (i.e. @mapbox/node-pre-gyp) contains a *.html file. Resulting in the following error during bundling/minification.

[ERROR] No loader is configured for ".html" files: ../../node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html

    ../../node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js:86:21:
      86 │       return require('./' + command)(self, argvx, callback);
         ╵                      ~~~~~~~~~~~~~~

/Users/user/project/node_modules/esbuild/lib/main.js:1651
  let error = new Error(text);

Is there any existing loader/plugin in esbuild for *.html files? Couldn't find any helpful documentation. Namely, I'm looking for an esbuild equivalent to webpacks html-loader

Any help is appreciated !

koshic commented 7 months ago

https://esbuild.github.io/content-types/#external-file - simplest solution. Other ways are depend on possible .html processing and so on.

NotArpit commented 7 months ago

Thanks - unfortunately that's not what I'm looking for. As I understand it using the external-file approach only copies the file directly to the output bundle. Rather I was wondering if there is a way to export the HTML as a string so it's all contained inside the bundle and the HTML is injected via JavaScript.

koshic commented 7 months ago

if there is a way to export the HTML as a string so it's all contained inside the bundle and the HTML

https://esbuild.github.io/content-types/#text

...injected via JavaScript.

It doesn't work like that, sorry ) You have to provide exact expectations - what should happen with .html file content. As an example, for one of my projects I use React SSR (and simple esbuild plugin) to create .html files as part of build process.