-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (43 loc) · 1.56 KB
/
index.js
File metadata and controls
59 lines (43 loc) · 1.56 KB
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
'use strict';
var _ = require('lodash');
var path = require('path');
var Downloader = require('lib/downloader');
let googleFontsUrl = "fonts.googleapis.com/css?family=";
function GoogleFontsWebpackPlugin(options) {
// Default options
this.options = _.extend({
fonts: [],
ssl: true,
download: false,
path: __dirname
}, options);
}
GoogleFontsWebpackPlugin.prototype.apply = function(compiler) {
// Generate Font URL's
var fontUrls = this.options.fonts.map(function(font,index){
let fontName = Object.keys(font)[0];
let fontOptions = fontName[fontName];
let fontUrl = (this.options.ssl ? "https://" : "http://")
+ googleFontsUrl + fontName.replace(' ', '+') + ':' + font[fontName];
return fontUrl;
});
compiler.plugin('compilation', function(compilation) {
if (this.download) {
var dl = new Downloader();
this.options.fonts.map(function(font) {
dl.requestFonts(font, this.options.path);
})
}
else {
compilation.plugin('html-webpack-plugin-before-html-generation', function(htmlPLuginData, callback) {
let cssAssets = htmlPLuginData.assets.css;
// Add fonts to beginning of CSS Assets
fontUrls.map(function(url) {
cssAssets.unshift(url);
})
callback();
});
}
});
};
module.exports = GoogleFontsWebpackPlugin;