@bymaksym/loadline 0.1.0 → 1.1.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.
Files changed (115) hide show
  1. package/README.md +272 -45
  2. package/dist/cli/cli/args.js +97 -116
  3. package/dist/cli/cli/gates.js +80 -14
  4. package/dist/cli/cli/main.js +275 -31
  5. package/dist/cli/cli/memory.js +114 -0
  6. package/dist/cli/cli/read/declared.js +52 -0
  7. package/dist/cli/cli/read/locate.js +214 -0
  8. package/dist/cli/cli/{read-build.js → read/read-build.js} +156 -87
  9. package/dist/cli/cli/read/read-config.js +120 -0
  10. package/dist/cli/cli/render/html-report.js +111 -0
  11. package/dist/cli/cli/render/render-agent.js +114 -0
  12. package/dist/cli/cli/render/render-asked.js +104 -0
  13. package/dist/cli/cli/render/render-badge.js +140 -0
  14. package/dist/cli/cli/{render-json.js → render/render-json.js} +26 -10
  15. package/dist/cli/cli/{render-markdown.js → render/render-markdown.js} +6 -6
  16. package/dist/cli/cli/{render-pr.js → render/render-pr.js} +37 -15
  17. package/dist/cli/cli/{render-sarif.js → render/render-sarif.js} +11 -6
  18. package/dist/cli/cli/{render-text.js → render/render-text.js} +169 -127
  19. package/dist/cli/cli/render/terminal.js +55 -0
  20. package/dist/cli/cli/report.js +76 -29
  21. package/dist/cli/cli/self-check.js +19 -23
  22. package/dist/cli/cli/since-last.js +45 -0
  23. package/dist/cli/cli/text/text-errors.js +124 -0
  24. package/dist/cli/cli/text/text.js +343 -0
  25. package/dist/cli/cli/text/usage.js +240 -0
  26. package/dist/cli/src/app/core/analysis/analysis.js +68 -24
  27. package/dist/cli/src/app/core/analysis/{barrels.js → graph/barrels.js} +10 -3
  28. package/dist/cli/src/app/core/analysis/{blast.js → graph/blast.js} +56 -17
  29. package/dist/cli/src/app/core/analysis/{cycles.js → graph/cycles.js} +1 -1
  30. package/dist/cli/src/app/core/analysis/{importers.js → graph/importers.js} +1 -1
  31. package/dist/cli/src/app/core/analysis/{insights.js → graph/insights.js} +10 -4
  32. package/dist/cli/src/app/core/analysis/invariant.js +1 -1
  33. package/dist/cli/src/app/core/analysis/metafile.types.js +21 -7
  34. package/dist/cli/src/app/core/analysis/{delivery.js → screens/delivery.js} +1 -1
  35. package/dist/cli/src/app/core/analysis/{entries.js → screens/entries.js} +87 -11
  36. package/dist/cli/src/app/core/analysis/screens/marks.js +32 -0
  37. package/dist/cli/src/app/core/analysis/screens/route-files.js +116 -0
  38. package/dist/cli/src/app/core/analysis/{sourcemap.js → sourcemap/sourcemap.js} +106 -7
  39. package/dist/cli/src/app/core/analysis/{split-drift.js → sourcemap/split-drift.js} +13 -7
  40. package/dist/cli/src/app/core/analysis/tool.js +83 -0
  41. package/dist/cli/src/app/core/assets/assets.js +137 -16
  42. package/dist/cli/src/app/core/baseline/baseline.js +0 -0
  43. package/dist/cli/src/app/core/{intake → build-text}/index-html.js +234 -29
  44. package/dist/cli/src/app/core/build-text/route-table.js +178 -0
  45. package/dist/cli/src/app/core/build-text/tool-marks.js +54 -0
  46. package/dist/cli/src/app/core/caching/caching.js +123 -45
  47. package/dist/cli/src/app/core/caching/from-analysis.js +20 -4
  48. package/dist/cli/src/app/core/config/config-text.js +90 -0
  49. package/dist/cli/src/app/core/config/config-values.js +358 -0
  50. package/dist/cli/src/app/core/config/loadline-config.js +81 -16
  51. package/dist/cli/src/app/core/config/merge-config.js +98 -0
  52. package/dist/cli/src/app/core/deps/deps.js +141 -23
  53. package/dist/cli/src/app/core/deps/pins.js +63 -0
  54. package/dist/cli/src/app/core/export/summary.js +54 -12
  55. package/dist/cli/src/app/core/findings/actions.js +65 -6
  56. package/dist/cli/src/app/core/findings/bundle/boot-single.js +76 -0
  57. package/dist/cli/src/app/core/findings/bundle/dupes.js +68 -0
  58. package/dist/cli/src/app/core/findings/{graph.js → bundle/graph.js} +32 -20
  59. package/dist/cli/src/app/core/findings/{own-in-boot.js → bundle/own-in-boot.js} +9 -5
  60. package/dist/cli/src/app/core/findings/{requests.js → bundle/requests.js} +3 -3
  61. package/dist/cli/src/app/core/findings/{shipped.js → bundle/shipped.js} +30 -18
  62. package/dist/cli/src/app/core/findings/compose.js +6 -1
  63. package/dist/cli/src/app/core/findings/effort.js +4 -0
  64. package/dist/cli/src/app/core/findings/finding.types.js +2 -0
  65. package/dist/cli/src/app/core/findings/findings.js +68 -55
  66. package/dist/cli/src/app/core/findings/{assets.js → folder/assets.js} +65 -16
  67. package/dist/cli/src/app/core/findings/{caching.js → folder/caching.js} +17 -10
  68. package/dist/cli/src/app/core/findings/{page.js → folder/page.js} +6 -2
  69. package/dist/cli/src/app/core/findings/{scan.js → folder/scan.js} +37 -13
  70. package/dist/cli/src/app/core/findings/kind-names.js +163 -0
  71. package/dist/cli/src/app/core/findings/{deps.js → supplied/deps.js} +13 -8
  72. package/dist/cli/src/app/core/findings/supplied/forbidden.js +71 -0
  73. package/dist/cli/src/app/core/findings/{situation.js → supplied/situation.js} +2 -2
  74. package/dist/cli/src/app/core/findings/text/causes.js +39 -0
  75. package/dist/cli/src/app/core/findings/text/finding-html.js +33 -0
  76. package/dist/cli/src/app/core/findings/{finding-text.js → text/finding-text.js} +307 -103
  77. package/dist/cli/src/app/core/findings/text/tool-advice.js +97 -0
  78. package/dist/cli/src/app/core/format/format.utils.js +172 -18
  79. package/dist/cli/src/app/core/format/ownership.js +93 -0
  80. package/dist/cli/src/app/core/i18n/en.js +180 -61
  81. package/dist/cli/src/app/core/i18n/es.js +192 -68
  82. package/dist/cli/src/app/core/intake/bundle-graph.js +285 -46
  83. package/dist/cli/src/app/core/intake/dist-files.js +87 -16
  84. package/dist/cli/src/app/core/intake/embedded.js +56 -0
  85. package/dist/cli/src/app/core/intake/unreached.js +139 -0
  86. package/dist/cli/src/app/core/intake/webpack-modules.js +185 -0
  87. package/dist/cli/src/app/core/intake/webpack-stats.js +298 -0
  88. package/dist/cli/src/app/core/project/budget-advice.js +14 -10
  89. package/dist/cli/src/app/core/project/project-context.js +89 -19
  90. package/dist/cli/src/app/core/project/project-name.js +36 -0
  91. package/dist/cli/src/app/core/scan/scan.js +116 -39
  92. package/dist/cli/src/app/core/scan/secrets.js +0 -0
  93. package/dist/cli/src/app/core/situation/situation-text.js +23 -0
  94. package/dist/cli/src/app/core/situation/situation.js +9 -8
  95. package/dist/cli/src/app/core/timing/timing.js +5 -2
  96. package/dist/cli/src/app/core/whatif/defer.js +29 -4
  97. package/dist/cli/src/app/core/whatif/source-importers.js +58 -0
  98. package/dist/cli/src/app/core/whatif/why.js +102 -0
  99. package/dist/cli/src/app/state/report-messages.utils.js +1 -4
  100. package/docs/CONFIG-REFERENCE.md +230 -0
  101. package/docs/CONFIG.md +511 -0
  102. package/docs/HOW-IT-WORKS.md +286 -25
  103. package/loadline.html +26 -24
  104. package/loadline.schema.json +1209 -0
  105. package/package.json +71 -35
  106. package/dist/cli/cli/text.js +0 -180
  107. package/dist/cli/src/app/core/findings/finding-html.js +0 -13
  108. /package/dist/cli/cli/{read-build.types.js → read/read-build.types.js} +0 -0
  109. /package/dist/cli/cli/{render-json.types.js → render/render-json.types.js} +0 -0
  110. /package/dist/cli/cli/{text.types.js → text/text.types.js} +0 -0
  111. /package/dist/cli/src/app/core/analysis/{exclusive.js → graph/exclusive.js} +0 -0
  112. /package/dist/cli/src/app/core/analysis/{insights.types.js → graph/insights.types.js} +0 -0
  113. /package/dist/cli/src/app/core/analysis/{module-graph.js → graph/module-graph.js} +0 -0
  114. /package/dist/cli/src/app/core/analysis/{sourcemap.types.js → sourcemap/sourcemap.types.js} +0 -0
  115. /package/dist/cli/src/app/core/findings/{finding-plain.js → text/finding-plain.js} +0 -0
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ /**
3
+ * The advice that depends on the tool that wrote the build, in its own words.
4
+ *
5
+ * Each signal used to name Vite and Angular side by side — "`sourcemap: true` in Vite, `"sourceMap":
6
+ * true` in Angular" — which is right for two tools and noise for the rest: an Ember build was told
7
+ * to edit a Vite config it does not have. With the tool known (`analysis/tool.ts`) the advice is the
8
+ * one line that applies; without it, it is the two common ones, as before.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.webpackSplit = exports.mayBeAngular = exports.mapsHidden = exports.mapsOn = void 0;
12
+ const finding_html_1 = require("./finding-html");
13
+ const VITE_CONFIG = { en: 'vite.config', es: 'vite.config' };
14
+ /** Source maps on. */
15
+ const MAPS = {
16
+ angular: { code: '"sourceMap": true', where: { en: 'angular.json', es: 'angular.json' } },
17
+ nuxt: { code: 'sourcemap: { client: true }', where: { en: 'nuxt.config', es: 'nuxt.config' } },
18
+ stencil: {
19
+ code: 'sourceMap: true',
20
+ where: { en: 'stencil.config.ts (Stencil 2 or later)', es: 'stencil.config.ts (Stencil 2 o posterior)' },
21
+ },
22
+ ember: { code: 'sourcemaps: { enabled: true }', where: { en: 'ember-cli-build.js', es: 'ember-cli-build.js' } },
23
+ sapper: {
24
+ code: 'output.sourcemap: true',
25
+ where: { en: 'the client of rollup.config.js', es: 'el client de rollup.config.js' },
26
+ },
27
+ vite: { code: 'build.sourcemap: true', where: VITE_CONFIG },
28
+ webpack: {
29
+ code: "devtool: 'source-map'",
30
+ where: { en: 'the webpack configuration', es: 'la configuración de webpack' },
31
+ },
32
+ rollup: { code: 'output.sourcemap: true', where: { en: 'rollup.config.js', es: 'rollup.config.js' } },
33
+ esbuild: { code: '--sourcemap', where: { en: 'the esbuild command', es: 'el comando de esbuild' } },
34
+ };
35
+ /** Source maps written but not linked from the bundle: for an error tool, not for the public. */
36
+ const HIDDEN_MAPS = {
37
+ angular: {
38
+ code: '"sourceMap": { "scripts": true, "hidden": true }',
39
+ where: { en: 'angular.json', es: 'angular.json' },
40
+ },
41
+ nuxt: { code: "sourcemap: { client: 'hidden' }", where: { en: 'nuxt.config', es: 'nuxt.config' } },
42
+ sapper: { code: "output.sourcemap: 'hidden'", where: { en: 'rollup.config.js', es: 'rollup.config.js' } },
43
+ vite: { code: "build.sourcemap: 'hidden'", where: VITE_CONFIG },
44
+ webpack: {
45
+ code: "devtool: 'hidden-source-map'",
46
+ where: { en: 'the webpack configuration', es: 'la configuración de webpack' },
47
+ },
48
+ rollup: { code: "output.sourcemap: 'hidden'", where: { en: 'rollup.config.js', es: 'rollup.config.js' } },
49
+ esbuild: { code: '--sourcemap=external', where: { en: 'the esbuild command', es: 'el comando de esbuild' } },
50
+ };
51
+ /** The framework's own setting first — Angular on webpack is still `angular.json` — then the bundler's. */
52
+ const settingFor = (table, tool) => (tool?.framework ? table[tool.framework] : undefined) ?? (tool?.bundler ? table[tool.bundler] : undefined) ?? null;
53
+ const said = (setting, lang) => `${(0, finding_html_1.mono)(setting.code)} ${lang === 'es' ? 'en' : 'in'} ${setting.where[lang]}`;
54
+ /** "`build.sourcemap: true` in vite.config", or the two common ones when the tool is not known. */
55
+ const mapsOn = (tool, lang) => {
56
+ const setting = settingFor(MAPS, tool);
57
+ if (setting) {
58
+ return said(setting, lang);
59
+ }
60
+ return lang === 'es'
61
+ ? `${(0, finding_html_1.mono)('sourcemap: true')} en Vite, ${(0, finding_html_1.mono)('"sourceMap": true')} en Angular`
62
+ : `${(0, finding_html_1.mono)('sourcemap: true')} in Vite, ${(0, finding_html_1.mono)('"sourceMap": true')} in Angular`;
63
+ };
64
+ exports.mapsOn = mapsOn;
65
+ /** The same for maps generated without being linked. */
66
+ const mapsHidden = (tool, lang) => {
67
+ const setting = settingFor(HIDDEN_MAPS, tool);
68
+ if (setting) {
69
+ return said(setting, lang);
70
+ }
71
+ return lang === 'es'
72
+ ? `${(0, finding_html_1.mono)('"sourceMap": { "scripts": true, "hidden": true }')} en Angular, ${(0, finding_html_1.mono)("build.sourcemap: 'hidden'")} en Vite`
73
+ : `${(0, finding_html_1.mono)('"sourceMap": { "scripts": true, "hidden": true }')} in Angular, ${(0, finding_html_1.mono)("build.sourcemap: 'hidden'")} in Vite`;
74
+ };
75
+ exports.mapsHidden = mapsHidden;
76
+ /** Whether the build may be Angular's, which ships a `console` call in every build there is. */
77
+ const mayBeAngular = (tool) => !tool?.framework || tool.framework === 'angular';
78
+ exports.mayBeAngular = mayBeAngular;
79
+ /**
80
+ * How a webpack build splits its dependencies out, said first when the build is webpack's: the
81
+ * `manualChunks` the rest of the advice names is Rollup's. Angular up to 16 builds with webpack and
82
+ * has an option of its own for it.
83
+ */
84
+ const webpackSplit = (tool, lang) => {
85
+ if (tool?.bundler !== 'webpack') {
86
+ return '';
87
+ }
88
+ if (tool.framework === 'angular') {
89
+ return lang === 'es'
90
+ ? `con ${(0, finding_html_1.mono)('"vendorChunk": true')} en angular.json, que el browser builder de Angular 16 o anterior tiene para eso; `
91
+ : `with ${(0, finding_html_1.mono)('"vendorChunk": true')} in angular.json, which the browser builder of Angular 16 or earlier has for it; `;
92
+ }
93
+ return lang === 'es'
94
+ ? `en webpack con un grupo de ${(0, finding_html_1.mono)('optimization.splitChunks.cacheGroups')} para ${(0, finding_html_1.mono)('node_modules')}; `
95
+ : `in webpack with a group of ${(0, finding_html_1.mono)('optimization.splitChunks.cacheGroups')} for ${(0, finding_html_1.mono)('node_modules')}; `;
96
+ };
97
+ exports.webpackSplit = webpackSplit;
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  /** Presentation helpers and metafile path normalisation. */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.formatDelta = exports.elidePath = exports.screenLabel = exports.SOURCE_EXTENSION = exports.ROUTE_SUFFIXES = exports.VIEW_SUFFIXES = exports.projectFolderOf = exports.chainSteps = exports.packageOf = exports.shortName = exports.baseName = exports.formatCount = exports.formatBytes = exports.setNumberLang = void 0;
4
+ exports.matchesStars = exports.namedBy = exports.formatDelta = exports.elidePath = exports.uniqueLabels = exports.screenLabel = exports.chunkLabel = exports.SOURCE_EXTENSION = exports.ROUTE_SUFFIXES = exports.VIEW_SUFFIXES = exports.projectFolderOf = exports.chainSteps = exports.packageOf = exports.shortName = exports.ANGULAR_BAZEL = exports.baseName = exports.formatCount = exports.formatBytesApart = exports.formatBytes = exports.setUnitSpace = exports.setNumberLang = void 0;
5
+ const ownership_1 = require("./ownership");
5
6
  const KB = 1024;
