-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathModifySourcePlugin.ts
More file actions
157 lines (133 loc) · 4.4 KB
/
Copy pathModifySourcePlugin.ts
File metadata and controls
157 lines (133 loc) · 4.4 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import crypto from 'crypto';
import { validate } from 'schema-utils';
import type { Schema } from 'schema-utils/declarations/validate';
import type { Compiler, NormalModule } from 'webpack';
import { getPluginLoaderPath } from './getPluginLoaderPath';
import { isPluginWebpackLoader } from './isPluginWebpackLoader';
import { NormalModuleLoader } from './NormalModuleLoader';
import { AbstractOperation } from './operations';
import { PluginWebpackLoaderOptions } from './PluginWebpackLoaderOptions';
export interface Rule {
test: RegExp | ((module: NormalModule) => boolean);
operations?: AbstractOperation[];
}
export type Options = {
debug?: boolean;
rules: Rule[];
constants?: Record<string, string | number>;
};
const validationSchema: Schema = {
type: 'object',
additionalProperties: false,
properties: {
rules: {
type: 'array',
items: {
type: 'object',
additionalProperties: false,
properties: {
test: {
anyOf: [{ instanceof: 'Function' }, { instanceof: 'RegExp' }]
},
operations: {
type: 'array',
items: {
type: 'object'
}
}
}
}
},
constants: {
type: 'object'
},
debug: {
type: 'boolean'
}
}
};
const PLUGIN_NAME = 'ModifySourcePlugin';
function hashModuleOptions(options: Record<string, any>): string {
return crypto.createHash('md5').update(JSON.stringify(options)).digest('hex');
}
export class ModifySourcePlugin {
constructor(protected readonly options: Options) {
validate(validationSchema, options, {
name: PLUGIN_NAME
});
}
public apply(compiler: Compiler): void {
const { rules, debug, constants = {} } = this.options;
const appliedOperationHashes: string[] = [];
compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
const tapCallback = (_: any, normalModule: NormalModule) => {
const userRequest = normalModule.userRequest || '';
const startIndex =
userRequest.lastIndexOf('!') === -1
? 0
: userRequest.lastIndexOf('!') + 1;
const moduleRequest = userRequest
.substring(startIndex)
.replace(/\\/g, '/');
const moduleLoaders = normalModule.loaders as NormalModuleLoader[];
rules.forEach(ruleOptions => {
const isRuleAlreadyApplied = moduleLoaders.some(moduleLoader => {
if (isPluginWebpackLoader(moduleLoader)) {
const loaderOptions =
typeof moduleLoader.options === 'string'
? (JSON.parse(
moduleLoader.options
) as PluginWebpackLoaderOptions)
: moduleLoader.options;
return appliedOperationHashes.includes(
hashModuleOptions(loaderOptions)
);
}
});
if (isRuleAlreadyApplied) {
return;
}
const test = ruleOptions.test;
const isMatched = (() => {
if (typeof test === 'function' && test(normalModule)) {
return true;
}
return test instanceof RegExp && test.test(moduleRequest);
})();
if (isMatched) {
const serializableOperations = ruleOptions.operations?.map(op =>
op.toSerializable()
);
const loader: NormalModuleLoader = {
loader: getPluginLoaderPath(),
options: {
moduleRequest,
operations: serializableOperations,
constants
}
};
moduleLoaders.push(loader);
appliedOperationHashes.push(hashModuleOptions(loader.options));
if (debug) {
// eslint-disable-next-line no-console
console.log(
`\n[${PLUGIN_NAME}] Use loader for "${moduleRequest}".`
);
}
}
});
};
const NormalModule = compiler.webpack?.NormalModule;
const isNormalModuleAvailable =
Boolean(NormalModule) && Boolean(NormalModule.getCompilationHooks);
if (isNormalModuleAvailable) {
NormalModule.getCompilationHooks(compilation).beforeLoaders.tap(
PLUGIN_NAME,
tapCallback
);
} else {
compilation.hooks.normalModuleLoader.tap(PLUGIN_NAME, tapCallback);
}
});
}
}