Passed
Branch master (e5fd7d)
by Andrew
03:38
created

src/index.ts   A

Complexity

Total Complexity 9
Complexity/F 4.5

Size

Lines of Code 75
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 85.14%

Importance

Changes 0
Metric Value
wmc 9
eloc 49
mnd 7
bc 7
fnc 2
dl 0
loc 75
ccs 63
cts 74
cp 0.8514
rs 10
bpm 3.5
cpm 4.5
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
C index.ts ➔ PluginCritical 0 50 9
1 1
import {Plugin} from 'rollup';
2 1
import * as path from 'path';
3 1
import {generate} from 'critical';
4 1
5 1
const criticalSuffix = '_critical.min.css';
6 1
7 1
/**
8 1
 * Default `criticalConfig` passed in to `critical`
9 1
 */
10 1
const defaultCriticalConfig: Partial<CriticalConfig> = {
11 1
  inline: false,
12 1
  extract: false,
13 1
  width: 1200,
14 1
  height: 1200,
15 1
  penthouse: {
16 1
    blockJSRequests: false
17 1
  }
18 1
};
19 1
20 1
/**
21 1
 * [Vite.js](https://vitejs.dev/) & [Rollup](https://rollupjs.org/) plugin for generating critical CSS
22 1
 * that uses the [critical](https://github.com/addyosmani/critical) generator under the hood.
23 1
 *
24 1
 * @param {CriticalPluginConfig} pluginConfig - the plugin configuration object
25 1
 * @param {Function} callback - callback upon completion of the critical CSS generation
26 1
 * @constructor
27 1
 */
28 1
export function PluginCritical(pluginConfig: CriticalPluginConfig, callback?: CriticalPluginCallback): Plugin {
29 2
  return {
30 2
    name: 'critical',
31 2
    async writeBundle(outputOptions, bundle) {
32 2
      const css: Array<string> = [];
33 2
      // Find all of the generated CSS assets
34 2
      for (const chunk of Object.values(bundle)) {
35 2
        if (chunk.type === 'asset' && chunk.fileName.endsWith('.css')) {
36 2
          const cssFile = path.join(outputOptions.dir || '', chunk.fileName);
37 2
          css.push(cssFile);
38 2
        }
39 2
      }
40 2
      // If we have no CSS, skip bundle
41 2
      if (!css.length) {
42
        return;
43
      }
44
      // Iterate through the pages
45 2
      for (const page of pluginConfig.criticalPages) {
46 2
        const criticalBase = pluginConfig.criticalBase;
47 2
        const criticalSrc = pluginConfig.criticalUrl + page.uri;
48 2
        // If inline is set to true, use HTML as target, otherwise CSS with suffix
49 2
        const criticalTarget = (pluginConfig.criticalConfig && pluginConfig.criticalConfig.inline == true) ? page.template + ".html" : page.template + criticalSuffix;
50 2
        // Merge in our options
51 2
        const options = Object.assign(
52 2
            { css },
53 2
            defaultCriticalConfig,
54 2
            {
55 2
              base: criticalBase,
56 2
              src: criticalSrc,
57 2
              target: criticalTarget,
58 2
            },
59 2
            pluginConfig.criticalConfig
60 2
        );
61 2
        // Generate the Critical CSS
62 2
        console.log(`Generating critical CSS from ${criticalSrc} to ${criticalTarget}`);
63 2
        await generate(options, (err: string) => {
64
          if (err) {
65
            console.error(err);
66
          }
67
          if (callback) {
68
            callback(err);
69
          }
70 1
        });
71
      }
72
    }
73 2
  }
74
}
75