2020-02-25 04:34:09 +01:00
|
|
|
// This plugin exposes a version of require() to the browser console to assist
|
|
|
|
// debugging. It also exposes the list of modules it knows about as the keys
|
|
|
|
// of the require.ids object.
|
|
|
|
|
|
|
|
import path from "path";
|
|
|
|
|
2021-03-18 01:32:29 +01:00
|
|
|
import type {ResolveRequest} from "enhanced-resolve";
|
|
|
|
import type {Chunk, Compiler, WebpackPluginInstance} from "webpack";
|
|
|
|
import {NormalModule, Template} from "webpack";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2021-03-18 01:32:29 +01:00
|
|
|
export default class DebugRequirePlugin implements WebpackPluginInstance {
|
|
|
|
apply(compiler: Compiler): void {
|
|
|
|
const resolved = new Map<string, Set<string>>();
|
2020-02-25 04:34:09 +01:00
|
|
|
const nameSymbol = Symbol("DebugRequirePluginName");
|
2021-03-18 01:32:29 +01:00
|
|
|
type NamedRequest = ResolveRequest & {
|
|
|
|
[nameSymbol]?: string;
|
|
|
|
};
|
|
|
|
let debugRequirePath: string | false = false;
|
2020-02-25 04:34:09 +01:00
|
|
|
|
2021-03-18 01:32:29 +01:00
|
|
|
compiler.resolverFactory.hooks.resolver
|
2020-02-25 04:34:09 +01:00
|
|
|
.for("normal")
|
2021-03-18 01:32:29 +01:00
|
|
|
.tap("DebugRequirePlugin", (resolver) => {
|
|
|
|
resolver.getHook("beforeRawModule").tap("DebugRequirePlugin", (req) => {
|
|
|
|
if (!(nameSymbol in req)) {
|
|
|
|
(req as NamedRequest)[nameSymbol] = req.request;
|
2020-02-25 04:34:09 +01:00
|
|
|
}
|
2021-03-18 01:32:29 +01:00
|
|
|
return undefined!;
|
2020-02-25 04:34:09 +01:00
|
|
|
});
|
|
|
|
|
2021-03-18 01:32:29 +01:00
|
|
|
resolver.getHook("beforeRelative").tap("DebugRequirePlugin", (req) => {
|
|
|
|
if (req.path !== false) {
|
|
|
|
const inPath = path.relative(compiler.context, req.path);
|
|
|
|
if (!inPath.startsWith("../") && !(nameSymbol in req)) {
|
|
|
|
(req as NamedRequest)[nameSymbol] = "./" + inPath;
|
2020-02-25 04:34:09 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-18 01:32:29 +01:00
|
|
|
return undefined!;
|
2020-02-25 04:34:09 +01:00
|
|
|
});
|
2021-03-18 01:32:29 +01:00
|
|
|
|
|
|
|
resolver
|
|
|
|
.getHook("beforeResolved")
|
|
|
|
.tap("DebugRequirePlugin", (req: ResolveRequest) => {
|
|
|
|
const name = (req as NamedRequest)[nameSymbol];
|
|
|
|
if (name !== undefined && req.path !== false) {
|
|
|
|
const names = resolved.get(req.path);
|
|
|
|
if (names) {
|
|
|
|
names.add(name);
|
|
|
|
} else {
|
|
|
|
resolved.set(req.path, new Set([name]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined!;
|
|
|
|
});
|
2020-02-25 04:34:09 +01:00
|
|
|
});
|
|
|
|
|
2020-02-25 08:18:50 +01:00
|
|
|
compiler.hooks.beforeCompile.tapPromise(
|
|
|
|
"DebugRequirePlugin",
|
2021-03-18 01:32:29 +01:00
|
|
|
async ({normalModuleFactory}) => {
|
2020-02-25 08:18:50 +01:00
|
|
|
const resolver = normalModuleFactory.getResolver("normal");
|
2021-03-18 01:32:29 +01:00
|
|
|
debugRequirePath = await new Promise((resolve) =>
|
2020-02-25 08:18:50 +01:00
|
|
|
resolver.resolve(
|
|
|
|
{},
|
|
|
|
__dirname,
|
2020-07-29 03:03:53 +02:00
|
|
|
"./debug-require",
|
2020-02-25 08:18:50 +01:00
|
|
|
{},
|
2021-03-18 01:32:29 +01:00
|
|
|
(err?: Error | null, result?: string | false) =>
|
|
|
|
resolve(err ? false : result!),
|
2020-07-02 02:16:03 +02:00
|
|
|
),
|
2020-02-25 08:18:50 +01:00
|
|
|
);
|
2020-07-02 02:16:03 +02:00
|
|
|
},
|
2020-02-25 08:18:50 +01:00
|
|
|
);
|
|
|
|
|
2021-03-18 01:32:29 +01:00
|
|
|
compiler.hooks.compilation.tap("DebugRequirePlugin", (compilation) => {
|
|
|
|
compilation.mainTemplate.hooks.bootstrap.tap(
|
2020-02-25 04:34:09 +01:00
|
|
|
"DebugRequirePlugin",
|
2021-03-18 01:32:29 +01:00
|
|
|
(source: string, chunk: Chunk) => {
|
2020-02-25 04:34:09 +01:00
|
|
|
const ids: [string, string | number][] = [];
|
2021-03-18 01:32:29 +01:00
|
|
|
let hasDebugRequire = false;
|
|
|
|
compilation.chunkGraph.hasModuleInGraph(
|
|
|
|
chunk,
|
|
|
|
(m) => {
|
|
|
|
if (m instanceof NormalModule) {
|
|
|
|
const id = compilation.chunkGraph.getModuleId(m);
|
|
|
|
if (m.resource === debugRequirePath) {
|
|
|
|
hasDebugRequire = true;
|
|
|
|
}
|
|
|
|
for (const name of resolved.get(m.resource) ?? []) {
|
|
|
|
ids.push([
|
|
|
|
m.rawRequest.slice(0, m.rawRequest.lastIndexOf("!") + 1) +
|
|
|
|
name,
|
|
|
|
id,
|
|
|
|
]);
|
|
|
|
}
|
2020-02-25 04:34:09 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2020-07-02 02:16:03 +02:00
|
|
|
() => true,
|
2020-02-25 04:34:09 +01:00
|
|
|
);
|
|
|
|
|
2021-03-18 01:32:29 +01:00
|
|
|
if (!hasDebugRequire) {
|
2020-02-25 08:18:50 +01:00
|
|
|
return source;
|
|
|
|
}
|
2020-02-25 04:34:09 +01:00
|
|
|
|
2020-02-25 08:18:50 +01:00
|
|
|
ids.sort();
|
2020-02-25 04:34:09 +01:00
|
|
|
return Template.asString([
|
|
|
|
source,
|
2021-03-18 01:32:29 +01:00
|
|
|
`__webpack_require__.debugRequireIds = ${JSON.stringify(
|
2020-07-29 03:23:47 +02:00
|
|
|
Object.fromEntries(ids),
|
|
|
|
null,
|
|
|
|
"\t",
|
2021-03-18 01:32:29 +01:00
|
|
|
)};`,
|
2020-02-25 04:34:09 +01:00
|
|
|
]);
|
2020-07-02 02:16:03 +02:00
|
|
|
},
|
2020-02-25 04:34:09 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|