Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Oct 27, 2022
1 parent b79538f commit e6f7f08
Show file tree
Hide file tree
Showing 12 changed files with 86 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.0

- Introduces filters

## 1.0.5

- Switches ownership
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ export default {
};
```

You can provide a filter to exclude files in your build. A filter can be an
array of regexes or a single match. You can use functions, as well to match on
file names.

**`astro.config.ts`**

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

export default {
integrations: [
critters({
exclude: [
"my-awesome.html",
(file: string) => file === "./dist/index.html",
],
}),
],
};
```

[astro-critters]: https://npmjs.org/astro-critters
[critters]: https://github.com/GoogleChromeLabs/critters
[astro-integration]: https://docs.astro.build/en/guides/integrations-guide/
Expand Down
3 changes: 2 additions & 1 deletion dist/lib/parse.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
declare const _default: (glob: string, debug?: number, type?: string, write?: (data: string) => any, read?: (file: string) => any) => Promise<void>;
import type { Options } from "../options/index.js";
declare const _default: (glob: string, debug: number | undefined, type: string | undefined, exclude: Options["exclude"], write?: (data: string) => any, read?: (file: string) => any) => Promise<void>;
export default _default;
2 changes: 1 addition & 1 deletion dist/lib/parse.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/lib/pipe-all.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/options/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Options as CrittersOptions } from "critters";
export declare type excludeFn = (file: string) => boolean;
export interface Options {
[key: string]: any;
path?: string;
exclude?: string | RegExp | excludeFn | [string] | [RegExp] | [excludeFn];
critters?: boolean | CrittersOptions;
logger?: number;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/options/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var t=()=>({path:"./dist/",critters:{preload:"swap",inlineFonts:!0,compress:!0,pruneSource:!0},logger:2});export{t as default};
var e=()=>({path:"./dist/",critters:{preload:"swap",inlineFonts:!0,compress:!0,pruneSource:!0},logger:2});export{e as default};
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.0.5",
"version": "1.1.0",
"type": "module",
"description": "🦔 AstroJS GoogleChromeLabs critters integration. Inline your critical CSS with Astro.",
"repository": {
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export default (options: Options = {}): AstroIntegration => {
name: "astro-critters",
hooks: {
"astro:config:done": async (options) => {
_options.path = !_options.path
? options.config.outDir.toString()
: _options.path;
_options.path = _options.path
? _options.path
: options.config.outDir.toString();
},
"astro:build:done": async () => {
await pipeAll(_options, _options.logger);
Expand Down
46 changes: 38 additions & 8 deletions src/lib/parse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import FastGlob from "fast-glob";
import fs from "fs";
import type { Options } from "../options/index.js";

export default async (
glob: string,
debug: number = 2,
type: string = "",
exclude: Options["exclude"],
write: (data: string) => any = async (data) => data,
read: (file: string) => any = async (file) =>
await fs.promises.readFile(file, "utf-8")
Expand All @@ -16,6 +18,36 @@ export default async (
total: 0,
};

let filters = new Set();

if (typeof exclude !== "undefined") {
if (exclude instanceof Array) {
for (const excludes of exclude) {
filters.add(excludes);
}
} else {
filters.add(exclude);
}
}

for (const filter of filters) {
if (typeof filter === "string") {
for (const file of files) {
if (file.match(filter)) {
files.splice(files.indexOf(file), 1);
}
}
}

if (typeof filter === "function") {
for (const file of files) {
if (filter(file)) {
files.splice(files.indexOf(file), 1);
}
}
}
}

for (const file of files) {
try {
const writeBuffer = await write(await read(file));
Expand All @@ -28,19 +60,17 @@ export default async (

inlines.files++;
} catch (error) {
console.log("Error: Cannot inline file " + file + "!");
console.log(`Error: Cannot inline file ${file}!`);
}
}

if (debug > 0 && inlines.files > 0) {
console.info(
"\u001b[32mSuccessfully inlined a total of " +
inlines.files +
" " +
type.toUpperCase() +
" " +
(inlines.files === 1 ? "file" : "files") +
".\u001b[39m"
`\u001b[32mSuccessfully inlined a total of ${
inlines.files
} ${type.toUpperCase()} ${
inlines.files === 1 ? "file" : "files"
}.\u001b[39m`
);
}
};
12 changes: 7 additions & 5 deletions src/lib/pipe-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ export default async (settings: Options, debug: number = 2) => {
}

switch (files) {
case "critters":
case "critters": {
await parse(
`${settings.path}**/*.html`,
debug,
"html",
async (data) => {
setting.path = !setting.path
? settings.path
: setting.path;
settings?.exclude,
async (data: any) => {
setting.path = setting.path
? setting.path
: settings.path;

return await new Critters(setting).process(data);
}
);
break;
}

default:
break;
Expand Down
5 changes: 5 additions & 0 deletions src/options/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type { Options as CrittersOptions } from "critters";
export type excludeFn = (file: string) => boolean;

export interface Options {
[key: string]: any;
path?: string;

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

critters?: boolean | CrittersOptions;

logger?: number;
}

Expand Down

0 comments on commit e6f7f08

Please sign in to comment.