UNPKG

4.48 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.PluginChunkReadHandler = void 0;
4var WebpackChunkModuleIterator_1 = require("./WebpackChunkModuleIterator");
5var WebpackInnerModuleIterator_1 = require("./WebpackInnerModuleIterator");
6var PluginChunkReadHandler = /** @class */ (function () {
7 function PluginChunkReadHandler(logger, fileHandler, licenseTypeIdentifier, licenseTextReader, licensePolicy, fileSystem) {
8 this.logger = logger;
9 this.fileHandler = fileHandler;
10 this.licenseTypeIdentifier = licenseTypeIdentifier;
11 this.licenseTextReader = licenseTextReader;
12 this.licensePolicy = licensePolicy;
13 this.fileSystem = fileSystem;
14 this.moduleIterator = new WebpackChunkModuleIterator_1.WebpackChunkModuleIterator();
15 this.innerModuleIterator = new WebpackInnerModuleIterator_1.WebpackInnerModuleIterator(require.resolve);
16 }
17 PluginChunkReadHandler.prototype.processChunk = function (compilation, chunk, moduleCache, stats) {
18 var _this = this;
19 this.moduleIterator.iterateModules(compilation, chunk, stats, function (chunkModule) {
20 _this.innerModuleIterator.iterateModules(chunkModule, function (module) {
21 var identifiedModule = _this.extractIdentifiedModule(module) ||
22 _this.fileHandler.getModule(module.resource);
23 if (identifiedModule) {
24 _this.processModule(compilation, chunk, moduleCache, identifiedModule);
25 }
26 });
27 });
28 };
29 PluginChunkReadHandler.prototype.extractIdentifiedModule = function (module) {
30 var resolved = module.resourceResolveData;
31 if (!resolved)
32 return undefined;
33 var directory = resolved.descriptionFileRoot, packageJson = resolved.descriptionFileData;
34 if (
35 // if missing data, fall back to fs module hunting
36 directory &&
37 packageJson &&
38 packageJson.name &&
39 // user checks to decide if we should include the module
40 this.fileHandler.isInModuleDirectory(directory) &&
41 !this.fileHandler.isBuildRoot(directory) &&
42 !this.fileHandler.excludedPackageTest(packageJson.name)) {
43 return {
44 directory: directory,
45 packageJson: packageJson,
46 name: packageJson.name
47 };
48 }
49 return undefined;
50 };
51 PluginChunkReadHandler.prototype.getPackageJson = function (directory) {
52 var filename = "" + directory + this.fileSystem.pathSeparator + "package.json";
53 return JSON.parse(this.fileSystem.readFileAsUtf8(filename));
54 };
55 PluginChunkReadHandler.prototype.processModule = function (compilation, chunk, moduleCache, module) {
56 var _a;
57 if (!moduleCache.alreadySeenForChunk(chunk.name, module.name)) {
58 var alreadyIncludedModule = moduleCache.getModule(module.name);
59 if (alreadyIncludedModule !== null) {
60 moduleCache.registerModule(chunk.name, alreadyIncludedModule);
61 }
62 else {
63 // module not yet in cache
64 var packageJson = (_a = module.packageJson) !== null && _a !== void 0 ? _a : this.getPackageJson(module.directory);
65 var licenseType = this.licenseTypeIdentifier.findLicenseIdentifier(compilation, module.name, packageJson);
66 if (this.licensePolicy.isLicenseUnacceptableFor(licenseType)) {
67 this.logger.error(compilation, "unacceptable license found for " + module.name + ": " + licenseType);
68 this.licensePolicy.handleUnacceptableLicense(module.name, licenseType);
69 }
70 if (this.licensePolicy.isLicenseWrittenFor(licenseType)) {
71 var licenseText = this.licenseTextReader.readLicense(compilation, module, licenseType);
72 moduleCache.registerModule(chunk.name, {
73 licenseText: licenseText,
74 packageJson: packageJson,
75 name: module.name,
76 directory: module.directory,
77 licenseId: licenseType
78 });
79 }
80 }
81 moduleCache.markSeenForChunk(chunk.name, module.name);
82 }
83 };
84 return PluginChunkReadHandler;
85}());
86exports.PluginChunkReadHandler = PluginChunkReadHandler;