Skip to content

Commit

Permalink
0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed May 10, 2022
1 parent 269829f commit 9598cb7
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 108 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.0.3

- Updates README.md
- Refactor

## 0.0.2

- Refactor
Expand Down
39 changes: 34 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# astro-compress 🗜️
# [astro-compress] 🗜️

This **[Astro integration][astro-integration]** brings compression utilities to
your Astro project.

[csso](https://npmjs.org/csso)
[html-minifier-terser](https://npmjs.org/html-minifier-terser)
[terser](https://npmjs.org/html-minifier-terser)

## Installation

Expand Down Expand Up @@ -44,18 +45,46 @@ Then, apply this integration to your `astro.config.*` file using the

**astro.config.mjs**

```js
```ts
import { defineConfig } from "astro/config";
import compress from "astro-compress";

export default {
export default defineConfig({
// ...
integrations: [compress()],
};
});
```

## Getting started

The utility should now automatically compress all your css and html files in the
The utility should now automatically compress all your CSS and HTML files in the
dist folder.

You can override any of the default options from the configurations of:

- [csso](src/options/csso.ts)
- [html-minifier-terser](src/options/html-minifier-terser.ts)
- [terser](src/options/terser.ts)

or disable them entirely:

```ts
import { defineConfig } from "astro/config";
import compress from "astro-compress";

export default defineConfig({
integrations: [
compress({
css: false,
html: false,
js: false,
}),
],
});
```

[astro-compress]: https://npmjs.org/astro-compress
[csso]: https://npmjs.org/csso
[html-minifier-terser]: https://npmjs.org/html-minifier-terser
[terser]: https://npmjs.org/html-minifier-terser
[astro-integration]: https://docs.astro.build/en/guides/integrations-guide/
3 changes: 0 additions & 3 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import type { AstroIntegration } from "astro";
import Options from "./options";
export interface Files {
[type: string]: string[];
}
export default function createPlugin(integrationOptions?: Options): AstroIntegration;
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions dist/options.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CSS from "./options/csso";
import HTML from "./options/html-minifier";
import HTML from "./options/html-minifier-terser";
import JS from "./options/terser";
export default interface Options {
/**
* Astro build path.
Expand All @@ -9,9 +10,13 @@ export default interface Options {
/**
* [csso] options.
*/
css?: CSS | false;
css?: CSS;
/**
* [html-minifier-terser] options.
*/
html?: HTML | false;
html?: HTML;
/**
* [terser] options.
*/
js?: JS;
}
File renamed without changes.
18 changes: 0 additions & 18 deletions dist/options/index.d.ts

This file was deleted.

3 changes: 3 additions & 0 deletions dist/options/terser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { MinifyOptions } from "terser";
export default interface JS extends MinifyOptions {
}
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "astro-compress",
"version": "0.0.2",
"version": "0.0.3",
"type": "module",
"description": "AstroJS compression utilities. Compress HTML, CSS, JavaScript and more.",
"repository": {
Expand Down Expand Up @@ -30,12 +30,7 @@
"dependencies": {
"csso": "5.0.3",
"fast-glob": "3.2.11",
"gifsicle": "7.0.0",
"html-minifier-terser": "6.1.0",
"marked": "4.0.15",
"pngquant-bin": "8.0.0",
"sharp": "0.30.4",
"svgo": "2.8.0",
"terser": "5.13.1"
},
"devDependencies": {
Expand Down
149 changes: 80 additions & 69 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,64 @@
import fs from "fs";
import FastGlob from "fast-glob";
import type { AstroIntegration } from "astro";
import Options from "./options";

// @ts-ignore
import * as cssMinify from "csso";
import * as cssoMinify from "csso";
// @ts-ignore
import * as htmlMinify from "html-minifier-terser";
import Options from "./options";
import htmlMinifierTerserMinify from "html-minifier-terser";
import { minify as terserMinify } from "terser";

export interface Files {
[type: string]: string[];
}
import CSS from "./options/csso";
import HTML from "./options/html-minifier-terser";
import JS from "./options/terser";

const minify = async (
glob: string,
options: CSS | HTML | JS,
parser: "csso" | "html-minifier-terser" | "terser"
) => {
const files = await FastGlob(glob);

for (const file of files) {
switch (parser) {
case "csso":
await fs.promises.readFile(file, "utf-8").then(async (data) => {
await fs.promises.writeFile(
file,
cssoMinify.minify(data, options).css,
"utf-8"
);
});

break;

case "html-minifier-terser":
await fs.promises.readFile(file, "utf-8").then(async (data) => {
await fs.promises.writeFile(
file,
htmlMinifierTerserMinify.minify(data, options),
"utf-8"
);
});

break;

case "terser":
await fs.promises.writeFile(
file,
// @ts-ignore
terserMinify(file, options),
"utf-8"
);

break;

default:
break;
}
}
};

export default function createPlugin(
integrationOptions: Options = {}
Expand Down Expand Up @@ -64,6 +113,16 @@ export default function createPlugin(
trimCustomFragments: false,
useShortDoctype: true,
},
js: {
ecma: 5,
enclose: false,
keep_classnames: false,
keep_fnames: false,
ie8: false,
module: false,
safari10: false,
toplevel: false,
},
};

const options = Object.assign(defaultOptions, integrationOptions);
Expand All @@ -75,69 +134,21 @@ export default function createPlugin(
return {
name: "astro-critters",
hooks: {
"astro:build:done": async ({ pages }) => {
const files: Files = {
css: !options.css
? []
: FastGlob.sync(`${options.path}**/*.css`),
html: !options.html
? []
: pages.map((page) => {
const pathname = page.pathname.endsWith("/")
? page.pathname
: `${page.pathname}/`;

const file =
pathname === "404/"
? "404.html"
: `${pathname}index.html`;

return `${options.path}${file}`;
}),
};

for (const type in files) {
if (Object.prototype.hasOwnProperty.call(files, type)) {
for (const file of files[type]) {
const data = await fs.promises.readFile(
file,
"utf-8"
);

switch (type) {
// csso
case "css":
await fs.promises.writeFile(
file,
cssMinify.minify(data, options.css).css,
"utf-8"
);
break;

// gifsicle
// marked
// pngquant-bin
// sharp
// svgo
// terser

// html-minifier
case "html":
await fs.promises.writeFile(
file,
await htmlMinify.minify(
data,
options.html
),
"utf-8"
);
break;

default:
break;
}
}
}
"astro:build:done": async () => {
if (options.css) {
minify(`${options.path}**/*.css`, options.css, "csso");
}

if (options.html) {
minify(
`${options.path}**/*.html`,
options.html,
"html-minifier-terser"
);
}

if (options.js) {
minify(`${options.path}**/*.js`, options.js, "terser");
}
},
},
Expand Down
12 changes: 9 additions & 3 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CSS from "./options/csso";
import HTML from "./options/html-minifier";
import HTML from "./options/html-minifier-terser";
import JS from "./options/terser";

export default interface Options {
/**
Expand All @@ -11,10 +12,15 @@ export default interface Options {
/**
* [csso] options.
*/
css?: CSS | false;
css?: CSS;

/**
* [html-minifier-terser] options.
*/
html?: HTML | false;
html?: HTML;

/**
* [terser] options.
*/
js?: JS;
}
File renamed without changes.
3 changes: 3 additions & 0 deletions src/options/terser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { MinifyOptions } from "terser";

export default interface JS extends MinifyOptions {}

0 comments on commit 9598cb7

Please sign in to comment.