-
Notifications
You must be signed in to change notification settings - Fork 5
/
IndexHtmlSource.js
181 lines (145 loc) · 5.5 KB
/
IndexHtmlSource.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var path = require('path');
var cheerio = require('cheerio');
var URI = require('URIjs');
var _ = require('lodash');
var Source = require('webpack/lib/Source');
/**
* @class
* @extends Source
* @param {Module} sourceModule
* @param {Chunk} sourceChunk
* @param {Compilation} compilation
*/
function IndexHtmlSource(sourceModule, sourceChunk, compilation) {
this.sourceModule = sourceModule;
this.sourceChunk = sourceChunk;
this.compilation = compilation;
}
module.exports = IndexHtmlSource;
IndexHtmlSource.prototype = Object.create(Source.prototype);
IndexHtmlSource.prototype.constructor = IndexHtmlSource;
IndexHtmlSource.prototype.source = function() {
var html = this._getHtmlFromModule();
var $ = cheerio.load(html);
coalesceLinks($);
this._resolveScripts($);
return $.html();
};
/**
* Extracts the HTML code from the module source
*/
IndexHtmlSource.prototype._getHtmlFromModule = function() {
var compilation = this.compilation;
var sourceChunk = this.sourceChunk;
function moduleWasExtracted(module) {
return module.loaders && module.loaders.some(function(loader) {
return loader.match(/extract-text-webpack-plugin/);
})
}
function getExtractTextLoaderOptions(module) {
for (var i = 0; i < module.loaders.length; i++) {
var loader = module.loaders[i];
var match = loader.match(/extract-text-webpack-plugin[\\/]loader\.js\?({.*})/);
if (match) {
return JSON.parse(match[1]);
}
}
}
function getExtractedFilename(module) {
var options = getExtractTextLoaderOptions(module);
var loaderId = options && options.id;
var extractTextPlugin = _.find(compilation.compiler.options.plugins,
function (p) {
return (p.constructor.name === 'ExtractTextPlugin') &&
((typeof(p.id) === "undefined" && typeof(loaderId) === "undefined") || (p.id === loaderId));
});
var filenamePattern = extractTextPlugin.filename
.replace(/\[(?:\w+:)?(contenthash)(?::[a-z]+\d*)?(?::(\d+))?]|([^\[\]]+)/ig,
function(match, contentHash, maxLength, literalPart) {
if (contentHash) {
if (maxLength) {
return '[a-f0-9]{1,' + maxLength + '}';
} else {
return '[a-f0-9]+';
}
} else {
return regexpQuote(literalPart);
}
});
filenamePattern = new RegExp(filenamePattern);
return _.find(sourceChunk.files, function(filename) {
return filename.match(filenamePattern);
});
}
function __webpack_require__(moduleId) {
var sourceModule;
if (typeof moduleId === "number") {
sourceModule = _.find(compilation.modules, function (m) {
return m.id === moduleId;
});
} else {
sourceModule = moduleId;
}
if (!sourceModule) {
return undefined;
}
if (_.endsWith(sourceModule.context, path.normalize('webpack-dev-server/client')) ||
_.endsWith(sourceModule.context, path.normalize('webpack/hot'))) {
return undefined;
} else if (moduleWasExtracted(sourceModule)) {
return (compilation.options.output.publicPath || '') + getExtractedFilename(sourceModule);
} else {
var module = {};
var source = sourceModule.source(null, {});
eval(source.source());
return module.exports;
}
}
// This is where the "real" __webpack_require__ would store the public path,
// it is used by url-loader to construct the link
__webpack_require__.p = compilation.options.output.publicPath || '';
return __webpack_require__(this.sourceModule);
};
/**
* Resolve <script> tags that refer to entry points by replacing them with the final names of the bundles.
* @param $
*/
IndexHtmlSource.prototype._resolveScripts = function($) {
var compilation = this.compilation;
var sourceContext = this.sourceModule.context;
$('script').each(function () {
var scriptSrc = $(this).attr('src');
if (scriptSrc) {
var scriptSrcUri = new URI(scriptSrc);
if (!scriptSrcUri.is('absolute')) {
var entry = path.resolve(sourceContext, scriptSrc);
var moduleForEntry = _.find(compilation.modules, function (module) {
return module.resource && path.normalize(module.resource) === entry
});
if (moduleForEntry) {
var chunkForEntry = moduleForEntry.chunks[0];
var chunkJsFile = _.find(chunkForEntry.files, function (file) {
return new URI(file).filename().match(/\.js$/)
});
if (chunkJsFile) {
$(this).attr('src', (compilation.options.output.publicPath || '') + chunkJsFile);
}
}
}
}
});
};
function regexpQuote(s) {
return s.toString().replace(/[-\\^$*+?.()|[\]{}]/g, "\\$&");
}
/**
* Coalesce all links with the same rel and href into one
* @param $
*/
function coalesceLinks($) {
$('link').each(function () {
var rel = $(this).attr('rel');
var href = $(this).attr('href');
$(this).nextAll("link[rel='" + rel + "'][href='" + href + "']").remove();
});
}