From c4a9e3ccf7144744ebcdb128fbfb115a17a6e0bc Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 31 May 2021 12:31:34 -0400 Subject: [PATCH] Dist build --- dist/cjs/index.js | 33 +++++++++++++++++++-------------- dist/esm/index.js | 33 +++++++++++++++++++-------------- dist/index.d.ts | 32 ++++++++++++++++++++++++++++++++ package-lock.json | 8 ++++---- package.json | 2 +- 5 files changed, 75 insertions(+), 33 deletions(-) diff --git a/dist/cjs/index.js b/dist/cjs/index.js index eadef33..992a083 100644 --- a/dist/cjs/index.js +++ b/dist/cjs/index.js @@ -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; @@ -62,5 +68,4 @@ function PluginCritical(pluginConfig, callback) { } }; } -; exports.default = PluginCritical; diff --git a/dist/esm/index.js b/dist/esm/index.js index 455f769..c4c9d59 100644 --- a/dist/esm/index.js +++ b/dist/esm/index.js @@ -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; @@ -57,5 +63,4 @@ function PluginCritical(pluginConfig, callback) { } }; } -; export default PluginCritical; diff --git a/dist/index.d.ts b/dist/index.d.ts index 85f723b..54fcf39 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -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[]; + /** + * 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; } +/** + * [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; diff --git a/package-lock.json b/package-lock.json index d0dc82e..1582ead 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-critical", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -6052,9 +6052,9 @@ "integrity": "sha512-c1PTsA3tYrIsLGkJkzHF+w9F2EyxfXGo4UyJc4pFL++FMjnq0HJS69T3M7d//gKrFKwy429bouPescbjecU+Zw==" }, "ts-jest": { - "version": "27.0.1", - "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-27.0.1.tgz", - "integrity": "sha512-03qAt77QjhxyM5Bt2KrrT1WbdumiwLz989sD3IUznSp3GIFQrx76kQqSMLF7ynnxrF3/1ipzABnHxMlU8PD4Vw==", + "version": "27.0.2", + "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-27.0.2.tgz", + "integrity": "sha512-pozjHOOfm+sbv9kXCvTFVyDntWvuJztzkNFql/akD75hSMZ2jsbidVauOhBRImAopXohqcLtPK/NTTIS8Y49Ug==", "dev": true, "requires": { "bs-logger": "0.x", diff --git a/package.json b/package.json index 4e6bc07..fbc58cd 100644 --- a/package.json +++ b/package.json @@ -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": {