6
7
  /**
7
8
  * The language the figures are written in. It only decides the decimal separator, and it is set
@@ -12,7 +13,7 @@ const KB = 1024;
12
13
  const formatter = (lang) => new Intl.NumberFormat(lang, { minimumFractionDigits: 2, maximumFractionDigits: 2 });
13
14
  /** The same thing for figures that are not sizes, where two decimals would be false precision. */
14
15
  const counter = (lang) => new Intl.NumberFormat(lang, { maximumFractionDigits: 1 });
15
- const numbers = { lang: 'en', decimals: formatter('en'), counts: counter('en') };
16
+ const numbers = { lang: 'en', decimals: formatter('en'), counts: counter('en'), unitSpace: ' ' };
16
17
  const setNumberLang = (lang) => {
17
18
  if (lang === numbers.lang) {
18
19
  return;
@@ -22,6 +23,15 @@ const setNumberLang = (lang) => {
22
23
  numbers.counts = counter(lang);
23
24
  };
24
25
  exports.setNumberLang = setNumberLang;
26
+ /**
27
+ * What goes between a size and its unit. A plain space for the command, whose output is a terminal
28
+ * and plain text. The page sets a narrow no-break space (U+202F): "736 kB" must never break across
29
+ * two lines, and in the monospace face a full space reads as a double gap between figure and unit.
30
+ */
31
+ const setUnitSpace = (space) => {
32
+ numbers.unitSpace = space;
33
+ };
34
+ exports.setUnitSpace = setUnitSpace;
25
35
  /**
26
36
  * Human-readable size. Stays in kB up to the megabyte so columns compare at a glance.
27
37
  *
@@ -31,14 +41,23 @@ exports.setNumberLang = setNumberLang;
31
41
  */
32
42
  const formatBytes = (bytes) => {
33
43
  if (bytes >= KB * KB) {
34
- return `${numbers.decimals.format(bytes / KB / KB)} MB`;
44
+ return `${numbers.decimals.format(bytes / KB / KB)}${numbers.unitSpace}MB`;
35
45
  }
36
- if (bytes >= KB) {
37
- return `${Math.round(bytes / KB)} kB`;
38
- }
39
- return `${bytes} B`;
46
+ const space = numbers.unitSpace;
47
+ return bytes >= KB ? `${Math.round(bytes / KB)}${space}kB` : `${bytes}${space}B`;
40
48
  };
41
49
  exports.formatBytes = formatBytes;
50
+ /**
51
+ * Two sizes that have to read as different: a figure and the limit it broke. Rounded alike they
52
+ * said "1 kB, over the 1 kB allowed" of 1,047 bytes against 1,024; then both are given in bytes.
53
+ */
54
+ const formatBytesApart = (actual, limit) => {
55
+ const [a, b] = [(0, exports.formatBytes)(actual), (0, exports.formatBytes)(limit)];
56
+ return a === b && actual !== limit
57
+ ? [`${(0, exports.formatCount)(actual)}${numbers.unitSpace}B`, `${(0, exports.formatCount)(limit)}${numbers.unitSpace}B`]
58
+ : [a, b];
59
+ };
60
+ exports.formatBytesApart = formatBytesApart;
42
61
  /**
43
62
  * A small figure that is not a size: releases a week, screens a session, times a week something
44
63
  * gets paid for.
@@ -52,21 +71,52 @@ exports.formatCount = formatCount;
52
71
  /** Last segment of a path: `chunk-ABC.js`, `login.page.ts`. */
53
72
  const baseName = (path) => path.split('/').pop() ?? path;
54
73
  exports.baseName = baseName;
74
+ /**
75
+ * Where Angular's own source maps point. Its packages are built with Bazel, and the maps they ship
76
+ * name the file in the Bazel output folder, not in the package: once a build composes them, a file
77
+ * of `@angular/forms` reads `node_modules/.pnpm/k8-fastbuild-ST-fdfa778d11ba/bin/packages/forms/…`
78
+ * (`bin/src/material/…` for the components repo). Read like any other path, that is a package
79
+ * called `.pnpm` — 72 kB of one real build's first load were — or, under npm, `k8-fastbuild-…`.
80
+ * Only a build read through its source maps meets it: a stats file names the `.mjs` in the package.
81
+ */
82
+ exports.ANGULAR_BAZEL = /^.*node_modules\/(?:\.pnpm\/)?[\w-]+-(?:fastbuild|opt|dbg)(?:-ST-[\da-f]+)?\/bin\/(?:packages|src)\//;
55
83
  /**
56
84
  * Strips the `node_modules` noise to leave the name a package is recognised by.
57
85
  * pnpm adds an extra segment (`.pnpm/pkg@version/node_modules/`) that has to be skipped first.
58
86
  */
59
- const shortName = (path) => path.replace(/^.*node_modules\/\.pnpm\/[^/]+\/node_modules\//, '').replace(/^.*node_modules\//, '');
87
+ const shortName = (path) => {
88
+ // What is yours is named by its path, wherever it sits; what `loadline.json` calls a dependency
89
+ // by the package it makes (`ownership.ts`).
90
+ if (!(0, ownership_1.isDependency)(path)) {
91
+ return path;
92
+ }
93
+ const declared = (0, ownership_1.declaredPackage)(path);
94
+ if (declared) {
95
+ return declared.rest ? `${declared.name}/${declared.rest}` : declared.name;
96
+ }
97
+ return path
98
+ .replace(exports.ANGULAR_BAZEL, '@angular/')
99
+ .replace(/^.*node_modules\/\.pnpm\/[^/]+\/node_modules\//, '')
100
+ .replace(/^.*node_modules\//, '');
101
+ };
60
102
  exports.shortName = shortName;
61
103
  /** Name of the npm package a path belongs to, or `null` if it is project code. */
62
104
  const packageOf = (path) => {
63
- const short = (0, exports.shortName)(path);
64
- if (short === path) {
105
+ if (!(0, ownership_1.isDependency)(path)) {
65
106
  return null;
66
107
  }
108
+ const declared = (0, ownership_1.declaredPackage)(path);
109
+ if (declared) {
110
+ return declared.name;
111
+ }
112
+ const short = (0, exports.shortName)(path);
67
113
  const parts = short.split('/');
68
114
  const head = parts[0] ?? short;
69
- return head.startsWith('@') ? parts.slice(0, 2).join('/') : head;
115
+ // A file straight in `node_modules/` or in a scope is a package of one file, resolved by name:
116
+ // Sapper's `@sapper/app.mjs` is `@sapper/app`, not a package called `@sapper/app.mjs`.
117
+ const scoped = head.startsWith('@');
118
+ const name = scoped ? parts.slice(0, 2).join('/') : head;
119
+ return parts.length === (scoped ? 2 : 1) ? name.replace(/\.[cm]?js$/, '') : name;
70
120
  };
71
121
  exports.packageOf = packageOf;
72
122
  /**
@@ -137,29 +187,104 @@ exports.ROUTE_SUFFIXES = ['route', 'routes'];
137
187
  * listed as `orders.page.vue`, extension and all.
138
188
  */
139
189
  exports.SOURCE_EXTENSION = '(?:[tj]sx?|vue|svelte)';
140
- const SUFFIX = new RegExp(String.raw `\.(?:${[...exports.VIEW_SUFFIXES, ...exports.ROUTE_SUFFIXES].join('|')})\.${exports.SOURCE_EXTENSION}$`);
190
+ /**
191
+ * What Angular up to 8 starts a lazy route at: `loadChildren: './article/article.module#…'` makes a
192
+ * chunk of `article.module.ngfactory.js` (or of `article.module.ts` without AOT), and an Angular 8
193
+ * build read through its webpack stats listed its screens as `article.module.ngfactory`. Only for
194
+ * the name: a module is no view, and the rules that tell screens from pieces of them never read it.
195
+ */
196
+ const LAZY_MODULE_SUFFIXES = [String.raw `module\.ngfactory`, 'module'];
197
+ const SUFFIX = new RegExp(String.raw `\.(?:${[...exports.VIEW_SUFFIXES, ...exports.ROUTE_SUFFIXES, ...LAZY_MODULE_SUFFIXES].join('|')})\.${exports.SOURCE_EXTENSION}$`);
141
198
  const EXTENSION = new RegExp(String.raw `\.${exports.SOURCE_EXTENSION}$`);
142
199
  /**
143
200
  * SvelteKit names a route file by its role and the route by its folder: `+page.svelte` in every
144
201
  * one of them. Left alone, three screens came out as three rows called `+page.svelte`.
145
202
  */
146
- const ROLE_FILE = /^\+/;
203
+ const ROLE_PREFIX = '+';
147
204
  /** Folders that are the root of the routes rather than one route: there the file name is all there is. */
148
205
  const ROUTE_ROOTS = new Set(['routes', 'pages', 'app', 'src']);
206
+ /** A route parameter written into a file name: `[id]`, `[...slug]`, `_id` (Nuxt 2), `$id` (Remix). */
207
+ const DYNAMIC_SEGMENT = /^(?:\[[^\]]+\]|[_$]\w+)$/;
208
+ /**
209
+ * A screen known only by its chunk — a folder read without source maps — named the way its bundler
210
+ * named the chunk, without the hash: `Article-BZRh73np.js` → `Article`. Rollup, Vite and esbuild
211
+ * name a chunk after the module it starts at and append eight characters, and on a real Vue build the
212
+ * table read `Article-BZRh73np` while a signal said every row was a hash nobody could read. A name
213
+ * that is only a hash stays one, and so does Angular's `chunk-`, which names nothing.
214
+ */
215
+ const chunkLabel = (chunk) => {
216
+ const stem = (0, exports.baseName)(chunk).replace(/\.m?js$/i, '');
217
+ // `-` and eight characters is Rollup, Vite and esbuild; `.` and a hex hash is Rollup 1 and the
218
+ // bundlers of its time — Sapper's table read `index.e902f999` and `index.62e5829d`.
219
+ const named = stem.replace(/-[\w-]{8}$/, '').replace(/\.[\da-f]{8,}$/i, '');
220
+ // A letter names nothing either: Stencil calls every chunk `p-3b66a627`, and its polyfills
221
+ // were listed as "p, p, p".
222
+ return named.length > 1 && named !== stem && named.toLowerCase() !== 'chunk' ? named : stem;
223
+ };
224
+ exports.chunkLabel = chunkLabel;
149
225
  /**
150
226
  * Readable screen name from the file it originates from.
151
227
  * `features/users/users.page.ts` -> `users`
152
228
  * `src/routes/orders/+page.svelte` -> `orders`
229
+ *
230
+ * @param chunk the chunk the screen starts at. When it is the source too, the folder said nothing
231
+ * finer — no source map — and the chunk's own name is all there is: see `chunkLabel`.
153
232
  */
154
- const screenLabel = (source) => {
155
- const name = (0, exports.baseName)(source).replace(SUFFIX, '').replace(EXTENSION, '') || (0, exports.baseName)(source);
156
- if (!ROLE_FILE.test(name)) {
157
- return name;
233
+ const screenLabel = (source, chunk) => {
234
+ if (source === chunk) {
235
+ return (0, exports.chunkLabel)(chunk);
158
236
  }
237
+ const name = (0, exports.baseName)(source).replace(SUFFIX, '').replace(EXTENSION, '') || (0, exports.baseName)(source);
159
238
  const folder = source.split('/').at(-2) ?? '';
160
- return folder && !ROUTE_ROOTS.has(folder) ? folder : name.slice(1);
239
+ const routed = folder && !ROUTE_ROOTS.has(folder);
240
+ if (name.startsWith(ROLE_PREFIX)) {
241
+ return routed ? folder : name.slice(1);
242
+ }
243
+ // File-system routing names a route by its folder too: `pages/movie/index.vue` is `movie`, and
244
+ // `pages/movie/[id].vue` is `movie/[id]`. On their own, a Nuxt app's table had `index` twice
245
+ // and `[id]` twice, and its gate messages said "Screen index downloads 191 kB" about two
246
+ // different screens.
247
+ // Under a parameter, the route is the path: `genre/[no]/movie` is not the `movie` screen. Asked
248
+ // before `index`, or Sapper's `profile/[user]/index.svelte` came out as `[user]` next to its own
249
+ // sibling `profile/[user]/[view]`.
250
+ if (routed && DYNAMIC_SEGMENT.test(folder)) {
251
+ return routeOf(source) ?? name;
252
+ }
253
+ if (name === 'index' && routed) {
254
+ return folder;
255
+ }
256
+ return DYNAMIC_SEGMENT.test(name) && routed ? `${folder}/${name}` : name;
161
257
  };
162
258
  exports.screenLabel = screenLabel;
259
+ /** The path of a file below the root of the routes, without its extension or a trailing `index`. */
260
+ const routeOf = (source) => {
261
+ const parts = source.replace(SUFFIX, '').replace(EXTENSION, '').split('/');
262
+ const root = parts.findLastIndex(part => ROUTE_ROOTS.has(part));
263
+ const route = parts.slice(root + 1).filter(part => part !== 'index');
264
+ return route.length > 0 ? route.join('/') : null;
265
+ };
266
+ /**
267
+ * The same labels made unique where two screens share one, by the folders above each, up to the
268
+ * root of the routes: `movie` and `genre/[no]/movie`. One that is unique already is left as it is.
269
+ */
270
+ const uniqueLabels = (screens, isChunk = () => false) => {
271
+ const count = new Map();
272
+ for (const screen of screens) {
273
+ count.set(screen.label, (count.get(screen.label) ?? 0) + 1);
274
+ }
275
+ return screens.map(screen => {
276
+ if ((count.get(screen.label) ?? 0) < 2) {
277
+ return screen;
278
+ }
279
+ // A screen known only by its chunk has no folders to tell it apart by, only its hash: two
280
+ // Sapper `[slug]` read `client/[slug].df9e6d95`, the folder of the build and not of a route.
281
+ const longer = isChunk(screen.source)
282
+ ? (0, exports.baseName)(screen.source).replace(/\.m?js$/i, '')
283
+ : routeOf(screen.source);
284
+ return longer && longer !== screen.label ? { ...screen, label: longer } : screen;
285
+ });
286
+ };
287
+ exports.uniqueLabels = uniqueLabels;
163
288
  /**
164
289
  * A path shortened to fit a table cell by dropping middle folders, never by cutting a word in two.
165
290
  *
@@ -202,3 +327,32 @@ exports.elidePath = elidePath;
202
327
  */
203
328
  const formatDelta = (diff) => `${diff >= 0 ? '+' : '−'}${(0, exports.formatBytes)(Math.abs(diff))}`;
204
329
  exports.formatDelta = formatDelta;
330
+ /**
331
+ * Whether a pattern of `loadline.json` names a file: the whole name, or with `*` standing for any
332
+ * text — the hash, a folder. Matched against the file name and against its path in the folder, so `client.*.js` and
333
+ * `build/p-*.js` both work. Matched by hand rather than turned into a regular expression: the
334
+ * pattern is somebody's text, and a regular expression built from it is one more thing to get wrong.
335
+ */
336
+ const namedBy = (pattern, path) => [(0, exports.baseName)(path), path].some(name => (0, exports.matchesStars)(pattern, name));
337
+ exports.namedBy = namedBy;
338
+ /** Whether `text` is `pattern` whole, with `*` standing for any text. The matcher `namedBy` uses. */
339
+ const matchesStars = (pattern, text) => {
340
+ const [first = '', ...rest] = pattern.split('*');
341
+ if (rest.length === 0) {
342
+ return text === first;
343
+ }
344
+ const last = rest.pop() ?? '';
345
+ if (!text.startsWith(first) || !text.endsWith(last) || text.length < first.length + last.length) {
346
+ return false;
347
+ }
348
+ let at = first.length;
349
+ for (const part of rest) {
350
+ const found = text.indexOf(part, at);
351
+ if (found === -1) {
352
+ return false;
353
+ }
354
+ at = found + part.length;
355
+ }
356
+ return at <= text.length - last.length;
357
+ };
358
+ exports.matchesStars = matchesStars;
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ /**
3
+ * Yours or theirs: the one place that decides whether a file is the project's own code or a
4
+ * dependency, and what the dependency is called.
5
+ *
6
+ * It was a dozen `includes('node_modules')` across the analysis, the scan and the command, each
7
+ * written for the case in front of it, and `packageOf` beside them. Two builds showed where that
8
+ * ends. Sapper keeps an application's own modules in `src/node_modules/` so they import by name —
9
+ * the RealWorld app's `api.js` and `utils.js` — and they came out as packages. And a monorepo's
10
+ * `packages/ui` is a dependency of the app in every sense but where it sits, and came out as yours.
11
+ *
12
+ * The default is `node_modules/`, with Sapper's convention read; `build.own` and
13
+ * `build.dependencies` of `loadline.json` say the rest for a project (`setOwnership`). Like the
14
+ * language of the figures, it is set once from outside — by the command when it reads the file, by
15
+ * the page when one is dropped — because it is asked from everywhere a path is named.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.declaredPackage = exports.isOwnFile = exports.isDependency = exports.setOwnership = void 0;
19
+ const rules = { own: [], dependencies: [] };
20
+ /**
21
+ * `packages/*` matches `packages/ui/src/x.ts` and captures `packages/ui`: `*` is one segment or a
22
+ * part of one, `**` any number of them. The capture is what names a dependency.
23
+ */
24
+ const ruleOf = (pattern) => {
25
+ const source = pattern
26
+ .replace(/^\.?\//, '')
27
+ .replace(/\/+$/, '')
28
+ .split('**')
29
+ .map(part => part
30
+ .split('*')
31
+ .map(text => text.replaceAll(/[$()+.?[\\\]^{|}]/g, String.raw `\$&`))
32
+ .join('[^/]*'))
33
+ .join('.*');
34
+ return { pattern, at: new RegExp(`^(${source})(?:/|$)`) };
35
+ };
36
+ /** The project's own words on it, from `build.own` and `build.dependencies`; `null` clears them. */
37
+ const setOwnership = (hints) => {
38
+ rules.own = (hints?.own ?? []).map(pattern => ruleOf(pattern));
39
+ rules.dependencies = (hints?.dependencies ?? []).map(pattern => ruleOf(pattern));
40
+ };
41
+ exports.setOwnership = setOwnership;
42
+ /**
43
+ * Sapper's convention: `src/node_modules/` holds the application's own modules, importable by name,
44
+ * and `src/node_modules/@sapper/` the runtime Sapper writes there, which is the framework's. A
45
+ * package's own `node_modules` further in is a package again.
46
+ */
47
+ const isSapperOwn = (path) => {
48
+ const marker = 'src/node_modules/';
49
+ const at = path.indexOf(marker);
50
+ if (at === -1 || (at > 0 && path[at - 1] !== '/')) {
51
+ return false;
52
+ }
53
+ const inside = path.slice(at + marker.length);
54
+ return !inside.startsWith('@sapper/') && !inside.includes('node_modules/');
55
+ };
56
+ /** The part of the path a `build.dependencies` pattern matched, when one did. */
57
+ const declaredDependency = (path) => {
58
+ for (const rule of rules.dependencies) {
59
+ const match = rule.at.exec(path);
60
+ if (match?.[1]) {
61
+ return match[1];
62
+ }
63
+ }
64
+ return null;
65
+ };
66
+ /** Whether a source file is a dependency rather than the project's own code. */
67
+ const isDependency = (path) => {
68
+ // `build.own` first: it wins over a dependency pattern that matches the same file.
69
+ const own = rules.own.some(rule => rule.at.test(path));
70
+ return !own && (declaredDependency(path) !== null || (path.includes('node_modules/') && !isSapperOwn(path)));
71
+ };
72
+ exports.isDependency = isDependency;
73
+ /** The opposite, for the many places that ask it that way round. */
74
+ const isOwnFile = (path) => !(0, exports.isDependency)(path);
75
+ exports.isOwnFile = isOwnFile;
76
+ /**
77
+ * What a `build.dependencies` pattern makes of a path: the package name — the last segment of what
78
+ * the pattern matched, or the last two when they are a scope and a name — and the rest. `null`
79
+ * when no pattern matched.
80
+ */
81
+ const declaredPackage = (path) => {
82
+ const prefix = declaredDependency(path);
83
+ if (!prefix || rules.own.some(rule => rule.at.test(path))) {
84
+ return null;
85
+ }
86
+ const segments = prefix.split('/');
87
+ const scoped = segments.at(-2)?.startsWith('@') === true;
88
+ return {
89
+ name: segments.slice(scoped ? -2 : -1).join('/'),
90
+ rest: path.slice(prefix.length).replace(/^\//, ''),
91
+ };
92
+ };
93
+ exports.declaredPackage = declaredPackage;