UNPKG

1.33 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { URL, fileURLToPath } = require("url");
9const { NormalModule } = require("..");
10
11/** @typedef {import("../Compiler")} Compiler */
12
13class FileUriPlugin {
14 /**
15 * Apply the plugin
16 * @param {Compiler} compiler the compiler instance
17 * @returns {void}
18 */
19 apply(compiler) {
20 compiler.hooks.compilation.tap(
21 "FileUriPlugin",
22 (compilation, { normalModuleFactory }) => {
23 normalModuleFactory.hooks.resolveForScheme
24 .for("file")
25 .tap("FileUriPlugin", resourceData => {
26 const url = new URL(resourceData.resource);
27 const path = fileURLToPath(url);
28 const query = url.search;
29 const fragment = url.hash;
30 resourceData.path = path;
31 resourceData.query = query;
32 resourceData.fragment = fragment;
33 resourceData.resource = path + query + fragment;
34 return true;
35 });
36 const hooks = NormalModule.getCompilationHooks(compilation);
37 hooks.readResource
38 .for(undefined)
39 .tapAsync("FileUriPlugin", (loaderContext, callback) => {
40 const { resourcePath } = loaderContext;
41 loaderContext.addDependency(resourcePath);
42 loaderContext.fs.readFile(resourcePath, callback);
43 });
44 }
45 );
46 }
47}
48
49module.exports = FileUriPlugin;