Skip to content

Commit

Permalink
Dist build
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Welch committed May 31, 2021
1 parent eccc540 commit c4a9e3c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 33 deletions.
33 changes: 19 additions & 14 deletions dist/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,43 @@ Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const critical = require('critical');
const criticalSuffix = '_critical.min.css';
/**
* Default `criticalConfig` passed in to `critical`
*/
const defaultCriticalConfig = {
inline: false,
minify: true,
extract: false,
width: 1200,
height: 1200,
concurrency: 4,
penthouse: {
blockJSRequests: false
}
};
/**
* [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, callback) {
return {
name: 'critical',
async writeBundle(outputOptions, bundle) {
const css = [];
// 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_1.default.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_1.default.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 All @@ -62,5 +68,4 @@ function PluginCritical(pluginConfig, callback) {
}
};
}
;
exports.default = PluginCritical;
33 changes: 19 additions & 14 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
import path from 'path';
const critical = require('critical');
const criticalSuffix = '_critical.min.css';
/**
* Default `criticalConfig` passed in to `critical`
*/
const defaultCriticalConfig = {
inline: false,
minify: true,
extract: false,
width: 1200,
height: 1200,
concurrency: 4,
penthouse: {
blockJSRequests: false
}
};
/**
* [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, callback) {
return {
name: 'critical',
async writeBundle(outputOptions, bundle) {
const css = [];
// 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 All @@ -57,5 +63,4 @@ function PluginCritical(pluginConfig, callback) {
}
};
}
;
export default PluginCritical;
32 changes: 32 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
import { Plugin } from 'rollup';
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
*/
declare function PluginCritical(pluginConfig: CriticalPluginConfig, callback?: Function): Plugin;
export default PluginCritical;
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@types/node": "^14.6.0",
"jest": "^27.0.3",
"rollup": "^2.50.5",
"ts-jest": "^27.0.1",
"ts-jest": "^27.0.2",
"typescript": "latest"
},
"peerDependencies": {
Expand Down

0 comments on commit c4a9e3c

Please sign in to comment.