-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveTestIdPlugin.ts
More file actions
41 lines (33 loc) · 1.19 KB
/
removeTestIdPlugin.ts
File metadata and controls
41 lines (33 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Compiler, NormalModule } from 'webpack';
export class removeTestIdPlugin {
apply(compiler: Compiler) {
compiler.hooks.compilation.tap(removeTestIdPlugin.name, (compilation, compilationParams) => {
const isWebpackV5 = compiler.webpack && compiler.webpack.version >= '5';
if (!isWebpackV5) {
throw new Error('only support webpack@5');
}
const modifiedModules: string[] = []
NormalModule.getCompilationHooks(compilation).beforeLoaders.tap(removeTestIdPlugin.name, (_,normalModule) => {
const { userRequest = '' } = normalModule
// safety guard
const startIndex =
userRequest.lastIndexOf('!') === -1
? 0
: userRequest.lastIndexOf('!') + 1;
const moduleRequest = userRequest
.slice(startIndex)
.replace(/\\/g, '/');
if (modifiedModules.includes(moduleRequest)) {
return;
}
const isTsx = /\.(t|j)sx$/.test(moduleRequest);
if (isTsx) {
normalModule.loaders.push({
loader: require.resolve('./removeTestIdLoader')
} as any)
modifiedModules.push(moduleRequest)
}
})
});
}
};