richardtallent / vite-plugin-singlefile

Vite plugin for inlining JavaScript and CSS resources
MIT License
808 stars 53 forks source link

Introduce a config option for collapsing newlines #85

Closed mataha closed 7 months ago

mataha commented 9 months ago

This commit introduces a config option that, when enabled, collapses leading and trailing newlines inserted while combining assets within a single file.

This is necessary because, by the time this plugin runs (i.e. during the generateBundle stage), it is too late to perform any code minification.

richardtallent commented 8 months ago

Sorry, I'm not following why this is needed?

richardtallent commented 7 months ago

No information provided, closing.

mataha commented 7 months ago

Apologies - I didn't see your previous message.

Consider the following example:

package.json ```json { "name": "example", "type": "module", "version": "1.0.0", "scripts": { "build": "vite build", "serve": "vite serve" }, "devDependencies": { "vite": "^5.0.12", "vite-plugin-singlefile": "^1.0.0" } } ```
vite.config.mjs ```js import { defineConfig } from "vite"; import { viteSingleFile } from "vite-plugin-singlefile"; export default defineConfig({ plugins: [ viteSingleFile({ useRecommendedBuildConfig: true, removeViteModuleLoader: true, }), ], build: { minify: true, } }); ```
index.html ```html Hello World! ```
index.css ```css html { box-sizing: border-box; text-size-adjust: none; } ```

The result:

<!doctype html>
<html>
  <head>
    <style>
html{box-sizing:border-box;text-size-adjust:none}

</style>
  </head>
  <body>
    Hello World!
  </body>
</html>

Notice the newlines. I would expect the following instead:

<!doctype html>
<html>
  <head>
    <style>html{box-sizing:border-box;text-size-adjust:none}</style>
  </head>
  <body>
    Hello World!
  </body>
</html>

If you're wondering why is this important for me - when on 512 Kbps, every shaven byte matters.

richardtallent commented 7 months ago

This has been resolved in v2.0

mataha commented 7 months ago

That almost did it - see #91.