1
-
2
- import { Linter , Plugin } from '@rsdoctor/types' ;
3
- import { debug } from '@rsdoctor/utils/logger' ;
1
+ import path from 'path' ;
2
+ import { RsdoctorPluginInstance } from '@/types' ;
3
+ import { ModuleGraph } from '@rsdoctor/graph' ;
4
+ import { Linter , Plugin , Constants } from '@rsdoctor/types' ;
4
5
import { Process } from '@rsdoctor/utils/build' ;
6
+ import { debug } from '@rsdoctor/utils/logger' ;
7
+ import fse from 'fs-extra' ;
5
8
import {
6
9
Chunks as ChunksBuildUtils ,
7
10
ModuleGraph as ModuleGraphBuildUtils ,
8
11
} from '../../build-utils/build' ;
9
- import {
10
- Chunks as ChunksUtils ,
11
- } from '../../build-utils/common' ;
12
- import fse from 'fs-extra' ;
12
+ import { Chunks as ChunksUtils } from '../../build-utils/common' ;
13
13
import { internalPluginTapPreOptions } from '../constants' ;
14
- import { RsdoctorPluginInstance } from '@/types' ;
15
- import { ModuleGraph } from '@rsdoctor/graph' ;
16
14
17
- /**
18
- * @description Generate ModuleGraph and ChunkGraph from stats and webpack module apis;
19
- * @param {Compiler } compiler
20
- * @return {* }
21
- * @memberof RsdoctorWebpackPlugin
22
- */
23
- export const ensureModulesChunksGraphFn = ( compiler : Plugin . BaseCompiler , _this : RsdoctorPluginInstance < Plugin . BaseCompiler , Linter . ExtendRuleData [ ] > ) => {
24
- if ( _this . _modulesGraphApplied ) return ;
25
- _this . _modulesGraphApplied = true ;
15
+ /**
16
+ * @description Generate ModuleGraph and ChunkGraph from stats and webpack module apis;
17
+ * @param {Compiler } compiler
18
+ * @return {* }
19
+ * @memberof RsdoctorWebpackPlugin
20
+ */
21
+ export const ensureModulesChunksGraphFn = (
22
+ compiler : Plugin . BaseCompiler ,
23
+ _this : RsdoctorPluginInstance < Plugin . BaseCompiler , Linter . ExtendRuleData [ ] > ,
24
+ ) => {
25
+ if ( _this . _modulesGraphApplied ) return ;
26
+ _this . _modulesGraphApplied = true ;
26
27
27
- const context : Required < ModuleGraphBuildUtils . TransformContext > = {
28
- astCache : new Map ( ) ,
29
- packagePathMap : new Map ( ) ,
30
- getSourceMap : ( file : string ) => {
31
- return _this . sdk . getSourceMap ( file ) ;
32
- } ,
33
- } ;
28
+ const context : Required < ModuleGraphBuildUtils . TransformContext > = {
29
+ astCache : new Map ( ) ,
30
+ packagePathMap : new Map ( ) ,
31
+ getSourceMap : ( file : string ) => {
32
+ return _this . sdk . getSourceMap ( file ) ;
33
+ } ,
34
+ } ;
34
35
35
- compiler . hooks . done . tapPromise (
36
- internalPluginTapPreOptions ( 'moduleGraph' ) ,
37
- async ( _stats ) => {
38
- const stats = _stats as Plugin . Stats ;
39
- const statsJson = stats . toJson ( ) ;
36
+ compiler . hooks . done . tapPromise (
37
+ internalPluginTapPreOptions ( 'moduleGraph' ) ,
38
+ async ( _stats ) => {
39
+ const stats = _stats as Plugin . Stats ;
40
+ const statsJson = stats . toJson ( ) ;
40
41
41
- debug ( Process . getMemoryUsageMessage , '[Before Generate ModuleGraph]' ) ;
42
+ debug ( Process . getMemoryUsageMessage , '[Before Generate ModuleGraph]' ) ;
42
43
43
- _this . chunkGraph = ChunksBuildUtils . chunkTransform ( new Map ( ) , statsJson ) ;
44
+ _this . chunkGraph = ChunksBuildUtils . chunkTransform ( new Map ( ) , statsJson ) ;
44
45
45
- /** generate module graph */
46
- _this . modulesGraph = await ModuleGraphBuildUtils . getModuleGraphByStats (
47
- stats . compilation ,
48
- statsJson ,
49
- process . cwd ( ) ,
50
- _this . chunkGraph ,
51
- _this . options . features ,
52
- context ,
53
- ) ;
46
+ /** generate module graph */
47
+ _this . modulesGraph = await ModuleGraphBuildUtils . getModuleGraphByStats (
48
+ stats . compilation ,
49
+ statsJson ,
50
+ process . cwd ( ) ,
51
+ _this . chunkGraph ,
52
+ _this . options . features ,
53
+ context ,
54
+ ) ;
54
55
55
- debug ( Process . getMemoryUsageMessage , '[After Generate ModuleGraph]' ) ;
56
+ debug ( Process . getMemoryUsageMessage , '[After Generate ModuleGraph]' ) ;
56
57
58
+ /** transform modules graph */
59
+ await getModulesInfosByStats ( compiler , statsJson , _this . modulesGraph ) ;
57
60
58
- /** transform modules graph */
59
- await getModulesInfosByStats (
60
- compiler ,
61
- statsJson ,
62
- _this . modulesGraph ,
63
- ) ;
61
+ debug ( Process . getMemoryUsageMessage , '[After Transform ModuleGraph]' ) ;
64
62
65
- debug ( Process . getMemoryUsageMessage , '[After Transform ModuleGraph]' ) ;
63
+ _this . modulesGraph &&
64
+ ( await _this . sdk . reportModuleGraph ( _this . modulesGraph ) ) ;
65
+ await _this . sdk . reportChunkGraph ( _this . chunkGraph ) ;
66
66
67
- _this . modulesGraph &&
68
- ( await _this . sdk . reportModuleGraph ( _this . modulesGraph ) ) ;
69
- await _this . sdk . reportChunkGraph ( _this . chunkGraph ) ;
67
+ /** Generate webpack-bundle-analyzer tile graph */
68
+ const reportFilePath = await ChunksBuildUtils . generateTileGraph (
69
+ statsJson as Plugin . BaseStats ,
70
+ {
71
+ reportFilename : path . join (
72
+ Constants . RsdoctorOutputFolder ,
73
+ ChunksBuildUtils . TileGraphReportName ,
74
+ ) ,
75
+ reportTitle : 'bundle-analyzer' ,
76
+ } ,
77
+ compiler . outputPath ,
78
+ ) ;
70
79
71
- /** Generate webpack-bundle-analyzer tile graph */
72
- const reportFilePath = await ChunksBuildUtils . generateTileGraph (
73
- statsJson as Plugin . BaseStats ,
74
- {
75
- reportFilename : ChunksBuildUtils . TileGraphReportName ,
76
- reportTitle : 'bundle-analyzer' ,
77
- } ,
78
- compiler . outputPath ,
79
- ) ;
80
+ reportFilePath &&
81
+ ( await _this . sdk . reportTileHtml (
82
+ fse . readFileSync ( reportFilePath , 'utf-8' ) ,
83
+ ) ) ;
84
+ } ,
85
+ ) ;
86
+ } ;
80
87
81
- reportFilePath &&
82
- ( await _this . sdk . reportTileHtml (
83
- fse . readFileSync ( reportFilePath , 'utf-8' ) ,
84
- ) ) ;
85
- } ,
86
- ) ;
88
+ /**
89
+ * @protected
90
+ * @description This function to get module parsed code and size;
91
+ * @param {Compiler } compiler
92
+ * @param {StatsCompilation } stats
93
+ * @param {ModuleGraph } moduleGraph
94
+ * @return {* }
95
+ * @memberof RsdoctorWebpackPlugin
96
+ */
97
+ async function getModulesInfosByStats (
98
+ compiler : Plugin . BaseCompiler ,
99
+ stats : Plugin . StatsCompilation ,
100
+ moduleGraph : ModuleGraph ,
101
+ ) {
102
+ if ( ! moduleGraph ) {
103
+ return ;
87
104
}
88
-
89
- /**
90
- * @protected
91
- * @description This function to get module parsed code and size;
92
- * @param {Compiler } compiler
93
- * @param {StatsCompilation } stats
94
- * @param {ModuleGraph } moduleGraph
95
- * @return {* }
96
- * @memberof RsdoctorWebpackPlugin
97
- */
98
- async function getModulesInfosByStats (
99
- compiler : Plugin . BaseCompiler ,
100
- stats : Plugin . StatsCompilation ,
101
- moduleGraph : ModuleGraph ,
102
- ) {
103
- if ( ! moduleGraph ) {
104
- return ;
105
- }
106
- try {
107
- const parsedModulesData =
108
- ( await ChunksBuildUtils . getAssetsModulesData (
109
- stats ,
110
- compiler . outputPath ,
111
- ) ) || { } ;
112
- ChunksUtils . transformAssetsModulesData ( parsedModulesData , moduleGraph ) ;
113
- } catch ( e ) { }
114
- }
105
+ try {
106
+ const parsedModulesData =
107
+ ( await ChunksBuildUtils . getAssetsModulesData (
108
+ stats ,
109
+ compiler . outputPath ,
110
+ ) ) || { } ;
111
+ ChunksUtils . transformAssetsModulesData ( parsedModulesData , moduleGraph ) ;
112
+ } catch ( e ) { }
113
+ }
0 commit comments