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 nodeModuleName = /\/node_modules\/([^\/]+)/.exec(path);
|
11 | return (nodeModuleName && nodeModuleName[1]) || "";
|
12 | };
|
13 |
|
14 | module.exports.pitch = function() {
|
15 | const callback = this[NS];
|
16 | const module = this.resourcePath;
|
17 | const loaderPaths = this.loaders
|
18 | .map(l => l.path)
|
19 | .filter(l => !l.includes("speed-measure-webpack-plugin"));
|
20 |
|
21 |
|
22 |
|
23 | hackWrapLoaders(loaderPaths, (loader, path) => {
|
24 | const loaderName = getLoaderName(path);
|
25 | const wrapFunc = func =>
|
26 | function() {
|
27 | const loaderId = id++;
|
28 | const almostThis = Object.assign({}, this, {
|
29 | async: function() {
|
30 | const asyncCallback = this.async.apply(this, arguments);
|
31 |
|
32 | return function() {
|
33 | callback({
|
34 | id: loaderId,
|
35 | type: "end",
|
36 | });
|
37 | return asyncCallback.apply(this, arguments);
|
38 | };
|
39 | }.bind(this)
|
40 | });
|
41 |
|
42 | callback({
|
43 | module,
|
44 | loaderName,
|
45 | id: loaderId,
|
46 | type: "start",
|
47 | });
|
48 | const ret = func.apply(almostThis, arguments);
|
49 | callback({
|
50 | id: loaderId,
|
51 | type: "end",
|
52 | });
|
53 | return ret;
|
54 | };
|
55 |
|
56 | if (loader.normal) loader.normal = wrapFunc(loader.normal);
|
57 | if (loader.default) loader.default = wrapFunc(loader.default);
|
58 | if (loader.pitch) loader.pitch = wrapFunc(loader.pitch);
|
59 | if (typeof loader === "function") return wrapFunc(loader);
|
60 | return loader;
|
61 | });
|
62 | };
|