-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (54 loc) · 1.69 KB
/
index.js
File metadata and controls
76 lines (54 loc) · 1.69 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var fs = require('fs');
var path = require('path');
var cheerio = require('cheerio');
var UglifyJs = require('uglify-js');
var utils = require('./utils');
var clientPath = path.join(__dirname, './client/index.js');
var client = '';
function LSOffline(options) {
if (!options) {
options = {};
}
if (options.cache !== false) {
options.cache = true;
}
if (options.debug !== true) {
options.debug = false;
}
this.options = options;
if(!options.debug) {
client = UglifyJs.minify(clientPath).code;
} else {
fs.readFile(clientPath, 'utf-8', function(err, data) {
client = data;
})
}
}
LSOffline.prototype.apply = function(compiler) {
var self = this;
compiler.plugin('compilation', function(compilation) {
compilation.plugin('html-webpack-plugin-after-html-processing', function(htmlPluginData, callback) {
var $ = cheerio.load(htmlPluginData.html, {
decodeEntities: false
});
htmlPluginData.assets.css.forEach(function (item) {
$('head').append(utils.createStyleTag(item))
});
/* compile javascript resource */
var chunks = Object.keys(htmlPluginData.assets.chunks);
var LS = {};
var insertTemp = '';
chunks.forEach(function(key) {
LS = utils.initConfig(htmlPluginData.assets.chunks[key].entry)
});
insertTemp += utils.createConfig(LS);
insertTemp += utils.createClient(client);
insertTemp += utils.createKit(JSON.stringify(self.options));
insertTemp = utils.appendScriptTag(insertTemp);
$('body').append(insertTemp);
htmlPluginData.html = $.html();
callback(null, htmlPluginData);
});
});
};
module.exports = LSOffline;