Skip to content

Commit

Permalink
1.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Nov 13, 2022
1 parent 14bfd8b commit 2c20c83
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 21 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.4

- Adds multiple paths

## 1.1.3

- Bug fix
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ export default {
};
```

You can add multiple paths to inline by specifying an array as the path
variable.

**`astro.config.ts`**

```ts
import critters from "astro-critters";

export default {
integrations: [
critters({
path: ["./build", "./dist"],
}),
],
};
```

Set logger to 0 if you do not want to see debug messages. Default is 2.

```ts
Expand Down
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.

2 changes: 2 additions & 0 deletions dist/lib/forward-slash-it.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const _default: (path: string) => string;
export default _default;
1 change: 1 addition & 0 deletions dist/lib/forward-slash-it.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var d=e=>e?.endsWith("/")?e:`${e}/`;export{d as default};
2 changes: 1 addition & 1 deletion dist/lib/pipe-all.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
declare const _default: (settings: Options, debug?: number) => Promise<void>;
declare const _default: (path: string, settings: Options, debug?: number) => Promise<void>;
export default _default;
2 changes: 1 addition & 1 deletion dist/lib/pipe-all.js

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

2 changes: 1 addition & 1 deletion dist/options/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Options as CrittersOptions } from "critters";
export declare type excludeFn = (file: string) => boolean;
export interface Options {
[key: string]: any;
path?: string;
path?: string | [string];
exclude?: string | RegExp | excludeFn | [string] | [RegExp] | [excludeFn];
critters?: boolean | CrittersOptions;
logger?: number;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "astro-critters",
"version": "1.1.3",
"version": "1.1.4",
"type": "module",
"description": "🦔 AstroJS GoogleChromeLabs critters integration. Inline your critical CSS with Astro.",
"repository": {
Expand Down
21 changes: 16 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { deepmerge } from "deepmerge-ts";

import pipeAll from "./lib/pipe-all.js";
import defaultOptions, { Options } from "./options/index.js";
import forwardSlashIt from "./lib/forward-slash-it.js";

export default (options: Options = {}): AstroIntegration => {
for (const option in options) {
Expand All @@ -16,10 +17,6 @@ export default (options: Options = {}): AstroIntegration => {

const _options = deepmerge(defaultOptions(), options);

_options.path = _options.path?.endsWith("/")
? _options.path
: `${_options.path}/`;

return {
name: "astro-critters",
hooks: {
Expand All @@ -29,7 +26,21 @@ export default (options: Options = {}): AstroIntegration => {
: options.config.outDir.toString();
},
"astro:build:done": async () => {
await pipeAll(_options, _options.logger);
let paths = new Set<string>();

if (typeof _options.path !== "undefined") {
if (_options.path instanceof Array) {
for (const path of _options.path) {
paths.add(forwardSlashIt(path));
}
} else {
paths.add(forwardSlashIt(_options.path));
}
}

for (const path of paths) {
await pipeAll(path, _options, _options.logger);
}
},
},
};
Expand Down
1 change: 1 addition & 0 deletions src/lib/forward-slash-it.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default (path: string) => (path?.endsWith("/") ? path : `${path}/`);
4 changes: 3 additions & 1 deletion src/lib/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export default async (
debug: number = 2,
type: string = "",
exclude: Options["exclude"],
// rome-ignore lint:
write: (data: string) => any = async (data) => data,
// rome-ignore lint:
read: (file: string) => any = async (file) =>
await fs.promises.readFile(file, "utf-8")
) => {
Expand Down Expand Up @@ -59,7 +61,7 @@ export default async (
await fs.promises.writeFile(file, writeBuffer, "utf-8");

inlines.files++;
} catch (error) {
} catch (_error) {
console.log(`Error: Cannot inline file ${file}!`);
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/lib/pipe-all.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// @ts-ignore
import Critters from "critters";

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

export default async (settings: Options, debug: number = 2) => {
export default async (path: string, settings: Options, debug: number = 2) => {
for (const files in settings) {
if (Object.prototype.hasOwnProperty.call(settings, files)) {
const setting = settings[files];
Expand All @@ -15,18 +16,17 @@ export default async (settings: Options, debug: number = 2) => {
switch (files) {
case "critters": {
await parse(
`${settings.path}**/*.html`,
`${path}**/*.html`,
debug,
"html",
settings?.exclude,
async (data: any) => {
setting.path = setting.path
? setting.path
: settings.path;

return await new Critters(setting).process(data);
}
async (data) =>
await new Critters({
...setting,
path: setting.path ? setting.path : path,
}).process(data)
);

break;
}

Expand Down
4 changes: 3 additions & 1 deletion src/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import type { Options as CrittersOptions } from "critters";
export type excludeFn = (file: string) => boolean;

export interface Options {
// rome-ignore lint:
[key: string]: any;
path?: string;

path?: string | [string];

exclude?: string | RegExp | excludeFn | [string] | [RegExp] | [excludeFn];

Expand Down

0 comments on commit 2c20c83

Please sign in to comment.