Initial commit

This commit is contained in:
2025-03-07 19:22:02 +01:00
commit 4a98255d83
55743 changed files with 5280367 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function getIndices(value) {
const result = [];
let index = value.indexOf("\n");
while (index !== -1) {
result.push(index + 1);
index = value.indexOf("\n", index + 1);
}
result.push(value.length + 1);
return result;
}
function offsetToPosition(source, offset) {
let index = -1;
const indices = getIndices(source);
const {
length
} = indices;
if (offset < 0) {
return {};
}
// eslint-disable-next-line no-plusplus
while (++index < length) {
if (indices[index] > offset) {
return {
line: index + 1,
column: offset - (indices[index - 1] || 0) + 1,
offset
};
}
}
return {};
}
class HtmlSourceError extends Error {
constructor(error, startOffset, endOffset, source) {
super(error);
this.name = "HtmlSourceError";
this.message = `${this.name}: ${this.message}`;
this.startOffset = startOffset;
this.endOffset = endOffset;
this.source = source;
const startPosition = offsetToPosition(source, this.startOffset);
const endPosition = offsetToPosition(source, this.endOffset);
this.message += ` (From line ${startPosition.line}, column ${startPosition.column}; to line ${endPosition.line}, column ${endPosition.column})`;
// We don't need stack
this.stack = false;
}
}
exports.default = HtmlSourceError;
+6
View File
@@ -0,0 +1,6 @@
"use strict";
const loader = require("./index");
module.exports = loader.default;
module.exports.raw = loader.raw;
module.exports.defaultMinimizerOptions = loader.defaultMinimizerOptions;
+74
View File
@@ -0,0 +1,74 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = loader;
Object.defineProperty(exports, "defaultMinimizerOptions", {
enumerable: true,
get: function () {
return _utils.defaultMinimizerOptions;
}
});
var _plugins = require("./plugins");
var _utils = require("./utils");
var _options = _interopRequireDefault(require("./options.json"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
async function loader(content) {
const rawOptions = this.getOptions(_options.default);
const options = (0, _utils.normalizeOptions)(rawOptions, this);
if (options.preprocessor) {
// eslint-disable-next-line no-param-reassign
content = await options.preprocessor(content, this);
}
const plugins = [];
const errors = [];
const imports = [];
const replacements = [];
let isSupportAbsoluteURL = false;
// TODO enable by default in the next major release
if (this._compilation && this._compilation.options && this._compilation.options.experiments && this._compilation.options.experiments.buildHttp) {
isSupportAbsoluteURL = true;
}
if (options.sources) {
plugins.push((0, _plugins.sourcesPlugin)({
isSupportAbsoluteURL,
isSupportDataURL: options.esModule,
sources: options.sources,
resourcePath: this.resourcePath,
context: this.context,
imports,
errors,
replacements
}));
}
if (options.minimize) {
plugins.push((0, _plugins.minimizerPlugin)({
minimize: options.minimize,
errors
}));
}
let {
html
} = await (0, _utils.pluginRunner)(plugins).process(content);
for (const error of errors) {
this.emitError(error instanceof Error ? error : new Error(error));
}
const isTemplateLiteralSupported = (0, _utils.supportTemplateLiteral)(this);
html = (isTemplateLiteralSupported ? (0, _utils.convertToTemplateLiteral)(html) : JSON.stringify(html)
// Invalid in JavaScript but valid HTML
).replace(/[\u2028\u2029]/g, str => str === "\u2029" ? "\\u2029" : "\\u2028");
if (options.postprocessor) {
// eslint-disable-next-line no-param-reassign
html = await options.postprocessor(html, this);
}
const importCode = (0, _utils.getImportCode)(html, imports, options);
const moduleCode = (0, _utils.getModuleCode)(html, replacements, this, {
esModule: options.esModule,
isTemplateLiteralSupported
});
const exportCode = (0, _utils.getExportCode)(html, options);
return `${importCode}${moduleCode}${exportCode}`;
}
+87
View File
@@ -0,0 +1,87 @@
{
"title": "HTML Loader options",
"type": "object",
"definitions": {
"Source": {
"anyOf": [
{
"type": "object",
"properties": {
"tag": {
"type": "string",
"minLength": 1
},
"attribute": {
"type": "string",
"minLength": 1
},
"type": {
"enum": ["src", "srcset"]
},
"filter": {
"instanceof": "Function"
}
},
"required": ["attribute", "type"],
"additionalProperties": false
},
{
"enum": ["..."]
}
]
},
"SourcesList": {
"type": "array",
"items": {
"$ref": "#/definitions/Source"
},
"minItems": 1,
"uniqueItems": true
}
},
"properties": {
"preprocessor": {
"instanceof": "Function",
"description": "Allows pre-processing of content before handling.",
"link": "https://github.com/webpack-contrib/html-loader#preprocessor"
},
"postprocessor": {
"instanceof": "Function",
"description": "Allows post-processing of content before handling.",
"link": "https://github.com/webpack-contrib/html-loader#postprocessor"
},
"sources": {
"anyOf": [
{ "type": "boolean" },
{
"type": "object",
"properties": {
"list": {
"$ref": "#/definitions/SourcesList"
},
"urlFilter": {
"instanceof": "Function"
},
"scriptingEnabled": {
"type": "boolean"
}
},
"additionalProperties": false
}
],
"description": "By default every loadable attributes (for example - <img src='image.png'>) is imported (const img = require('./image.png'). You may need to specify loaders for images in your configuration.",
"link": "https://github.com/webpack-contrib/html-loader#sources"
},
"minimize": {
"anyOf": [{ "type": "boolean" }, { "type": "object" }],
"description": "Tell html-loader to minimize HTML.",
"link": "https://github.com/webpack-contrib/html-loader#minimize"
},
"esModule": {
"type": "boolean",
"description": "Enable or disable ES modules syntax.",
"link": "https://github.com/webpack-contrib/html-loader#esmodule"
}
},
"additionalProperties": false
}
+20
View File
@@ -0,0 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "minimizerPlugin", {
enumerable: true,
get: function () {
return _minimizerPlugin.default;
}
});
Object.defineProperty(exports, "sourcesPlugin", {
enumerable: true,
get: function () {
return _sourcesPlugin.default;
}
});
var _sourcesPlugin = _interopRequireDefault(require("./sources-plugin"));
var _minimizerPlugin = _interopRequireDefault(require("./minimizer-plugin"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
+17
View File
@@ -0,0 +1,17 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _htmlMinifierTerser = require("html-minifier-terser");
var _default = options => async function process(html) {
try {
// eslint-disable-next-line no-param-reassign
html = await (0, _htmlMinifierTerser.minify)(html, options.minimize);
} catch (error) {
options.errors.push(error);
}
return html;
};
exports.default = _default;
+160
View File
@@ -0,0 +1,160 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _parse = require("parse5");
var _utils = require("../utils");
const DOUBLE_QUOTE = '"'.charCodeAt(0);
const SINGLE_QUOTE = "'".charCodeAt(0);
var _default = options => function process(html) {
const sources = [];
const document = (0, _parse.parse)(html, {
sourceCodeLocationInfo: true,
scriptingEnabled: options.sources.scriptingEnabled
});
let needIgnore = false;
(0, _utils.traverse)(document, node => {
const {
tagName,
attrs: attributes,
sourceCodeLocation
} = node;
if (node.nodeName === "#comment") {
const match = node.data.match(_utils.webpackIgnoreCommentRegexp);
if (match) {
needIgnore = match[2] === "true";
}
return;
}
if (!tagName) {
return;
}
if (needIgnore) {
needIgnore = false;
return;
}
attributes.forEach(attribute => {
let {
name
} = attribute;
name = attribute.prefix ? `${attribute.prefix}:${name}` : name;
const handlers = new Map([...(options.sources.list.get("*") || new Map()), ...(options.sources.list.get(tagName.toLowerCase()) || new Map())]);
if (handlers.size === 0) {
return;
}
const handler = handlers.get(name.toLowerCase());
if (!handler) {
return;
}
if (handler.filter && !handler.filter(tagName, name, attributes, options.resourcePath)) {
return;
}
const attributeAndValue = html.slice(sourceCodeLocation.attrs[name].startOffset, sourceCodeLocation.attrs[name].endOffset);
const isValueQuoted = attributeAndValue.charCodeAt(attributeAndValue.length - 1) === DOUBLE_QUOTE || attributeAndValue.charCodeAt(attributeAndValue.length - 1) === SINGLE_QUOTE;
const valueStartOffset = sourceCodeLocation.attrs[name].startOffset + attributeAndValue.indexOf(attribute.value);
const valueEndOffset = sourceCodeLocation.attrs[name].endOffset - (isValueQuoted ? 1 : 0);
const optionsForTypeFn = {
tag: tagName,
startTag: {
startOffset: sourceCodeLocation.startTag.startOffset,
endOffset: sourceCodeLocation.startTag.endOffset
},
endTag: sourceCodeLocation.endTag ? {
startOffset: sourceCodeLocation.endTag.startOffset,
endOffset: sourceCodeLocation.endTag.endOffset
} :
// eslint-disable-next-line no-undefined
undefined,
attributes,
attribute: name,
attributePrefix: attribute.prefix,
attributeNamespace: attribute.namespace,
attributeStartOffset: sourceCodeLocation.attrs[name].startOffset,
attributeEndOffset: sourceCodeLocation.attrs[name].endOffset,
value: attribute.value,
isSupportAbsoluteURL: options.isSupportAbsoluteURL,
isSupportDataURL: options.isSupportDataURL,
isValueQuoted,
valueEndOffset,
valueStartOffset,
html
};
let result;
try {
result = handler.type(optionsForTypeFn);
} catch (error) {
options.errors.push(error);
}
result = Array.isArray(result) ? result : [result];
for (const source of result) {
if (!source) {
// eslint-disable-next-line no-continue
continue;
}
sources.push({
...source,
name,
isValueQuoted
});
}
});
});
const urlFilter = (0, _utils.getFilter)(options.sources.urlFilter);
const imports = new Map();
const replacements = new Map();
let offset = 0;
for (const source of sources) {
const {
name,
value,
isValueQuoted,
startOffset,
endOffset
} = source;
let request = value;
if (!urlFilter(name, value, options.resourcePath)) {
// eslint-disable-next-line no-continue
continue;
}
let hash;
const indexHash = request.lastIndexOf("#");
if (indexHash >= 0) {
hash = request.substring(indexHash);
request = request.substring(0, indexHash);
}
request = (0, _utils.requestify)(options.context, request);
let importName = imports.get(request);
if (!importName) {
importName = `___HTML_LOADER_IMPORT_${imports.size}___`;
imports.set(request, importName);
options.imports.push({
importName,
request
});
}
const replacementKey = JSON.stringify({
request,
isValueQuoted,
hash
});
let replacementName = replacements.get(replacementKey);
if (!replacementName) {
replacementName = `___HTML_LOADER_REPLACEMENT_${replacements.size}___`;
replacements.set(replacementKey, replacementName);
options.replacements.push({
replacementName,
importName,
hash,
isValueQuoted
});
}
// eslint-disable-next-line no-param-reassign
html = html.slice(0, startOffset + offset) + replacementName + html.slice(endOffset + offset);
offset += startOffset + replacementName.length - endOffset;
}
return html;
};
exports.default = _default;
+14
View File
@@ -0,0 +1,14 @@
"use strict";
module.exports = function (url, maybeNeedQuotes) {
if (!url) {
return url;
}
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = String(url);
if (maybeNeedQuotes && /[\t\n\f\r "'=<>`]/.test(url)) {
return "\"".concat(url, "\"");
}
return url;
};
+5
View File
@@ -0,0 +1,5 @@
"use strict";
module.exports = function () {
return "a { color: red; }";
};
+1002
View File
File diff suppressed because it is too large Load Diff