-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
406 lines (336 loc) · 14.1 KB
/
index.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
* Created by Paul on 2017-07-02.
*/
const fs = require("fs");
const path = require("path");
const util = require("util");
const { builtinModules } = require("module");
const { Compilation, sources } = require("webpack");
let debugMode = false;
const pluginName = "GeneratePackageJsonPlugin";
const pluginPrefix = "( Generate Package Json Webpack Plugin ): ";
function GeneratePackageJsonPlugin(
basePackage = {
name: "",
version: "0.0.1",
}, {
debug = false,
extraSourcePackageFilenames = [],
sourcePackageFilenames = [],
additionalDependencies = {},
useInstalledVersions = true,
resolveContextPaths,
forceWebpackVersion,
excludeDependencies = [],
} = {}
) {
if (debug) {
console.log(`GeneratePackageJsonPlugin: Debugging mode activated!`);
debugMode = true;
}
/*
TODO for future - accept a string and load the base.package.json directly
if (typeof otherPackageValues === "string") {
otherPackageValues = JSON.parse(fs.readFileSync(path.join(__dirname, filename)).toString());
}*/
const sourcePackagesDependencyVersionMap = {};
const allSourcePackageFilenames = [...extraSourcePackageFilenames, ...sourcePackageFilenames];
if (allSourcePackageFilenames.length > 0) {
logIfDebug(`GPJWP: Received ${allSourcePackageFilenames.length} extra package.json file${allSourcePackageFilenames.length > 1 ? "s" : ""} from which to source versions (later takes preference):`);
allSourcePackageFilenames.reverse();
let fileIndex = 1;
for (const filename of allSourcePackageFilenames) {
logIfDebug(`${fileIndex++} : ${filename}`);
const extraSourcePackage = JSON.parse(fs.readFileSync(filename).toString());
Object.assign(sourcePackagesDependencyVersionMap, extraSourcePackage.dependencies ? extraSourcePackage.dependencies : {});
}
}
logIfDebug(`GPJWP: Final map of dependency versions to be used if encountered in bundle:\n`, sourcePackagesDependencyVersionMap);
Object.assign(this, {
basePackage,
dependencyVersionMap: sourcePackagesDependencyVersionMap,
additionalDependencies,
useInstalledVersions,
resolveContextPaths,
forceWebpackVersion,
excludeDependencies,
});
}
function logIfDebug(something, object = "") {
if (debugMode) {
if (object) {
console.log(something, util.inspect(object, { depth: 3 }));
} else {
console.log(something);
}
}
}
const nodeModulesPart = "node_modules";
function getNameFromPortableId(raw) {
if (raw.indexOf("javascript\/esm") >= 0) {
const nodeModulesLastIndex = raw.lastIndexOf(nodeModulesPart);
if (nodeModulesLastIndex >= 0) {
const mainModulePart = raw.slice(nodeModulesLastIndex + nodeModulesPart.length + 1);
const firstSlashIndex = mainModulePart.indexOf(path.sep);
if (mainModulePart.startsWith("@")) {
const secondSlashIndex = mainModulePart.indexOf(path.sep, firstSlashIndex + 1);
if (secondSlashIndex >= 0) {
return `${mainModulePart.slice(0, firstSlashIndex)}/${mainModulePart.slice(firstSlashIndex + 1, secondSlashIndex)}`;
} else {
return `${mainModulePart.slice(0, firstSlashIndex)}/${mainModulePart.slice(firstSlashIndex + 1)}`;
}
} else {
if (firstSlashIndex >= 0) {
return mainModulePart.slice(0, firstSlashIndex);
} else {
return mainModulePart;
}
}
} else {
return "";
}
}
let cut = raw.substring(raw.indexOf("\"") + 1, raw.lastIndexOf("\""));
let slashCount = (cut.match(/\//g) || []).length;
while ((cut.indexOf("@") === -1 && slashCount > 0) || slashCount > 1) {
cut = cut.substring(0, cut.lastIndexOf("\/"));
slashCount -= 1;
}
return cut;
}
const searchErrorNoExportsPart = `No "exports" main defined in `;
const packageJsonPart = "package.json";
function resolveModuleBasePath(moduleName, options = undefined) {
let moduleMainFilePath;
try {
moduleMainFilePath = require.resolve(moduleName, options);
} catch (e) {
const message = e.message ? e.message : "";
const indexOfMessage = message.lastIndexOf(searchErrorNoExportsPart);
const indexOfPackage = message.lastIndexOf("package.json");
if (indexOfMessage >= 0 && indexOfPackage >= 0) {
moduleMainFilePath = e.message.slice(
indexOfMessage + searchErrorNoExportsPart.length,
indexOfPackage + packageJsonPart.length,
);
} else {
throw e;
}
}
const moduleNameParts = moduleName.split("/");
let searchForPathSection;
if (moduleName.startsWith("@") && moduleNameParts.length > 1) {
const [org, mod] = moduleNameParts;
searchForPathSection = `node_modules${path.sep}${org}${path.sep}${mod}`;
} else {
const [mod] = moduleNameParts;
searchForPathSection = `node_modules${path.sep}${mod}`;
}
const lastIndex = moduleMainFilePath.lastIndexOf(searchForPathSection);
if (lastIndex === -1) {
throw new Error(`Couldn't resolve the base path of "${moduleName}". Searched inside the resolved main file path "${moduleMainFilePath}" using "${searchForPathSection}"`);
}
return moduleMainFilePath.slice(0, lastIndex + searchForPathSection.length);
}
GeneratePackageJsonPlugin.prototype.apply = function (compiler) {
// const isWebpack5 = require("webpack").version.split(".")[0] >= 5;
const isWebpack5 = this.forceWebpackVersion != null ? (this.forceWebpackVersion === "webpack5") : (require("webpack").version.split(".")[0] >= 5);
const computePackageJson = (compilation) => {
const dependencyTypes = ["dependencies", "devDependencies", "peerDependencies"]
const modules = Object.assign({}, this.additionalDependencies);
const getInstalledVersionForModuleName = (moduleName, context) => {
let modulePackageFile;
let resolveFile = path.join(moduleName, "./package.json");
try {
const moduleBasePath = resolveModuleBasePath(moduleName, context ? {
paths: [context],
} : this.resolveContextPaths ? {
paths: this.resolveContextPaths,
} : undefined);
/*console.log(`Found module base path: ${moduleBasePath}`);
modulePackageFile = require.resolve(resolveFile, context ? {
paths: [context],
} : this.resolveContextPaths ? {
paths: this.resolveContextPaths,
} : undefined);
console.log(`Found module package.json file: ${modulePackageFile}`);*/
modulePackageFile = path.join(moduleBasePath, "./package.json");
} catch (e) {
// logIfDebug(`GPJWP: Ignoring module without a found package.json: ${moduleName} ("${resolveFile}" couldn't resolve): ${e.message}`);
console.error(`GPJWP: Ignoring module without a found package.json: ${moduleName} ("${resolveFile}" couldn't resolve): ${e.message}`);
return undefined;
}
let version;
try {
version = JSON.parse(fs.readFileSync(modulePackageFile).toString()).version;
} catch (e) {
throw new Error(`Can't parse package.json file: ${modulePackageFile}`);
}
if (!version) {
throw new Error(`Missing package.json version: ${modulePackageFile}`);
}
return version;
}
// const isWebpack5 = Number(wpVersion.split(".")[0]) >= 5;
const processModule = (module) => {
const portableId = module.portableId ? module.portableId : module.identifier();
if (portableId.indexOf("external ") === -1) {
logIfDebug(`GPJWP: Found module: ${portableId}`);
return;
}
logIfDebug(`GPJWP: Found external module: ${portableId}`);
const moduleName = getNameFromPortableId(portableId);
if (moduleName === '.' || moduleName === '..') {
logIfDebug(`GPJP: excluded "${portableId}" because it is on a relative path`);
return;
}
if (this.excludeDependencies.includes(moduleName)) {
logIfDebug(`GPJWP: excluded "${moduleName}" from generated package.json`);
return;
}
if (moduleName.length === 0) {
console.error(`GPJWP: Couldn't decipher the module name from external module input: ${portableId} - will be ignored in final output.`);
return;
}
if (builtinModules.indexOf(moduleName) !== -1) {
logIfDebug(`GPJWP: Native node.js module detected: ${portableId}`);
return;
}
if (!this.useInstalledVersions) {
const dependencyVersion = this.dependencyVersionMap[moduleName];
if (dependencyVersion) {
modules[moduleName] = dependencyVersion;
}
return;
}
const moduleIssuer = isWebpack5
? compilation.moduleGraph.getIssuer(module)
: module.issuer;
const context = moduleIssuer && moduleIssuer.context;
const moduleVersion = getInstalledVersionForModuleName(moduleName, context);
if (moduleVersion == null) {
console.error(`GPJWP: Couldn't resolve a version for module "${moduleName}" (from portable ID: ${portableId}) - will be ignored in final output.`)
} else {
modules[moduleName] = moduleVersion;
}
};
if (isWebpack5) {
for (const module of compilation.modules) {
processModule(module);
}
} else {
compilation.chunks.forEach((chunk) => {
if (typeof chunk.modulesIterable !== "undefined") { // webpack 4
for (const module of chunk.modulesIterable) {
processModule(module);
}
} else if (typeof chunk.forEachModule !== "undefined") { // webpack 3
chunk.forEachModule((module) => {
processModule(module);
});
} else { // webpack 2
for (let i = 0; i < chunk.modules.length; i += 1) {
const module = chunk.modules[i];
processModule(module);
}
}
});
}
const basePackageValues = Object.assign({}, this.basePackage);
for (const dependencyType of dependencyTypes) {
// Overwrite modules or set new module dependencies for those that have been
// deliberately set in " basePackageValues.[dependencyType] "
// This mechanism ensures that dependencies declared in the basePackageValues
// take precedence over the found dependencies
if (basePackageValues && basePackageValues[dependencyType]) {
const nonWebpackModuleNames = Object.keys(basePackageValues[dependencyType]);
for (let k = 0; k < nonWebpackModuleNames.length; k += 1) {
const moduleName = nonWebpackModuleNames[k];
let useVersionMap = !this.useInstalledVersions;
if (basePackageValues[dependencyType] && basePackageValues[dependencyType][moduleName] && basePackageValues[dependencyType][moduleName].length > 0) {
logIfDebug(`GPJWP: Adding deliberate module in "${dependencyType}" with version set deliberately: ${moduleName} -> ${basePackageValues[dependencyType][moduleName]}`);
if (dependencyType === "dependencies") {
modules[moduleName] = basePackageValues[dependencyType][moduleName];
}
} else if (this.useInstalledVersions) {
const version = getInstalledVersionForModuleName(moduleName);
if (version != null) {
if (dependencyType !== "dependencies") {
basePackageValues[dependencyType][moduleName] = version;
delete modules[moduleName];
} else {
modules[moduleName] = version;
}
} else {
console.warn(`${pluginPrefix}Couldn't find installed version for module "${moduleName}" - falling back to extra source package version map (if provided)`);
useVersionMap = true;
}
}
if (useVersionMap) {
if (this.dependencyVersionMap[moduleName] != null) {
logIfDebug(`GPJWP: Adding deliberate module in "${dependencyType}" with version found in sources: ${moduleName} -> ${this.dependencyVersionMap[moduleName]}`);
if (dependencyType !== "dependencies") {
basePackageValues[dependencyType][moduleName] = this.dependencyVersionMap[moduleName];
delete modules[moduleName];
} else {
modules[moduleName] = this.dependencyVersionMap[moduleName];
}
} else {
console.warn(`${pluginPrefix}You have set a module in "${dependencyType}" to be included deliberately with name: "${moduleName}" - but there is no version specified in any source files, or found to be installed (if you used the option useInstalledVersions)!`);
}
}
}
}
}
logIfDebug(`GPJWP: Modules to be used in generated package.json`, modules);
const finalPackageValues = Object.assign({}, basePackageValues, { dependencies: orderKeys(modules) });
return JSON.stringify(finalPackageValues, this.replacer ? this.replacer : null, this.space ? this.space : 2);
};
const emitPackageJsonOld = (compilation, callback) => { // webpack 2-4
const json = computePackageJson(compilation);
compilation.assets['package.json'] = {
source: function () {
return json;
},
size: function () {
return json.length;
}
};
callback();
};
const emitPackageJson = (compilation) => {
const json = computePackageJson(compilation);
compilation.emitAsset('package.json', new sources.RawSource(json));
};
if (isWebpack5) {
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
compilation.hooks.processAssets.tap(
{ name: pluginName, stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL },
() => emitPackageJson(compilation)
);
});
} else {
if (typeof compiler.hooks !== "undefined") { // webpack 4
compiler.hooks.emit.tapAsync(pluginName, emitPackageJsonOld);
} else { // webpack 2-3
compiler.plugin('emit', emitPackageJsonOld);
}
}
};
function orderKeys(obj) {
const keys = Object.keys(obj).sort(function keyOrder(k1, k2) {
if (k1 < k2) return -1;
else if (k1 > k2) return +1;
else return 0;
});
let i, after = {};
for (i = 0; i < keys.length; i++) {
after[keys[i]] = obj[keys[i]];
delete obj[keys[i]];
}
for (i = 0; i < keys.length; i++) {
obj[keys[i]] = after[keys[i]];
}
return obj;
}
module.exports = GeneratePackageJsonPlugin;