1 | const path = require("path");
|
2 | const fs = require("fs");
|
3 | const { hackWrapLoaders } = require("./utils");
|
4 |
|
5 | let id = 0;
|
6 |
|
7 | const NS = path.dirname(fs.realpathSync(__filename));
|
8 |
|
9 | const getLoaderName = (path) => {
|
10 | const standardPath = path.replace(/\\/g, "/");
|
11 | const nodeModuleName = /\/node_modules\/([^\/]+)/.exec(standardPath);
|
12 | return (nodeModuleName && nodeModuleName[1]) || "";
|
13 | };
|
14 |
|
15 | module.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 |
|
23 |
|
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 | };
|