MCPcopy
hub / github.com/webpack/webpack / parse

Method parse

lib/html/HtmlParser.js:850–1495  ·  view source on GitHub ↗

* Parses the provided source and updates the parser state. * @param {string | Buffer | PreparsedAst} source the source to parse * @param {ParserState} state the parser state * @returns {ParserState} the parser state

(source, state)

Source from the content-addressed store, hash-verified

848 * @returns {ParserState} the parser state
849 */
850 parse(source, state) {
851 if (Buffer.isBuffer(source)) {
852 source = source.toString("utf8");
853 } else if (typeof source === "object") {
854 throw new Error("webpackAst is unexpected for the HtmlParser");
855 }
856 if (source[0] === "\uFEFF") {
857 source = source.slice(1);
858 }
859
860 const locConverter = new LocConverter(source);
861
862 const module = state.module;
863 const compilation = state.compilation;
864 const { hashFunction, module: outputModule } = compilation.outputOptions;
865 const context = compilation.compiler.context;
866 const css = Boolean(compilation.options.experiments.css);
867
868 // Stable, per-HTML-module prefix used when generating entry names for
869 // script src / modulepreload references so they don't collide across
870 // HTML modules in the same compilation. We hash the module's resource
871 // path (a plain absolute path) — going through `contextify` against
872 // the compilation root keeps the hash machine-stable for the same
873 // project layout. Note: `module.identifier()` returns `html|<path>`
874 // for HTML modules, which doesn't start with `/`, so contextify would
875 // leave it absolute. `module.resource` is the bare path.
876 /** @type {string} */
877 const resource =
878 /** @type {EXPECTED_ANY} */ (module).resource || module.identifier();
879 const moduleHash = createHash(hashFunction || "md4")
880 .update(context ? contextify(context, resource) : resource)
881 .digest("hex")
882 .slice(0, 8);
883
884 // Script src / modulepreload references are collected per-type
885 // during the walk; HtmlModulesPlugin later turns them into real
886 // entries. `script` and `script-module` entries are chained via a
887 // leader-only dependOn so they share a runtime.
888 // `<link rel="modulepreload">` entries are kept independent — they
889 // must preload without running, so they can never become a runtime
890 // leader that other entries would import.
891 /**
892 * @typedef {object} EntryScriptInfo
893 * @property {string} request
894 * @property {string} entryName
895 * @property {"script" | "script-module" | "modulepreload" | "stylesheet"} type
896 */
897 /** @type {EntryScriptInfo[]} */
898 const scriptEntries = [];
899 /** @type {EntryScriptInfo[]} */
900 const scriptModuleEntries = [];
901 /** @type {EntryScriptInfo[]} */
902 const modulePreloadEntries = [];
903 /** @type {EntryScriptInfo[]} */
904 const stylesheetEntries = [];
905
906 // Offset of the first script tag; anchors injected stylesheet `<link>`s
907 // before it so a later entry's CSS still loads ahead of every script.

Callers

nothing calls this directly

Calls 15

getMethod · 0.95
createHashFunction · 0.85
decodeHtmlEntitiesFunction · 0.85
findAttrFunction · 0.85
attrSourceSpanFunction · 0.85
isASCIIWhitespaceFunction · 0.85
buildHtmlAstFunction · 0.85
sliceMethod · 0.80
useMethod · 0.80

Tested by

no test coverage detected