@rsdoctor/graph 1.6.1 → 1.6.2-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,5 +11,5 @@ export declare class Asset implements SDK.AssetInstance {
11
11
  toData(types: SDK.ToDataType): SDK.AssetData;
12
12
  setChunks(chunks: SDK.ChunkInstance[]): void;
13
13
  setId(id: number): void;
14
- setGzipSize(content: string): void;
14
+ setGzipSize(content: string, level?: number): void;
15
15
  }
@@ -11,5 +11,5 @@ export declare class Asset implements SDK.AssetInstance {
11
11
  toData(types: SDK.ToDataType): SDK.AssetData;
12
12
  setChunks(chunks: SDK.ChunkInstance[]): void;
13
13
  setId(id: number): void;
14
- setGzipSize(content: string): void;
14
+ setGzipSize(content: string, level?: number): void;
15
15
  }
@@ -0,0 +1,2 @@
1
+ export declare const DEFAULT_GZIP_LEVEL = 6;
2
+ export declare function getGzipSize(content: string, level?: number): number;
package/dist/gzip.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare const DEFAULT_GZIP_LEVEL = 6;
2
+ export declare function getGzipSize(content: string, level?: number): number;
package/dist/index.cjs CHANGED
@@ -228,7 +228,12 @@ class ChunkGraph {
228
228
  this._assetMap = new Map(), this._assetPathMap = new Map(), this._chunkMap = new Map(), this._entrypointMap = new Map();
229
229
  }
230
230
  }
231
- const types_namespaceObject = require("@rsdoctor/types"), external_node_zlib_namespaceObject = require("node:zlib");
231
+ const types_namespaceObject = require("@rsdoctor/types"), external_node_zlib_namespaceObject = require("node:zlib"), DEFAULT_GZIP_LEVEL = 6;
232
+ function getGzipSize(content, level = 6) {
233
+ return (0, external_node_zlib_namespaceObject.gzipSync)(content, {
234
+ level
235
+ }).length;
236
+ }
232
237
  let asset_id = 1;
