UNPKG

1.85 kBJavaScriptView Raw
1const path = require("path");
2const fs = require("fs");
3const { hackWrapLoaders } = require("./utils");
4
5let id = 0;
6
7const NS = path.dirname(fs.realpathSync(__filename));
8
9const getLoaderName = (path) => {
10 const standardPath = path.replace(/\\/g, "/");
11 const nodeModuleName = /\/node_modules\/([^\/]+)/.exec(standardPath);
12 return (nodeModuleName && nodeModuleName[1]) || "";
13};
14
15module.exports.pitch = function () {
16 const callback = this[NS];
17 const module = this.resourcePath;
18 const loaderPaths = this.loaders
19 .map((l) => l.path)
20 .filter((l) => !l.includes("speed-measure-webpack-plugin"));
21
22 // Hack ourselves to overwrite the `require` method so we can override the
23 // loadLoaders
24 hackWrapLoaders(loaderPaths, (loader, path) => {
25 const loaderName = getLoaderName(path);
26 const wrapFunc = (func) =>
27 function () {
28 const loaderId = id++;
29 const almostThis = Object.assign({}, this, {
30 async: function () {
31 const asyncCallback = this.async.apply(this, arguments);
32
33 return function () {
34 callback({
35 id: loaderId,
36 type: "end",
37 });
38 return asyncCallback.apply(this, arguments);
39 };
40 }.bind(this),
41 });
42
43 callback({
44 module,
45 loaderName,
46 id: loaderId,
47 type: "start",
48 });
49 const ret = func.apply(almostThis, arguments);
50 callback({
51 id: loaderId,
52 type: "end",
53 });
54 return ret;
55 };
56
57 if (loader.normal) loader.normal = wrapFunc(loader.normal);
58 if (loader.default) loader.default = wrapFunc(loader.default);
59 if (loader.pitch) loader.pitch = wrapFunc(loader.pitch);
60 if (typeof loader === "function") return wrapFunc(loader);
61 return loader;
62 });
63};