MCPcopy
hub / github.com/webpack/webpack / parseModuleUrl

Method parseModuleUrl

lib/dependencies/WorkerPlugin.js:128–171  ·  view source on GitHub ↗
(parser, expr)

Source from the content-addressed store, hash-verified

126 * @returns {[string, Range] | void} parsed
127 */
128 const parseModuleUrl = (parser, expr) => {
129 if (expr.type !== "NewExpression" || expr.callee.type === "Super") {
130 return;
131 }
132 if (
133 expr.arguments.length === 1 &&
134 expr.arguments[0].type === "MemberExpression" &&
135 isMetaUrl(parser, expr.arguments[0])
136 ) {
137 const arg1 = expr.arguments[0];
138 return [
139 getUrl(parser.state.module),
140 [
141 /** @type {Range} */ (arg1.range)[0],
142 /** @type {Range} */ (arg1.range)[1]
143 ]
144 ];
145 } else if (expr.arguments.length === 2) {
146 const [arg1, arg2] = expr.arguments;
147 if (arg1.type === "SpreadElement") return;
148 if (arg2.type === "SpreadElement") return;
149 const callee = parser.evaluateExpression(expr.callee);
150 if (!callee.isIdentifier() || callee.identifier !== "URL") return;
151 const arg2Value = parser.evaluateExpression(arg2);
152 if (
153 !arg2Value.isString() ||
154 !(
155 /** @type {string} */ (arg2Value.string).startsWith("file://")
156 ) ||
157 arg2Value.string !== getUrl(parser.state.module)
158 ) {
159 return;
160 }
161 const arg1Value = parser.evaluateExpression(arg1);
162 if (!arg1Value.isString()) return;
163 return [
164 /** @type {string} */ (arg1Value.string),
165 [
166 /** @type {Range} */ (arg1.range)[0],
167 /** @type {Range} */ (arg2.range)[1]
168 ]
169 ];
170 }
171 };
172
173 /**
174 * Checks whether this worker plugin is meta url.

Callers

nothing calls this directly

Calls 5

isMetaUrlFunction · 0.85
evaluateExpressionMethod · 0.80
isIdentifierMethod · 0.80
isStringMethod · 0.80
getUrlFunction · 0.70

Tested by

no test coverage detected