-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
72 lines (52 loc) · 1.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const mix = require('laravel-mix');
const merge = require('lodash/merge');
class Critical {
constructor() {
this.criticals = [];
}
dependencies() {
this.requiresReload = `
Critical-Css-Webpack-Plugin has been installed. Please run "npm run dev" again.
`;
return ['critical-css-webpack-plugin'];
}
register(config) {
if (!config.urls || config.urls.length <= 0) {
throw new Error(
'You need to provide at least 1 valid url object containing both url and template keys.'
);
}
const critical = merge({
enabled: mix.inProduction(),
paths: {},
urls: [],
options: {
inline:false
},
}, config);
if (critical.paths.suffix == null) critical.paths.suffix = '_critical.min';
this.criticals.push(critical)
}
webpackPlugins() {
if (this.criticals.map((e) => e.enabled).some(Boolean)) {
const CriticalCssPlugin = require("critical-css-webpack-plugin");
const plugins = [];
this.criticals.forEach((critical) => {
critical.enabled && critical.urls.forEach((template) => {
const criticalSrc = critical.paths.base + template.url;
const criticalDest = `${critical.paths.templates + template.template + critical.paths.suffix}.css`;
if (criticalSrc.indexOf('amp_') !== -1) {
critical.options.width = 600;
critical.options.height = 19200;
}
plugins.push(new CriticalCssPlugin(Object.assign({
src: criticalSrc,
target: criticalDest,
}, critical.options)));
});
})
return plugins;
}
}
}
mix.extend('criticalCss', new Critical());