Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Aug 17, 2022
1 parent ad9a056 commit 8439654
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 74 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.0

- First stable version

## 0.0.11

- Refactor
Expand Down
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AstroIntegration } from "astro";
import Options from "./options";
import { Options } from "./options";
/**
* It takes in an object of options, and returns an object that Astro can use to create a plugin
* @param {Options} integrationOptions - Options = {}
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.

9 changes: 9 additions & 0 deletions dist/lib/parse.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* It takes a glob, a write function, and a read function, and then it parses all the files in the glob
* with the write function, and then it writes the result to the file
* @param {string} glob - The glob pattern to match files.
* @param write - (data: string) => Promise<string> = async (data) => data,
* @param read - (file: string) => Promise<string> = async (file) =>
*/
declare const _default: (glob: string, write?: (data: string) => Promise<string>, read?: (file: string) => Promise<string>) => Promise<void>;
export default _default;
1 change: 1 addition & 0 deletions dist/lib/parse.js

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

8 changes: 8 additions & 0 deletions dist/lib/pipe-all.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Options } from "../options";
/**
* It takes a settings object, creates a new Critters instance, and then parses all HTML files in the
* given directory, passing each file's contents to the Critters instance
* @param {Options} settings - Options
*/
declare const _default: (settings: Options) => Promise<void>;
export default _default;
1 change: 1 addition & 0 deletions dist/lib/pipe-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import a from"critters";import p from"./parse";var c=async t=>{const r=new a(t);await p(`${t.path}**/*.html`,async o=>await r.process(o))};export{c as default};
4 changes: 3 additions & 1 deletion dist/options.d.ts → dist/options/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Options as CritterOptions } from "critters";
export default interface Options extends CritterOptions {
export interface Options extends CritterOptions {
[key: string]: any;
/**
* Astro build path.
* @default "./dist/"
*/
path?: string;
}
declare const _default: Options;
export default _default;
1 change: 1 addition & 0 deletions dist/options/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var r={path:"./dist/",preload:"swap",inlineFonts:!0,compress:!0};export{r as default};
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "astro-critters",
"version": "0.0.11",
"version": "1.0.0",
"type": "module",
"description": "🦔 AstroJS GoogleChromeLabs critters integration. Inline your critical CSS with Astro.",
"repository": {
Expand All @@ -24,7 +24,7 @@
"performance"
],
"scripts": {
"build": "esbuild --format=esm --platform=node --target=node14 --sources-content=false --minify --outdir=dist src/index.ts && tsc"
"build": "ts-node ./scripts/build.ts && tsc"
},
"dependencies": {
"critters": "0.0.16",
Expand All @@ -34,7 +34,8 @@
"devDependencies": {
"@types/node": "18.7.6",
"astro": "1.0.6",
"esbuild": "0.15.4",
"esbuild": "0.15.5",
"ts-node": "10.9.1",
"typescript": "4.7.4"
}
}
31 changes: 31 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import fs from "fs";
import esbuild, { BuildOptions, Plugin } from "esbuild";

const outDir = "./dist";

const cleanDist: Plugin = {
name: "clean-dist",
setup(build) {
build.onStart(() => {
fs.rm(outDir, { recursive: true }, () => {});
});
},
};

const options: BuildOptions = {
entryPoints: [
"src/index.ts",
"src/lib/parse.ts",
"src/lib/pipe-all.ts",
"src/options/index.ts",
],
format: "esm",
minify: true,
outdir: outDir,
platform: "node",
target: "node14",
write: true,
plugins: [cleanDist],
};

await esbuild.build(options);
59 changes: 2 additions & 57 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,8 @@
import type { AstroIntegration } from "astro";
import { deepmerge } from "deepmerge-ts";
import FastGlob from "fast-glob";
import fs from "fs";

import Options from "./options";

import Critters from "critters";

/**
* It takes a settings object, creates a new Critters instance, and then parses all HTML files in the
* given directory, passing each file's contents to the Critters instance
* @param {Options} settings - Options
*/
const pipeAll = async (settings: Options) => {
const critters = new Critters(settings);

await parse(
`${settings.path}**/*.html`,
async (data) => await critters.process(data)
);
};

/**
* It takes a glob, a write function, and a read function, and then it parses all the files in the glob
* with the write function, and then it writes the result to the file
* @param {string} glob - The glob pattern to match files.
* @param write - (data: string) => Promise<string> = async (data) => data,
* @param read - (file: string) => Promise<string> = async (file) =>
*/
const parse = async (
glob: string,
write: (data: string) => Promise<string> = async (data) => data,
read: (file: string) => Promise<string> = async (file) =>
await fs.promises.readFile(file, "utf-8")
) => {
const files = await FastGlob(glob);

for (const file of files) {
try {
const writeBuffer = await write(await read(file));

if (!writeBuffer) {
continue;
}

await fs.promises.writeFile(file, writeBuffer, "utf-8");
} catch (error) {
console.log("Error: Cannot inline file " + file + " CSS!");
}
}
};
import defaultOptions, { Options } from "./options";
import pipeAll from "./lib/pipe-all";

/**
* It takes in an object of options, and returns an object that Astro can use to create a plugin
Expand All @@ -59,14 +12,6 @@ const parse = async (
export default function createPlugin(
integrationOptions: Options = {}
): AstroIntegration {
const defaultOptions: Options = {
path: "./dist/",
// @ts-ignore
preload: "swap-high",
inlineFonts: true,
compress: true,
};

const _options = deepmerge(defaultOptions, integrationOptions);

_options.path = _options.path?.endsWith("/")
Expand Down
32 changes: 32 additions & 0 deletions src/lib/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import FastGlob from "fast-glob";
import fs from "fs";

/**
* It takes a glob, a write function, and a read function, and then it parses all the files in the glob
* with the write function, and then it writes the result to the file
* @param {string} glob - The glob pattern to match files.
* @param write - (data: string) => Promise<string> = async (data) => data,
* @param read - (file: string) => Promise<string> = async (file) =>
*/
export default async (
glob: string,
write: (data: string) => Promise<string> = async (data) => data,
read: (file: string) => Promise<string> = async (file) =>
await fs.promises.readFile(file, "utf-8")
) => {
const files = await FastGlob(glob);

for (const file of files) {
try {
const writeBuffer = await write(await read(file));

if (!writeBuffer) {
continue;
}

await fs.promises.writeFile(file, writeBuffer, "utf-8");
} catch (error) {
console.log("Error: Cannot inline file " + file + " CSS!");
}
}
};
18 changes: 18 additions & 0 deletions src/lib/pipe-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Critters from "critters";

import { Options } from "../options";
import parse from "./parse";

/**
* It takes a settings object, creates a new Critters instance, and then parses all HTML files in the
* given directory, passing each file's contents to the Critters instance
* @param {Options} settings - Options
*/
export default async (settings: Options) => {
const critters = new Critters(settings);

await parse(
`${settings.path}**/*.html`,
async (data) => await critters.process(data)
);
};
11 changes: 0 additions & 11 deletions src/options.ts

This file was deleted.

18 changes: 18 additions & 0 deletions src/options/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Options as CritterOptions } from "critters";

export interface Options extends CritterOptions {
[key: string]: any;

/**
* Astro build path.
* @default "./dist/"
*/
path?: string;
}

export default {
path: "./dist/",
preload: "swap",
inlineFonts: true,
compress: true,
} as Options;
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"include": [
"src"
],
"ts-node": {
"esm": true
},
"compilerOptions": {
"allowJs": true,
"baseUrl": ".",
Expand Down

0 comments on commit 8439654

Please sign in to comment.