forked from nystudio107/rollup-plugin-critical
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
397 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var __require = (x) => { | ||
if (typeof require !== "undefined") | ||
return require(x); | ||
throw new Error('Dynamic require of "' + x + '" is not supported'); | ||
}; | ||
|
||
// src/index.ts | ||
import path from "path"; | ||
var critical = __require("critical"); | ||
var criticalSuffix = "_critical.min.css"; | ||
var defaultCriticalConfig = { | ||
inline: false, | ||
minify: true, | ||
extract: false, | ||
width: 1200, | ||
height: 1200, | ||
penthouse: { | ||
blockJSRequests: false | ||
} | ||
}; | ||
function PluginCritical(pluginConfig, callback) { | ||
return { | ||
name: "critical", | ||
async writeBundle(outputOptions, bundle) { | ||
const css = []; | ||
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 (!css.length) { | ||
return; | ||
} | ||
for (const page of pluginConfig.criticalPages) { | ||
const criticalBase = pluginConfig.criticalBase; | ||
const criticalSrc = pluginConfig.criticalUrl + page.uri; | ||
const criticalDest = page.template + criticalSuffix; | ||
const options = Object.assign({css}, defaultCriticalConfig, { | ||
base: criticalBase, | ||
src: criticalSrc, | ||
target: criticalDest | ||
}, pluginConfig.criticalConfig); | ||
console.log(`Generating critical CSS from ${criticalSrc} to ${criticalDest}`); | ||
await critical.generate(options, (err) => { | ||
if (err) { | ||
console.error(err); | ||
} | ||
if (callback) { | ||
callback(err); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
var src_default = PluginCritical; | ||
export { | ||
src_default as default | ||
}; | ||
//# sourceMappingURL=index.mjs.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"version": 3, | ||
"sources": ["../src/index.ts"], | ||
"sourcesContent": ["import {Plugin} from 'rollup';\nimport path from 'path';\nimport { CriticalConfig } from './@types/critical';\nimport { CriticalPluginConfig } from './@types/rollup-plugin-critical';\nconst critical = require('critical');\n\nconst criticalSuffix = '_critical.min.css';\n\n/**\n * Default `criticalConfig` passed in to `critical`\n */\nconst defaultCriticalConfig: Partial<CriticalConfig> = {\n inline: false,\n minify: true,\n extract: false,\n width: 1200,\n height: 1200,\n penthouse: {\n blockJSRequests: false\n }\n};\n\n/**\n * [Vite.js](https://vitejs.dev/) & [Rollup](https://rollupjs.org/) plugin for generating critical CSS\n * that uses the [critical](https://github.com/addyosmani/critical) generator under the hood.\n *\n * @param {CriticalPluginConfig} pluginConfig - the plugin configuration object\n * @param {Function} callback - callback upon completion of the critical CSS generation\n * @constructor\n */\nfunction PluginCritical(pluginConfig: CriticalPluginConfig, callback?: Function): Plugin {\n return {\n name: 'critical',\n async writeBundle(outputOptions, bundle) {\n const css: Array<string> = [];\n // Find all of the generated CSS assets\n for (const chunk of Object.values(bundle)) {\n if (chunk.type === 'asset' && chunk.fileName.endsWith('.css')) {\n const cssFile = path.join(outputOptions.dir || '', chunk.fileName);\n css.push(cssFile);\n }\n }\n // If we have no CSS, skip bundle\n if (!css.length) {\n return;\n }\n // Iterate through the pages\n for (const page of pluginConfig.criticalPages) {\n const criticalBase = pluginConfig.criticalBase;\n const criticalSrc = pluginConfig.criticalUrl + page.uri;\n const criticalDest = page.template + criticalSuffix;\n // Merge in our options\n const options = Object.assign(\n { css },\n defaultCriticalConfig,\n {\n base: criticalBase,\n src: criticalSrc,\n target: criticalDest,\n },\n pluginConfig.criticalConfig\n );\n // Generate the Critical CSS\n console.log(`Generating critical CSS from ${criticalSrc} to ${criticalDest}`);\n await critical.generate(options, (err: string) => {\n if (err) {\n console.error(err);\n }\n if (callback) {\n callback(err);\n }\n });\n }\n }\n }\n}\n\nexport default PluginCritical;\n"], | ||
"mappings": ";;;;;;;AACA;AAGA,IAAM,WAAW,UAAQ;AAEzB,IAAM,iBAAiB;AAKvB,IAAM,wBAAiD;AAAA,EACrD,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,SAAS;AAAA,EACT,OAAO;AAAA,EACP,QAAQ;AAAA,EACR,WAAW;AAAA,IACT,iBAAiB;AAAA;AAAA;AAYrB,wBAAwB,cAAoC,UAA6B;AACvF,SAAO;AAAA,IACL,MAAM;AAAA,UACA,YAAY,eAAe,QAAQ;AACvC,YAAM,MAAqB;AAE3B,iBAAW,SAAS,OAAO,OAAO,SAAS;AACzC,YAAI,MAAM,SAAS,WAAW,MAAM,SAAS,SAAS,SAAS;AAC7D,gBAAM,UAAU,KAAK,KAAK,cAAc,OAAO,IAAI,MAAM;AACzD,cAAI,KAAK;AAAA;AAAA;AAIb,UAAI,CAAC,IAAI,QAAQ;AACf;AAAA;AAGF,iBAAW,QAAQ,aAAa,eAAe;AAC7C,cAAM,eAAe,aAAa;AAClC,cAAM,cAAc,aAAa,cAAc,KAAK;AACpD,cAAM,eAAe,KAAK,WAAW;AAErC,cAAM,UAAU,OAAO,OACnB,CAAE,MACF,uBACA;AAAA,UACE,MAAM;AAAA,UACN,KAAK;AAAA,UACL,QAAQ;AAAA,WAEV,aAAa;AAGjB,gBAAQ,IAAI,gCAAgC,kBAAkB;AAC9D,cAAM,SAAS,SAAS,SAAS,CAAC,QAAgB;AAChD,cAAI,KAAK;AACP,oBAAQ,MAAM;AAAA;AAEhB,cAAI,UAAU;AACZ,qBAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAQrB,IAAO,cAAQ;", | ||
"names": [] | ||
} |
Oops, something went wrong.