Passed
Branch develop (785de4)
by Andrew
03:03
created

index.ts ➔ PluginCritical   C

Complexity

Conditions 10

Size

Total Lines 45
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10.0862

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 45
ccs 19
cts 21
cp 0.9048
rs 5.9999
c 0
b 0
f 0
cc 10
crap 10.0862

How to fix   Complexity   

Complexity

Complex classes like index.ts ➔ PluginCritical often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import {Plugin} from 'rollup';
2 1
import path from 'path';
3 1
const critical = require('critical');
4
5 1
const criticalSuffix = '_critical.min.css';
6
7 1
const defaultCriticalConfig = {
8
  base: './',
9
  inline: false,
10
  minify: true,
11
  extract: false,
12
  width: 1200,
13
  height: 1200,
14
  concurrency: 4,
15
  penthouse: {
16
    blockJSRequests: false
17
  }
18
};
19
20
interface CriticalPages {
21
  uri: string;
22
  template: string;
23
}
24
25
interface CriticalPluginConfig {
26
  criticalUrl: string;
27
  criticalBase?: string;
28
  pages: Partial<CriticalPages>[];
29
  criticalConfig?: Partial<CriticalConfig>;
30
}
31
32
function PluginCritical(pluginConfig: CriticalPluginConfig, callback?: Function): Plugin {
33 1
  return {
34
    name: 'critical',
35
    async writeBundle(outputOptions, bundle) {
36 1
      const css: Array<string> = [];
37
      // Find all of the generated CSS assets
38 2
      if (bundle) {
39 1
        for (const chunk of Object.values(bundle)) {
40 4
          if (chunk.type === 'asset' && chunk.fileName.endsWith('.css')) {
41 2
            if (outputOptions.dir !== undefined) {
42 1
              const cssFile = path.join(outputOptions.dir, chunk.fileName);
43 1
              css.push(cssFile);
44
            }
45
          }
46
        }
47
        // If we have no CSS, skip bundle
48 2
        if (!css.length) {
49
          return;
50
        }
51
      }
52
      // Iterate through the pages
53 1
      for (const page of pluginConfig.pages) {
54 1
        const criticalBase = pluginConfig.criticalBase;
55 1
        const criticalSrc = pluginConfig.criticalUrl + page.uri;
56 1
        const criticalDest = page.template + criticalSuffix;
57
        // Merge in our options
58 1
        const options = Object.assign(
59
            { css },
60
            defaultCriticalConfig,
61
            {
62
              base: criticalBase,
63
              src: criticalSrc,
64
              target: criticalDest,
65
            },
66
            pluginConfig.criticalConfig
67
        );
68
        // Generate the Critical CSS
69 1
        console.log(`Generating critical CSS from ${criticalSrc} to ${criticalDest}`);
70 1
        await critical.generate(options, (err: string) => {
71 2
          if (err) {
72
            console.error(err);
73
          }
74 2
          if (callback) {
75 1
            callback(err);
76
          }
77
        });
78
      }
79
    }
80
  }
81
};
82
83
export default PluginCritical
84