Skip to content

Commit

Permalink
add files to the JSON result #20
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Aug 3, 2024
1 parent 8d20d8b commit 9c2bf3f
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 73 deletions.
46 changes: 23 additions & 23 deletions dist/browser-umd.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/browser-umd.js.map

Large diffs are not rendered by default.

46 changes: 23 additions & 23 deletions dist/browser.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/browser.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/critical/fontscript.js.map

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

13 changes: 13 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ interface CriticalCliStats {
interface CriticalCliResult {
fonts: Array<string>,
stats: Array<CriticalCliStats>,

files: {

html?: string;
fonts?: string;
css: {

min?: string;
raw?: string;
nested?: string;
minNested?: string;
}
},
styles: any[];
html: string
}
Expand Down
36 changes: 28 additions & 8 deletions dist/index.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/index.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ export interface CriticalCliStats {
export interface CriticalCliResult {
fonts: Array<string>,
stats: Array<CriticalCliStats>,

files: {

html?: string;
fonts?: string;
css: {

min?: string;
raw?: string;
nested?: string;
minNested?: string;
}
},
styles: any[];
html: string
}
Expand Down
4 changes: 2 additions & 2 deletions src/critical/extract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {resolve} from "../file/path";
import {resolve} from "../file";
import {fontscript} from "./fontscript";
import {
import type {
CriticalExtractOptions,
CriticalResult,
FileMapObject,
Expand Down
2 changes: 1 addition & 1 deletion src/critical/fontscript.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {FontObject} from "../@types";
import type {FontObject} from "../@types";

export function fontscript(fonts: FontObject[]): string {

Expand Down
3 changes: 3 additions & 0 deletions src/critical/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

export * from './extract';
export * from './fontscript';
4 changes: 4 additions & 0 deletions src/file/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export * from './download';
export * from './path';
export * from './size';
2 changes: 1 addition & 1 deletion src/file/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function resolve(path: string, from: string): string {
}

// @ts-ignore
const baseURL: URL = new URL(from, window?.location);
const baseURL: URL = new URL(from, window.location);
const pathURL: URL = new URL(path, baseURL);

if (baseURL.protocol != pathURL.protocol ||
Expand Down
46 changes: 35 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as playwright from "playwright";
import {Browser, BrowserContext, BrowserType, ConsoleMessage, devices, LaunchOptions, Page, Request} from "playwright";
import {basename, dirname, resolve} from "path";
import {basename, dirname, resolve} from "node:path";
import {readFileSync} from "node:fs";
import {fontscript} from "./critical/fontscript.js";
import {size} from "./file/size.js";
import {
import {fontscript} from "./critical";
import {size} from "./file";
import type {
BrowserOptions,
CriticalCliResult,
CriticalCliStats,
Expand Down Expand Up @@ -71,7 +71,7 @@ export async function critical(url: string | CriticalOptions, options: CriticalO

await page.goto(options.input.startsWith('data:') ? options.input : 'data:text/html;base64,' + Buffer.from(options.input).toString('base64'), {waitUntil: 'networkidle'});

let base = options.base ?? 'file://' + process.cwd();
let base: string = options.base ?? 'file://' + process.cwd();

if (!base.endsWith('/')) {

Expand Down Expand Up @@ -116,6 +116,7 @@ export async function critical(url: string | CriticalOptions, options: CriticalO

if (['"', "'"].includes(url.charAt(0))) {

// @ts-ignore
url = url.replace(/^(['"])([^\1\s]+)\1$/, '$2');
}

Expand Down Expand Up @@ -439,6 +440,13 @@ export async function critical(url: string | CriticalOptions, options: CriticalO
}
}

let htmlFile: string | undefined = undefined;
let fontJS: string | undefined = undefined;
let minCssFile: string | undefined = undefined;
let rawCssFile: string | undefined = undefined;
let nestedCssFile: string | undefined = undefined;
let minNestedCssFile: string | undefined = undefined;

if (options.filename) {

const rawCSS: string = [...styles].join('\n');
Expand All @@ -461,11 +469,11 @@ export async function critical(url: string | CriticalOptions, options: CriticalO
cssFile += '.css';
}

const minCssFile: string = cssFile.slice(0, -4) + '.min.css';
const rawCssFile: string = cssFile.slice(0, -4) + '.raw.css';
minCssFile = cssFile.slice(0, -4) + '.min.css' as string;
rawCssFile = cssFile.slice(0, -4) + '.raw.css' as string;

const minNestedCssFile: string = cssFile.slice(0, -4) + '.nested.min.css';
const nestedCssFile: string = cssFile.slice(0, -4) + '.nested.css';
minNestedCssFile = cssFile.slice(0, -4) + '.nested.min.css' as string;
nestedCssFile = cssFile.slice(0, -4) + '.nested.css' as string;

console.error(chalk.blue(`[${shortUrl}]> writing css at `) + chalk.green(minCssFile + ' [' + size(code.length) + ']'));
// @ts-ignore
Expand Down Expand Up @@ -499,8 +507,9 @@ export async function critical(url: string | CriticalOptions, options: CriticalO
html = html.replace(match[0], `<style data-critical="true">${[...styles].join('\n')}</style>`);
}

htmlFile = options.filename + '.html';
// @ts-ignore
await writeFile(`${options.filename}.html`, html);
await writeFile(htmlFile, html);
}

const fontObjects: Set<FontObject> = <Set<FontObject>>new Set([...fonts].map((font: string) => JSON.parse(font)));
Expand Down Expand Up @@ -529,5 +538,20 @@ export async function critical(url: string | CriticalOptions, options: CriticalO
await writeFile(fontJS, data);
}

return {styles: [...styles], fonts: [...fonts], stats, html};
return {
styles: [...styles],
files: {

html: htmlFile,
fonts: fontJS,
css: {

min: minCssFile,
raw: rawCssFile,
nested: nestedCssFile,
minNested: minNestedCssFile
}

}, fonts: [...fonts], stats, html
};
}

0 comments on commit 9c2bf3f

Please sign in to comment.