* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 132 | * @returns {void} |
| 133 | */ |
| 134 | apply(compiler) { |
| 135 | compiler.hooks.validate.tap(PLUGIN_NAME, () => { |
| 136 | compiler.validate( |
| 137 | () => require("../../schemas/plugins/schemes/VirtualUrlPlugin.json"), |
| 138 | this.options, |
| 139 | { |
| 140 | name: "Virtual Url Plugin", |
| 141 | baseDataPath: "options" |
| 142 | }, |
| 143 | (options) => |
| 144 | require("../../schemas/plugins/schemes/VirtualUrlPlugin.check")( |
| 145 | options |
| 146 | ) |
| 147 | ); |
| 148 | }); |
| 149 | |
| 150 | const scheme = this.scheme; |
| 151 | const cachedParseResourceWithoutFragment = |
| 152 | parseResourceWithoutFragment.bindCache(compiler.root); |
| 153 | |
| 154 | compiler.hooks.compilation.tap( |
| 155 | PLUGIN_NAME, |
| 156 | (compilation, { normalModuleFactory }) => { |
| 157 | compilation.hooks.assetPath.tap( |
| 158 | { name: PLUGIN_NAME, before: "TemplatedPathPlugin" }, |
| 159 | (path, data) => { |
| 160 | if (data.filename && this.modules[data.filename]) { |
| 161 | /** |
| 162 | * Returns safe path. |
| 163 | * @param {string} str path |
| 164 | * @returns {string} safe path |
| 165 | */ |
| 166 | const toSafePath = (str) => |
| 167 | `__${str |
| 168 | .replace(/:/g, "__") |
| 169 | .replace(/^[^a-z0-9]+|[^a-z0-9]+$/gi, "") |
| 170 | .replace(/[^a-z0-9._-]+/gi, "_")}`; |
| 171 | |
| 172 | // filename: virtual:logo.svg -> __virtual__logo.svg |
| 173 | data.filename = toSafePath(data.filename); |
| 174 | } |
| 175 | return path; |
| 176 | } |
| 177 | ); |
| 178 | |
| 179 | normalModuleFactory.hooks.resolveForScheme |
| 180 | .for(scheme) |
| 181 | .tap(PLUGIN_NAME, (resourceData) => { |
| 182 | const virtualConfig = this.findVirtualModuleConfigById( |
| 183 | resourceData.resource |
| 184 | ); |
| 185 | const url = cachedParseResourceWithoutFragment( |
| 186 | resourceData.resource |
| 187 | ); |
| 188 | const path = url.path; |
| 189 | const type = virtualConfig.type || ""; |
| 190 | const context = virtualConfig.context || this.context; |
| 191 |
nothing calls this directly
no test coverage detected