MCPcopy
hub / github.com/webpack/webpack / emitUrlFunction

Method emitUrlFunction

lib/css/CssParser.js:2539–2609  ·  view source on GitHub ↗
(fn, fname)

Source from the content-addressed store, hash-verified

2537 * @param {string} fname the function name, lower-cased
2538 */
2539 const emitUrlFunction = (fn, fname) => {
2540 if (fname === "url" || fname === "src") {
2541 // Quoted `url("…")` / `src("…")`: first non-whitespace value must be the string token.
2542 const first = A.children(fn)[nextNonWhitespace(A.children(fn), 0)];
2543 if (!first || A.type(first) !== NodeType.String) return;
2544 const string = /** @type {Token} */ (first);
2545 if (
2546 webpackIgnored(
2547 [lastTokenEndForComments, A.start(fn)],
2548 A.start(string),
2549 A.end(string)
2550 )
2551 ) {
2552 return;
2553 }
2554 const value = normalizeUrl(
2555 input.slice(A.start(string) + 1, A.end(string) - 1),
2556 true
2557 );
2558 // Ignore `url()`, `url('')` and `url("")`, they are valid by spec
2559 if (value.length === 0) return;
2560 const dep = new CssUrlDependency(
2561 value,
2562 [A.start(string), A.end(string)],
2563 "string"
2564 );
2565 setDepLoc(dep, A.start(string), A.end(string));
2566 module.addDependency(dep);
2567 module.addCodeGenerationDependency(dep);
2568 } else if (IMAGE_SET_FUNCTION.test(fname)) {
2569 // `image-set(…)`: each comma segment's first string is the URL; advance the magic-comment fence per string.
2570 lastTokenEndForComments = A.nameEnd(fn) + 1;
2571 let prevStringEnd = A.start(fn);
2572 let firstInSegment = true;
2573 for (const cv of A.children(fn)) {
2574 if (A.type(cv) === NodeType.Comma) {
2575 firstInSegment = true;
2576 continue;
2577 }
2578 if (A.type(cv) === NodeType.Whitespace) continue;
2579 const wasFirst = firstInSegment;
2580 firstInSegment = false;
2581 if (!wasFirst || A.type(cv) !== NodeType.String) continue;
2582 const string = /** @type {Token} */ (cv);
2583 const start = prevStringEnd;
2584 prevStringEnd = A.end(string);
2585 const value = normalizeUrl(
2586 input.slice(A.start(string) + 1, A.end(string) - 1),
2587 true
2588 );
2589 if (value.length === 0) continue;
2590 if (
2591 webpackIgnored(
2592 [start, A.end(string)],
2593 A.start(string),
2594 A.end(string)
2595 )
2596 ) {

Callers

nothing calls this directly

Calls 7

nextNonWhitespaceFunction · 0.85
normalizeUrlFunction · 0.85
sliceMethod · 0.80
addDependencyMethod · 0.65
typeMethod · 0.45
testMethod · 0.45

Tested by

no test coverage detected