Skip to content

Commit

Permalink
1.1.24
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Dec 19, 2022
1 parent be84abb commit c05e519
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.24

- Cleanup

## 1.1.23

- Switches ownership
Expand Down
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AstroIntegration } from "astro";
import type { Options } from "files-pipeline/dist/options/lib/critters/index.js";
import type { Options } from "./options/index.js";
declare const _default: (options?: Options) => AstroIntegration;
export default _default;
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.

4 changes: 4 additions & 0 deletions dist/options/critters.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { Options as CrittersOptions } from "critters";
export default interface CRITTERS extends CrittersOptions {
[key: string]: any;
}
8 changes: 8 additions & 0 deletions dist/options/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type CRITTERS from "./critters.js";
import type { Options as OptionsBase } from "files-pipeline/dist/options/index.js";
export interface Options extends OptionsBase {
[key: string]: any;
critters?: boolean | CRITTERS;
}
declare const _default: Options;
export default _default;
1 change: 1 addition & 0 deletions dist/options/index.js

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

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "astro-critters",
"version": "1.1.23",
"version": "1.1.24",
"type": "module",
"description": "🦔 AstroJS GoogleChromeLabs critters integration. Inline your critical CSS with Astro.",
"repository": {
Expand All @@ -24,15 +24,16 @@
"performance"
],
"scripts": {
"build": "lightrix build src/index.ts"
"build": "lightrix build src/index.ts src/options/index.ts"
},
"dependencies": {
"files-pipeline": "0.0.1"
"critters": "0.0.16",
"files-pipeline": "0.0.2"
},
"devDependencies": {
"@lightrix/config": "0.0.7",
"@lightrix/scripts": "0.1.2",
"astro": "1.7.1"
"astro": "1.7.2"
},
"publishConfig": {
"access": "public"
Expand Down
105 changes: 95 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,98 @@
import type { AstroIntegration } from "astro";

import { pipeline } from "files-pipeline";
import type { Options } from "files-pipeline/dist/options/lib/critters/index.js";

export default (options: Options = {}): AstroIntegration => ({
name: "astro-critters",
hooks: {
"astro:build:done": async () => {
await new pipeline(options).critters();
import type {
executions,
optionPath,
} from "files-pipeline/dist/options/index.js";

import type { Options } from "./options/index.js";

import defaults from "./options/index.js";

import { fileURLToPath } from "url";

import applyTo from "files-pipeline/dist/lib/apply-to.js";
import deepmerge from "files-pipeline/dist/lib/deepmerge.js";

import { files } from "files-pipeline";

// critters
// @ts-ignore
import Critters from "critters";

export default (options: Options = {}): AstroIntegration => {
for (const option in options) {
if (
Object.prototype.hasOwnProperty.call(options, option) &&
options[option] === true
) {
options[option] = defaults[option];
}
}

options = deepmerge(defaults, options);

const paths = new Set<optionPath>();

if (typeof options["path"] !== "undefined") {
if (
options["path"] instanceof Array ||
options["path"] instanceof Set
) {
for (const path of options["path"]) {
paths.add(path);
}
} else {
paths.add(options["path"]);
}
}

return {
name: "astro-critters",
hooks: {
"astro:build:done": async () => {
for (const path of paths) {
const _path = await applyTo(path, (url: URL | string) =>
url instanceof URL ? fileURLToPath(url) : url
);

const critters = new Critters(
deepmerge(options["critters"], {
path:
_path instanceof Map
? _path.keys().next().value
: _path,
logLevel: (() => {
switch (options["logger"]) {
case 0:
return "silent";
// rome-ignore lint/nursery/noPrecisionLoss:
case 1:
return "silent";
// rome-ignore lint/nursery/noPrecisionLoss:
case 2:
return "info";
default:
return "info";
}
})(),
})
);

await (
await (
await (
await new files(options["logger"]).in(path)
).by("**/*.html")
).not(options["exclude"])
).pipeline(
deepmerge(defaults["pipeline"], {
wrote: async (current) =>
critters.process(current.buffer),
} satisfies executions)
);
}
},
},
},
});
};
};
7 changes: 7 additions & 0 deletions src/options/critters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @ts-ignore
import type { Options as CrittersOptions } from "critters";

export default interface CRITTERS extends CrittersOptions {
// rome-ignore lint/suspicious/noExplicitAny:
[key: string]: any;
}
33 changes: 33 additions & 0 deletions src/options/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import defaults from "files-pipeline/dist/options/index.js";
import deepmerge from "files-pipeline/dist/lib/deepmerge.js";

import type CRITTERS from "./critters.js";

import type { Options as OptionsBase } from "files-pipeline/dist/options/index.js";

export interface Options extends OptionsBase {
// rome-ignore lint/suspicious/noExplicitAny:
[key: string]: any;

critters?: boolean | CRITTERS;
}

export default deepmerge(defaults, {
critters: {
preload: "media",
inlineFonts: true,
compress: true,
pruneSource: true,
},
pipeline: {
failed: async (current) =>
`Error: Cannot inline file ${current.inputPath}!`,
fulfilled: async (pipe) =>
pipe.files > 0
? `Successfully inlined a total of ${pipe.files} HTML ${
pipe.files === 1 ? "file" : "files"
}.`
: false,
accomplished: false,
},
} satisfies Options) as Options;

0 comments on commit c05e519

Please sign in to comment.