Skip to content

Commit

Permalink
Merge branch 'release/1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Welch committed May 31, 2021
2 parents 84becd8 + 57b4f19 commit 1ef449c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file.

## 1.0.1 - 2021-05-31
### Changed
* Remove unneeded conditionals
* Improve comment typehints in jsdoc
* Remove vestigial `concurrency` property

## 1.0.0 - 2021-05-30
### Added
* Initial release
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,25 @@ export default {

### `criticalUrl: string`

The base URL to use in combination with the `criticalPages` `uri`s to determine the URLs to scrape for Critical CSS
The base URL to use in combination with the `criticalPages` `uri`s to determine the URLs to scrape for Critical CSS.

This can also be a file system path. This is combined with `criticalPages.uri` (see below) to determine pages to scrap for critical CSS.

Determines the `criticalConfig.src` property (see below)

### `criticalBase: string`

The base file system path to where the generated Critical CSS file should be stored
The base file system path to where the generated Critical CSS file should be saved.

This is combined with `criticalPages.template` (see below) with `_critical.min.css` appended to it to determine the saved critical CSS file name.

Determines the `criticalConfig.target` property (see below)

### `criticalPages: array of objects`

An array objects that contain the page `uri`s that are combined with the `criticalUrl` to determine the URLs to scrape for Critical CSS. The resulting files are named with the `template` path, and saved to the `criticalBase` directory
An array objects that contain the page `uri`s that are combined with the `criticalUrl` to determine the URLs to scrape for Critical CSS.

The resulting files are named with the `template` path, and saved to the `criticalBase` directory

### `criticalConfig: object`

Expand All @@ -69,7 +79,6 @@ const defaultCriticalConfig = {
extract: false,
width: 1200,
height: 1200,
concurrency: 4,
penthouse: {
blockJSRequests: false
}
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": "rollup-plugin-critical",
"version": "1.0.0",
"version": "1.0.1",
"description": "Rollup plugin to generate critical CSS.",
"author": "nystudio107",
"license": "MIT",
Expand Down
60 changes: 45 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,80 @@ const critical = require('critical');

const criticalSuffix = '_critical.min.css';

const defaultCriticalConfig = {
/**
* Default `criticalConfig` passed in to `critical`
*/
const defaultCriticalConfig: Partial<CriticalConfig> = {
inline: false,
minify: true,
extract: false,
width: 1200,
height: 1200,
concurrency: 4,
penthouse: {
blockJSRequests: false
}
};

interface CriticalPages {
/** Combined with `criticalUrl` to determine the URLs to scrape for Critical CSS */
uri: string;
/** Critical CSS files are named with the `template` path, and saved to the `criticalBase` directory */
template: string;
}

interface CriticalPluginConfig {
/**
* The base URL to use in combination with the `criticalPages` `uri`s to determine the URLs to scrape for Critical CSS.
* This can also be a file system path. This is combined with `criticalPages.uri`
* to determine pages to scrap for critical CSS.
* Determines the `criticalConfig.src` property
*/
criticalUrl: string;
/**
* The base file system path to where the generated Critical CSS file should be saved.
* This is combined with `criticalPages.template` with `_critical.min.css` appended
* to it to determine the saved critical CSS file name.
* Determines the `criticalConfig.target` property
*/
criticalBase?: string;
/**
* An array objects that contain the page `uri`s that are combined with the `criticalUrl` to
* determine the URLs to scrape for Critical CSS. The resulting files are named with the
* `template` path, and saved to the `criticalBase` directory
*/
criticalPages: Partial<CriticalPages>[];
/**
* This is the full [config for critical](https://github.com/addyosmani/critical#options) that is passed
* through to the `critical` package.
* You may optionally override any properties you like here
*/
criticalConfig?: Partial<CriticalConfig>;
}

/**
* [Vite.js](https://vitejs.dev/) & [Rollup](https://rollupjs.org/) plugin for generating critical CSS
* that uses the [critical](https://github.com/addyosmani/critical) generator under the hood.
*
* @param {CriticalPluginConfig} pluginConfig - the plugin configuration object
* @param {Function} callback - callback upon completion of the critical CSS generation
* @constructor
*/
function PluginCritical(pluginConfig: CriticalPluginConfig, callback?: Function): Plugin {
return {
name: 'critical',
async writeBundle(outputOptions, bundle) {
const css: Array<string> = [];
// Find all of the generated CSS assets
if (bundle) {
for (const chunk of Object.values(bundle)) {
if (chunk.type === 'asset' && chunk.fileName.endsWith('.css')) {
if (outputOptions.dir !== undefined) {
const cssFile = path.join(outputOptions.dir, chunk.fileName);
css.push(cssFile);
}
}
}
// If we have no CSS, skip bundle
if (!css.length) {
return;
for (const chunk of Object.values(bundle)) {
if (chunk.type === 'asset' && chunk.fileName.endsWith('.css')) {
const cssFile = path.join(outputOptions.dir || '', chunk.fileName);
css.push(cssFile);
}
}
// If we have no CSS, skip bundle
if (!css.length) {
return;
}
// Iterate through the pages
for (const page of pluginConfig.criticalPages) {
const criticalBase = pluginConfig.criticalBase;
Expand Down Expand Up @@ -77,6 +107,6 @@ function PluginCritical(pluginConfig: CriticalPluginConfig, callback?: Function)
}
}
}
};
}

export default PluginCritical

0 comments on commit 1ef449c

Please sign in to comment.