233
238
  class Asset {
234
239
  static init() {
@@ -253,10 +258,8 @@ class Asset {
253
258
  setId(id) {
254
259
  this.id = id;
255
260
  }
256
- setGzipSize(content) {
257
- this.gzipSize = (0, external_node_zlib_namespaceObject.gzipSync)(content, {
258
- level: 9
259
- }).length;
261
+ setGzipSize(content, level) {
262
+ this.gzipSize = getGzipSize(content, level);
260
263
  }
261
264
  }
262
265
  let entrypoint_id = 1;
@@ -596,17 +599,7 @@ class Module {
596
599
  }
597
600
  setSize(input) {
598
601
  let { size } = this;
599
- if (size.sourceSize = input.sourceSize ?? size.sourceSize, size.transformedSize = input.transformedSize ?? size.transformedSize, size.parsedSize = input.parsedSize ?? size.parsedSize, 'number' == typeof input.gzipSize) size.gzipSize = input.gzipSize;
600
- else {
601
- let code = this.source.parsedSource || this.source.source;
602
- if (code && 'string' == typeof code && code.length > 0) try {
603
- size.gzipSize = (0, external_node_zlib_namespaceObject.gzipSync)(code, {
604
- level: 9
605
- }).length;
606
- } catch {
607
- size.gzipSize = 0;
608
- }
609
- }
602
+ size.sourceSize = input.sourceSize ?? size.sourceSize, size.transformedSize = input.transformedSize ?? size.transformedSize, size.parsedSize = input.parsedSize ?? size.parsedSize, 'number' == typeof input.gzipSize && (size.gzipSize = input.gzipSize);
610
603
  }
611
604
  getSize() {
612
605
  return {
@@ -1296,15 +1289,21 @@ class PackageGraph {
1296
1289
  }
1297
1290
  }
1298
1291
  const logger_namespaceObject = require("@rsdoctor/utils/logger");
1292
+ function tryGetGzipSize(content, { gzip = !0, gzipLevel = 6 }) {
1293
+ if (gzip && content) try {
1294
+ return getGzipSize(content, gzipLevel);
1295
+ } catch {
1296
+ return;
1297
+ }
1298
+ }
1299
1299
  async function getAssetsModulesData(moduleGraph, chunkGraph, bundleDir, opts, sourceMapSets = new Map(), assetsWithoutSourceMap) {
1300
+ let gzipOptions = {
1301
+ gzip: opts.gzip,
1302
+ gzipLevel: opts.gzipLevel
1303
+ };
1300
1304
  if (sourceMapSets.size > 0) {
1301
1305
  for (let [modulePath, codes] of ((0, logger_namespaceObject.time)("Start Parse bundle by sourcemap."), sourceMapSets.entries())){
1302
- let gzipSize, modules = moduleGraph.getModuleByFile(modulePath);
1303
- try {
1304
- codes && 'string' == typeof codes && codes.length > 0 && (gzipSize = (0, external_node_zlib_namespaceObject.gzipSync)(codes, {
1305
- level: 9
1306
- }).length);
1307
- } catch {}
1306
+ let modules = moduleGraph.getModuleByFile(modulePath), gzipSize = tryGetGzipSize(codes, gzipOptions);
1308
1307
  for (let module of modules)module?.setSize({
1309
1308
  parsedSize: codes.length,
1310
1309
  gzipSize
@@ -1334,19 +1333,21 @@ async function getAssetsModulesData(moduleGraph, chunkGraph, bundleDir, opts, so
1334
1333
  'runtimeSrc'
1335
1334
  ]), Object.assign(parsedModules, bundleInfo?.modules || {});
1336
1335
  }
1337
- common_namespaceObject.Lodash.isEmpty(bundlesSources) && (bundlesSources = null, parsedModules = null, process.env.DEVTOOLS_DEV && logger_namespaceObject.logger.warn('\nNo bundles were parsed. Analyzer will show only original module sizes from stats file.\n')), parsedModules && transformAssetsModulesData(parsedModules, moduleGraph);
1336
+ common_namespaceObject.Lodash.isEmpty(bundlesSources) && (bundlesSources = null, parsedModules = null, process.env.DEVTOOLS_DEV && logger_namespaceObject.logger.warn('\nNo bundles were parsed. Analyzer will show only original module sizes from stats file.\n')), parsedModules && transformAssetsModulesData(parsedModules, moduleGraph, gzipOptions);
1338
1337
  }
1339
1338
  (0, logger_namespaceObject.timeEnd)("Start Parse bundle by AST.");
1340
1339
  }
1340
+ if (!1 !== gzipOptions.gzip) for (let module of moduleGraph.getModules()){
1341
+ if (module.getSize().gzipSize > 0) continue;
1342
+ let source = module.getSource(), gzipSize = tryGetGzipSize(source.parsedSource || source.source, gzipOptions);
1343
+ void 0 !== gzipSize && module.setSize({
1344
+ gzipSize
1345
+ });
1346
+ }
1341
1347
  }
1342
- function transformAssetsModulesData(parsedModulesData, moduleGraph) {
1348
+ function transformAssetsModulesData(parsedModulesData, moduleGraph, gzipOptions = {}) {
1343
1349
  moduleGraph && Object.entries(parsedModulesData).forEach(([moduleId, parsedData])=>{
1344
- let gzipSize, module = moduleGraph.getModuleByWebpackId(moduleId ?? '');
1345
- try {
1346
- parsedData?.content && 'string' == typeof parsedData.content && parsedData.content.length > 0 && (gzipSize = (0, external_node_zlib_namespaceObject.gzipSync)(parsedData.content, {
1347
- level: 9
1348
- }).length);
1349
- } catch {}
1350
+ let module = moduleGraph.getModuleByWebpackId(moduleId ?? ''), gzipSize = tryGetGzipSize(parsedData?.content, gzipOptions);
1350
1351
  module?.setSize({
1351
1352
  parsedSize: parsedData?.size,
1352
1353
  gzipSize
@@ -1388,10 +1389,10 @@ function chunkTransform(assetMap, bundleStats) {
1388
1389
  return chunkGraph;
1389
1390
  }
1390
1391
  const COMPRESSIBLE_REGEX = /\.(?:js|css|html|json|svg|txt|xml|xhtml|wasm|manifest)$/i;
1391
- function assetsContents(assetMap, chunkGraph, supports) {
1392
+ function assetsContents(assetMap, chunkGraph, gzip) {
1392
1393
  chunkGraph.getAssets().forEach((asset)=>{
1393
1394
  let { content = '' } = assetMap.get(asset.path) || {};
1394
- asset.content = content, content.length > 0 && 0 === asset.size && (asset.size = Buffer.byteLength(content, 'utf8')), COMPRESSIBLE_REGEX.test(asset.path) && supports?.gzip && asset.setGzipSize(content);
1395
+ asset.content = content, content.length > 0 && 0 === asset.size && (asset.size = Buffer.byteLength(content, 'utf8')), COMPRESSIBLE_REGEX.test(asset.path) && !1 !== gzip && asset.setGzipSize(content, gzip.gzipLevel);
1395
1396
  });
1396
1397
  }
1397
1398
  const external_path_browserify_namespaceObject = require("path-browserify");
package/dist/index.js CHANGED
@@ -200,6 +200,11 @@ class ChunkGraph {
200
200
  this._assetMap = new Map(), this._assetPathMap = new Map(), this._chunkMap = new Map(), this._entrypointMap = new Map();
201
201
  }
202
202
  }
203
+ function getGzipSize(content, level = 6) {
204
+ return gzipSync(content, {
205
+ level
206
+ }).length;
207
+ }
203
208
  let asset_id = 1;
204
209
  class Asset {
205
210
  static init() {
@@ -224,10 +229,8 @@ class Asset {
224
229
  setId(id) {
225
230
  this.id = id;
226
231
  }
227
- setGzipSize(content) {
228
- this.gzipSize = gzipSync(content, {
229
- level: 9
230
- }).length;
232
+ setGzipSize(content, level) {
233
+ this.gzipSize = getGzipSize(content, level);
231
234
  }
232
235
  }
233
236
  let entrypoint_id = 1;
@@ -564,17 +567,7 @@ class Module {
564
567
  }
565
568
  setSize(input) {
566
569
  let { size } = this;
567
- if (size.sourceSize = input.sourceSize ?? size.sourceSize, size.transformedSize = input.transformedSize ?? size.transformedSize, size.parsedSize = input.parsedSize ?? size.parsedSize, 'number' == typeof input.gzipSize) size.gzipSize = input.gzipSize;
568
- else {
569
- let code = this.source.parsedSource || this.source.source;
570
- if (code && 'string' == typeof code && code.length > 0) try {
571
- size.gzipSize = gzipSync(code, {
572
- level: 9
573
- }).length;
574
- } catch {
575
- size.gzipSize = 0;
576
- }
577
- }
570
+ size.sourceSize = input.sourceSize ?? size.sourceSize, size.transformedSize = input.transformedSize ?? size.transformedSize, size.parsedSize = input.parsedSize ?? size.parsedSize, 'number' == typeof input.gzipSize && (size.gzipSize = input.gzipSize);
578
571
  }
579
572
  getSize() {
580
573
  return {
@@ -1259,15 +1252,21 @@ class PackageGraph {
1259
1252
  };
1260
1253
  }
1261
1254
  }
1255
+ function tryGetGzipSize(content, { gzip = !0, gzipLevel = 6 }) {
1256
+ if (gzip && content) try {
1257
+ return getGzipSize(content, gzipLevel);
1258
+ } catch {
1259
+ return;
1260
+ }
1261
+ }
1262
1262
  async function getAssetsModulesData(moduleGraph, chunkGraph, bundleDir, opts, sourceMapSets = new Map(), assetsWithoutSourceMap) {
1263
+ let gzipOptions = {
1264
+ gzip: opts.gzip,
1265
+ gzipLevel: opts.gzipLevel
1266
+ };
1263
1267
  if (sourceMapSets.size > 0) {
1264
1268
  for (let [modulePath, codes] of (time("Start Parse bundle by sourcemap."), sourceMapSets.entries())){
1265
- let gzipSize, modules = moduleGraph.getModuleByFile(modulePath);
1266
- try {
1267
- codes && 'string' == typeof codes && codes.length > 0 && (gzipSize = gzipSync(codes, {
1268
- level: 9
1269
- }).length);
1270
- } catch {}
1269
+ let modules = moduleGraph.getModuleByFile(modulePath), gzipSize = tryGetGzipSize(codes, gzipOptions);
1271
1270
  for (let module of modules)module?.setSize({
1272
1271
  parsedSize: codes.length,
1273
1272
  gzipSize
@@ -1297,19 +1296,21 @@ async function getAssetsModulesData(moduleGraph, chunkGraph, bundleDir, opts, so
1297
1296
  'runtimeSrc'
1298
1297
  ]), Object.assign(parsedModules, bundleInfo?.modules || {});
1299
1298
  }
1300
- Lodash.isEmpty(bundlesSources) && (bundlesSources = null, parsedModules = null, process.env.DEVTOOLS_DEV && logger.warn('\nNo bundles were parsed. Analyzer will show only original module sizes from stats file.\n')), parsedModules && transformAssetsModulesData(parsedModules, moduleGraph);
1299
+ Lodash.isEmpty(bundlesSources) && (bundlesSources = null, parsedModules = null, process.env.DEVTOOLS_DEV && logger.warn('\nNo bundles were parsed. Analyzer will show only original module sizes from stats file.\n')), parsedModules && transformAssetsModulesData(parsedModules, moduleGraph, gzipOptions);
1301
1300
  }
1302
1301
  timeEnd("Start Parse bundle by AST.");
1303
1302
  }
1303
+ if (!1 !== gzipOptions.gzip) for (let module of moduleGraph.getModules()){
1304
+ if (module.getSize().gzipSize > 0) continue;
1305
+ let source = module.getSource(), gzipSize = tryGetGzipSize(source.parsedSource || source.source, gzipOptions);
1306
+ void 0 !== gzipSize && module.setSize({
1307
+ gzipSize
1308
+ });
1309
+ }
1304
1310
  }
1305
- function transformAssetsModulesData(parsedModulesData, moduleGraph) {
1311
+ function transformAssetsModulesData(parsedModulesData, moduleGraph, gzipOptions = {}) {
1306
1312
  moduleGraph && Object.entries(parsedModulesData).forEach(([moduleId, parsedData])=>{
1307
- let gzipSize, module = moduleGraph.getModuleByWebpackId(moduleId ?? '');
1308
- try {
1309
- parsedData?.content && 'string' == typeof parsedData.content && parsedData.content.length > 0 && (gzipSize = gzipSync(parsedData.content, {
1310
- level: 9
1311
- }).length);
1312
- } catch {}
1313
+ let module = moduleGraph.getModuleByWebpackId(moduleId ?? ''), gzipSize = tryGetGzipSize(parsedData?.content, gzipOptions);
1313
1314
  module?.setSize({
1314
1315
  parsedSize: parsedData?.size,
1315
1316
  gzipSize
@@ -1351,10 +1352,10 @@ function chunkTransform(assetMap, bundleStats) {
1351
1352
  return chunkGraph;
1352
1353
  }
1353
1354
  let COMPRESSIBLE_REGEX = /\.(?:js|css|html|json|svg|txt|xml|xhtml|wasm|manifest)$/i;
1354
- function assetsContents(assetMap, chunkGraph, supports) {
1355
+ function assetsContents(assetMap, chunkGraph, gzip) {
1355
1356
  chunkGraph.getAssets().forEach((asset)=>{
1356
1357
  let { content = '' } = assetMap.get(asset.path) || {};
1357
- asset.content = content, content.length > 0 && 0 === asset.size && (asset.size = Buffer.byteLength(content, 'utf8')), COMPRESSIBLE_REGEX.test(asset.path) && supports?.gzip && asset.setGzipSize(content);
1358
+ asset.content = content, content.length > 0 && 0 === asset.size && (asset.size = Buffer.byteLength(content, 'utf8')), COMPRESSIBLE_REGEX.test(asset.path) && !1 !== gzip && asset.setGzipSize(content, gzip.gzipLevel);
1358
1359
  });
1359
1360
  }
1360
1361
  function isImportDependency({ type }) {
@@ -1,4 +1,4 @@
1
1
  import { SDK, Plugin } from '@rsdoctor/types';
2
2
  export declare function assetsContents(assetMap: Map<string, {
3
3
  content: string;
4
- }>, chunkGraph: SDK.ChunkGraphInstance, supports: Plugin.RsdoctorPluginOptionsNormalized['supports']): void;
4
+ }>, chunkGraph: SDK.ChunkGraphInstance, gzip: Plugin.NormalizedGzipConfig): void;
@@ -1,4 +1,4 @@
1
1
  import { SDK, Plugin } from '@rsdoctor/types';
2
2
  export declare function assetsContents(assetMap: Map<string, {
3
3
  content: string;
4
- }>, chunkGraph: SDK.ChunkGraphInstance, supports: Plugin.RsdoctorPluginOptionsNormalized['supports']): void;
4
+ }>, chunkGraph: SDK.ChunkGraphInstance, gzip: Plugin.NormalizedGzipConfig): void;
@@ -7,6 +7,10 @@ export type ParsedModuleSizeData = {
7
7
  content: string;
8
8
  };
9
9
  };
10
+ interface GzipOptions {
11
+ gzip?: boolean;
12
+ gzipLevel?: number;
13
+ }
10
14
  /**
11
15
  * The following code is modified based on
12
16
  * https://github.com/webpack-contrib/webpack-bundle-analyzer/blob/8a3d3f0f40010f2b41ccd28519eda5a44e13da3e/src/analyzer.js#L20
@@ -18,5 +22,6 @@ export type ParsedModuleSizeData = {
18
22
  */
19
23
  export declare function getAssetsModulesData(moduleGraph: SDK.ModuleGraphInstance, chunkGraph: SDK.ChunkGraphInstance, bundleDir: string, opts: {
20
24
  parseBundle?: ParseBundle;
21
- }, sourceMapSets?: Map<string, string>, assetsWithoutSourceMap?: Set<string>): Promise<void>;
22
- export declare function transformAssetsModulesData(parsedModulesData: ParsedModuleSizeData, moduleGraph: SDK.ModuleGraphInstance): void;
25
+ } & GzipOptions, sourceMapSets?: Map<string, string>, assetsWithoutSourceMap?: Set<string>): Promise<void>;
26
+ export declare function transformAssetsModulesData(parsedModulesData: ParsedModuleSizeData, moduleGraph: SDK.ModuleGraphInstance, gzipOptions?: GzipOptions): void;
27
+ export {};
@@ -7,6 +7,10 @@ export type ParsedModuleSizeData = {
7
7
  content: string;
8
8
  };
9
9
  };
10
+ interface GzipOptions {
11
+ gzip?: boolean;
12
+ gzipLevel?: number;
13
+ }
10
14
  /**
11
15
  * The following code is modified based on
12
16
  * https://github.com/webpack-contrib/webpack-bundle-analyzer/blob/8a3d3f0f40010f2b41ccd28519eda5a44e13da3e/src/analyzer.js#L20
@@ -18,5 +22,6 @@ export type ParsedModuleSizeData = {
18
22
  */
19
23
  export declare function getAssetsModulesData(moduleGraph: SDK.ModuleGraphInstance, chunkGraph: SDK.ChunkGraphInstance, bundleDir: string, opts: {
20
24
  parseBundle?: ParseBundle;
21
- }, sourceMapSets?: Map<string, string>, assetsWithoutSourceMap?: Set<string>): Promise<void>;
22
- export declare function transformAssetsModulesData(parsedModulesData: ParsedModuleSizeData, moduleGraph: SDK.ModuleGraphInstance): void;
25
+ } & GzipOptions, sourceMapSets?: Map<string, string>, assetsWithoutSourceMap?: Set<string>): Promise<void>;
26
+ export declare function transformAssetsModulesData(parsedModulesData: ParsedModuleSizeData, moduleGraph: SDK.ModuleGraphInstance, gzipOptions?: GzipOptions): void;
27
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsdoctor/graph",
3
- "version": "1.6.1",
3
+ "version": "1.6.2-beta.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/web-infra-dev/rsdoctor",
@@ -30,8 +30,8 @@
30
30
  "es-toolkit": "^1.49.0",
31
31
  "path-browserify": "1.0.1",
32
32
  "source-map": "^0.7.6",
33
- "@rsdoctor/types": "1.6.1",
34
- "@rsdoctor/utils": "1.6.1"
33
+ "@rsdoctor/utils": "1.6.2-beta.0",
34
+ "@rsdoctor/types": "1.6.2-beta.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/body-parser": "1.19.6",