| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
- */
- var htmlMinifier = require("html-minifier");
- var attrParse = require("./lib/attributesParser");
- var loaderUtils = require("loader-utils");
- var url = require("url");
- var assign = require("object-assign");
- var compile = require("es6-templates").compile;
- function randomIdent() {
- return "xxxHTMLLINKxxx" + Math.random() + Math.random() + "xxx";
- }
- function getLoaderConfig(context) {
- var query = loaderUtils.getOptions(context) || {};
- var configKey = query.config || 'htmlLoader';
- var config = context.options && context.options.hasOwnProperty(configKey) ? context.options[configKey] : {};
- delete query.config;
- return assign(query, config);
- }
- module.exports = function(content) {
- this.cacheable && this.cacheable();
- var config = getLoaderConfig(this);
- var attributes = ["img:src"];
- if(config.attrs !== undefined) {
- if(typeof config.attrs === "string")
- attributes = config.attrs.split(" ");
- else if(Array.isArray(config.attrs))
- attributes = config.attrs;
- else if(config.attrs === false)
- attributes = [];
- else
- throw new Error("Invalid value to config parameter attrs");
- }
- var root = config.root;
- var links = attrParse(content, function(tag, attr) {
- return attributes.indexOf(tag + ":" + attr) >= 0;
- });
- links.reverse();
- var data = {};
- content = [content];
- links.forEach(function(link) {
- if(!loaderUtils.isUrlRequest(link.value, root)) return;
- var uri = url.parse(link.value);
- if (uri.hash !== null && uri.hash !== undefined) {
- uri.hash = null;
- link.value = uri.format();
- link.length = link.value.length;
- }
- do {
- var ident = randomIdent();
- } while(data[ident]);
- data[ident] = link.value;
- var x = content.pop();
- content.push(x.substr(link.start + link.length));
- content.push(ident);
- content.push(x.substr(0, link.start));
- });
- content.reverse();
- content = content.join("");
- if (config.interpolate === 'require'){
- var reg = /\$\{require\([^)]*\)\}/g;
- var result;
- var reqList = [];
- while(result = reg.exec(content)){
- reqList.push({
- length : result[0].length,
- start : result.index,
- value : result[0]
- })
- }
- reqList.reverse();
- content = [content];
- reqList.forEach(function(link) {
- var x = content.pop();
- do {
- var ident = randomIdent();
- } while(data[ident]);
- data[ident] = link.value.substring(11,link.length - 3)
- content.push(x.substr(link.start + link.length));
- content.push(ident);
- content.push(x.substr(0, link.start));
- });
- content.reverse();
- content = content.join("");
- }
- if(typeof config.minimize === "boolean" ? config.minimize : this.minimize) {
- var minimizeOptions = assign({}, config);
- [
- "removeComments",
- "removeCommentsFromCDATA",
- "removeCDATASectionsFromCDATA",
- "collapseWhitespace",
- "conservativeCollapse",
- "removeAttributeQuotes",
- "useShortDoctype",
- "keepClosingSlash",
- "minifyJS",
- "minifyCSS",
- "removeScriptTypeAttributes",
- "removeStyleTypeAttributes",
- ].forEach(function(name) {
- if(typeof minimizeOptions[name] === "undefined") {
- minimizeOptions[name] = true;
- }
- });
- content = htmlMinifier.minify(content, minimizeOptions);
- }
- if(config.interpolate && config.interpolate !== 'require') {
- content = compile('`' + content + '`').code;
- } else {
- content = JSON.stringify(content);
- }
- var exportsString = "module.exports = ";
- if (config.exportAsDefault) {
- exportsString = "exports.default = ";
- } else if (config.exportAsEs6Default) {
- exportsString = "export default ";
- }
- return exportsString + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
- if(!data[match]) return match;
- return '" + require(' + JSON.stringify(loaderUtils.urlToRequest(data[match], root)) + ') + "';
- }) + ";";
- }
|