"use strict";exports.__esModule=true;exports.default=exports.ReactSyncScriptsConformanceCheck=exports.MinificationConformanceCheck=exports.GranularChunksConformanceCheck=exports.DuplicatePolyfillsConformanceCheck=void 0;var _recast=require("next/dist/compiled/recast");var _TestInterface=require("./TestInterface");var _duplicatePolyfillsConformanceCheck=require("./checks/duplicate-polyfills-conformance-check");exports.DuplicatePolyfillsConformanceCheck=_duplicatePolyfillsConformanceCheck.DuplicatePolyfillsConformanceCheck;var _granularChunksConformance=require("./checks/granular-chunks-conformance");exports.GranularChunksConformanceCheck=_granularChunksConformance.GranularChunksConformanceCheck;var _minificationConformanceCheck=require("./checks/minification-conformance-check");exports.MinificationConformanceCheck=_minificationConformanceCheck.MinificationConformanceCheck;var _reactSyncScriptsConformanceCheck=require("./checks/react-sync-scripts-conformance-check");exports.ReactSyncScriptsConformanceCheck=_reactSyncScriptsConformanceCheck.ReactSyncScriptsConformanceCheck;// eslint-disable-next-line import/no-extraneous-dependencies
class WebpackConformancePlugin{constructor(options){this.tests=void 0;this.errors=void 0;this.warnings=void 0;this.compiler=void 0;this.buildStartedHandler=(_compilation,callback)=>{const buildStartedResults=this.tests.map(test=>{if(test.buildStared&&this.compiler){return test.buildStared(this.compiler.options);}return{result:_TestInterface.IConformanceTestStatus.SUCCESS};});this.gatherResults(buildStartedResults);callback();};this.buildCompletedHandler=(compilation,cb)=>{const buildCompletedResults=this.tests.map(test=>{if(test.buildCompleted){return test.buildCompleted(compilation.assets);}return{result:_TestInterface.IConformanceTestStatus.SUCCESS};});this.gatherResults(buildCompletedResults);compilation.errors.push(...this.errors);compilation.warnings.push(...this.warnings);cb();};this.parserHandler=factory=>{const JS_TYPES=['auto','esm','dynamic'];const collectedVisitors=new Map();// Collect all interested visitors from all tests.
this.tests.forEach(test=>{if(test.getAstNode){const getAstNodeCallbacks=test.getAstNode();getAstNodeCallbacks.forEach(result=>{if(!collectedVisitors.has(result.visitor)){collectedVisitors.set(result.visitor,[]);};collectedVisitors.get(result.visitor).push(result.inspectNode);});}});// Do an extra walk per module and add interested visitors to the walk.
for(const type of JS_TYPES){factory.hooks.parser.for('javascript/'+type).tap(this.constructor.name,parser=>{parser.hooks.program.tap(this.constructor.name,ast=>{const visitors={};const that=this;for(const visitorKey of collectedVisitors.keys()){visitors[visitorKey]=function(path){const callbacks=collectedVisitors.get(visitorKey)||[];callbacks.forEach(cb=>{if(!cb){return;}const{request}=parser.state.module;const outcome=cb(path,{request});that.gatherResults([outcome]);});this.traverse(path);return false;};}(0,_recast.visit)(ast,visitors);});});}};this.tests=[];if(options.tests){this.tests.push(...options.tests);}this.errors=[];this.warnings=[];}gatherResults(results){results.forEach(result=>{if(result.result===_TestInterface.IConformanceTestStatus.FAILED){result.errors&&this.errors.push(...result.errors);result.warnings&&this.warnings.push(...result.warnings);}});}apply(compiler){this.compiler=compiler;compiler.hooks.make.tapAsync(this.constructor.name,this.buildStartedHandler);compiler.hooks.emit.tapAsync(this.constructor.name,this.buildCompletedHandler);compiler.hooks.normalModuleFactory.tap(this.constructor.name,this.parserHandler);}}exports.default=WebpackConformancePlugin;
//# sourceMappingURL=index.js.map