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
+20
View File
@@ -0,0 +1,20 @@
Copyright JS Foundation and other contributors
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+1164
View File
File diff suppressed because it is too large Load Diff
+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
+87
View File
@@ -0,0 +1,87 @@
{
"name": "html-loader",
"version": "5.1.0",
"description": "Html loader module for webpack",
"license": "MIT",
"repository": "webpack-contrib/html-loader",
"author": "Tobias Koppers @sokra",
"homepage": "https://github.com/webpack-contrib/html-loader",
"bugs": "https://github.com/webpack-contrib/html-loader/issues",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/webpack"
},
"main": "dist/cjs.js",
"engines": {
"node": ">= 18.12.0"
},
"scripts": {
"start": "npm run build -- -w",
"clean": "del-cli dist",
"validate:runtime": "es-check es5 \"dist/runtime/**/*.js\"",
"prebuild": "npm run clean",
"build": "cross-env NODE_ENV=production babel src -d dist --copy-files",
"postbuild": "npm run validate:runtime",
"commitlint": "commitlint --from=master",
"security": "npm audit --production",
"lint:prettier": "prettier --cache --list-different .",
"lint:js": "eslint --cache .",
"lint:spelling": "cspell --cache --no-must-find-files --quiet \"**/*.*\"",
"lint": "npm-run-all -l -p \"lint:**\"",
"fix:js": "npm run lint:js -- --fix",
"fix:prettier": "npm run lint:prettier -- --write",
"fix": "npm-run-all -l fix:js fix:prettier",
"test:only": "cross-env NODE_ENV=test jest",
"test:watch": "npm run test:only -- --watch",
"test:coverage": "npm run test:only -- --coverage",
"pretest": "npm run lint",
"test": "npm run test:coverage",
"prepare": "husky install && npm run build",
"release": "standard-version"
},
"files": [
"dist"
],
"peerDependencies": {
"webpack": "^5.0.0"
},
"dependencies": {
"html-minifier-terser": "^7.2.0",
"parse5": "^7.1.2"
},
"devDependencies": {
"@babel/cli": "^7.24.7",
"@babel/core": "^7.24.7",
"@babel/preset-env": "^7.24.7",
"@commitlint/cli": "^18.6.1",
"@commitlint/config-conventional": "^18.6.2",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^29.7.0",
"cross-env": "^7.0.3",
"cspell": "^8.9.1",
"del": "^7.1.0",
"del-cli": "^5.1.0",
"es-check": "^7.2.1",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"handlebars": "^4.7.8",
"html-webpack-plugin": "^5.6.0",
"husky": "^8.0.3",
"jest": "^29.7.0",
"lint-staged": "^15.2.7",
"memfs": "^4.9.3",
"npm-run-all": "^4.1.5",
"posthtml": "^0.16.6",
"posthtml-webp": "^2.2.0",
"prettier": "^3.3.2",
"standard-version": "^9.5.0",
"unescape-unicode": "^0.2.0",
"webpack": "^5.92.1"
},
"keywords": [
"webpack",
"html",
"loader"
]
}