MCPcopy
hub / github.com/webpack/webpack / apply

Method apply

lib/url/URLParserPlugin.js:110–272  ·  view source on GitHub ↗

* Applies the plugin by registering its hooks on the compiler. * @param {JavascriptParser} parser the parser * @returns {void}

(parser)

Source from the content-addressed store, hash-verified

108 * @returns {void}
109 */
110 apply(parser) {
111 const relative = this.options.url === "relative";
112
113 parser.hooks.canRename.for("URL").tap(PLUGIN_NAME, approve);
114 parser.hooks.evaluateNewExpression.for("URL").tap(PLUGIN_NAME, (expr) => {
115 const evaluatedExpr = getEvaluatedExpr(expr, parser);
116 const request = evaluatedExpr && evaluatedExpr.asString();
117
118 if (!request) return;
119 const url = new URL(request, getUrl(parser.state.module));
120
121 return new BasicEvaluatedExpression()
122 .setString(url.toString())
123 .setRange(/** @type {Range} */ (expr.range));
124 });
125 parser.hooks.new.for("URL").tap(PLUGIN_NAME, (_expr) => {
126 const expr = /** @type {NewExpressionNode} */ (_expr);
127 const { options: importOptions, errors: commentErrors } =
128 parser.parseCommentOptions(/** @type {Range} */ (expr.range));
129
130 if (commentErrors) {
131 for (const e of commentErrors) {
132 const { comment } = e;
133 parser.state.module.addWarning(
134 new CommentCompilationWarning(
135 `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
136 /** @type {DependencyLocation} */ (comment.loc)
137 )
138 );
139 }
140 }
141
142 if (importOptions && importOptions.webpackIgnore !== undefined) {
143 if (typeof importOptions.webpackIgnore !== "boolean") {
144 parser.state.module.addWarning(
145 new UnsupportedFeatureWarning(
146 `\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
147 /** @type {DependencyLocation} */ (expr.loc)
148 )
149 );
150 return;
151 } else if (importOptions.webpackIgnore) {
152 if (expr.arguments.length !== 2) return;
153
154 const [, arg2] = expr.arguments;
155
156 if (arg2.type !== "MemberExpression" || !isMetaUrl(parser, arg2)) {
157 return;
158 }
159
160 const dep = new ConstDependency(
161 RuntimeGlobals.baseURI,
162 /** @type {Range} */ (arg2.range),
163 [RuntimeGlobals.baseURI]
164 );
165 dep.loc = /** @type {DependencyLocation} */ (expr.loc);
166 parser.state.module.addPresentationalDependency(dep);
167

Callers

nothing calls this directly

Calls 14

getEvaluatedExprFunction · 0.85
isMetaUrlFunction · 0.85
tapMethod · 0.80
setRangeMethod · 0.80
setStringMethod · 0.80
getUrlFunction · 0.70
addDependencyMethod · 0.65
asStringMethod · 0.45
toStringMethod · 0.45
parseCommentOptionsMethod · 0.45

Tested by

no test coverage detected