package-build-stats 8.2.4 → 8.2.6

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.
@@ -6,6 +6,7 @@ type MakeRspackConfigOptions = {
6
6
  debug?: boolean;
7
7
  minify?: boolean;
8
8
  entry: Entry;
9
+ outputPath: string;
9
10
  };
10
- export default function makeRspackConfig({ packageName: _packageName, entry, externals, debug: _debug, minify, }: MakeRspackConfigOptions): Configuration;
11
+ export default function makeRspackConfig({ packageName: _packageName, entry, externals, debug: _debug, minify, outputPath, }: MakeRspackConfigOptions): Configuration;
11
12
  export {};
@@ -3,7 +3,7 @@ import escapeRegex from 'escape-string-regexp';
3
3
  import rspack from '@rspack/core';
4
4
  import { createRequire } from 'module';
5
5
  const require = createRequire(import.meta.url);
6
- export default function makeRspackConfig({ packageName: _packageName, entry, externals, debug: _debug, minify = true, }) {
6
+ export default function makeRspackConfig({ packageName: _packageName, entry, externals, debug: _debug, minify = true, outputPath, }) {
7
7
  const externalsRegex = makeExternalsRegex(externals.externalPackages);
8
8
  const isExternalRequest = (request) => {
9
9
  const isPeerDep = externals.externalPackages.length
@@ -154,6 +154,7 @@ export default function makeRspackConfig({ packageName: _packageName, entry, ext
154
154
  },
155
155
  output: {
156
156
  filename: '[name].bundle.js',
157
+ path: outputPath,
157
158
  },
158
159
  externals: ({ request }, callback) => isExternalRequest(request || '')
159
160
  ? callback(undefined, 'commonjs ' + request)
@@ -7,6 +7,7 @@ type CompilePackageArgs = {
7
7
  entry: Entry;
8
8
  debug?: boolean;
9
9
  minify?: boolean;
10
+ outputPath: string;
10
11
  };
11
12
  type CompilePackageReturn = {
12
13
  stats: Stats;
@@ -36,7 +37,7 @@ type BuildPackageResultWithIgnored = BuildPackageResult & {
36
37
  declare function getCompilationErrors(stats: Stats): import("@rspack/core").RspackError[];
37
38
  declare const BuildUtils: {
38
39
  createEntryPoint(packageName: string, installPath: string, options: CreateEntryPointOptions): string;
39
- compilePackage({ name, entry, externals, debug, minify, }: CompilePackageArgs): Promise<CompilePackageReturn>;
40
+ compilePackage({ name, entry, externals, debug, minify, outputPath, }: CompilePackageArgs): Promise<CompilePackageReturn>;
40
41
  _parseMissingModules(errors: ReturnType<typeof getCompilationErrors>): string[];
41
42
  buildPackage({ name, installPath, externals, options, }: BuildPackageArgs): Promise<{
42
43
  assets: {
@@ -1,4 +1,5 @@
1
1
  import path from 'path';
2
+ import config from '../config/config.js';
2
3
  import { rspack } from '@rspack/core';
3
4
  import isValidNPMName from 'is-valid-npm-name';
4
5
  import { gzipSync } from 'zlib';
@@ -14,6 +15,18 @@ function notEmpty(value) {
14
15
  function getCompilationErrors(stats) {
15
16
  return [...stats.compilation.errors].filter(notEmpty).flat();
16
17
  }
18
+ function closeCompiler(compiler) {
19
+ return new Promise((resolve, reject) => {
20
+ compiler.close(error => {
21
+ if (error) {
22
+ reject(error);
23
+ }
24
+ else {
25
+ resolve();
26
+ }
27
+ });
28
+ });
29
+ }
17
30
  const BuildUtils = {
18
31
  createEntryPoint(packageName, installPath, options) {
19
32
  const entryPath = path.join(installPath, options.entryFilename || 'index.js');
@@ -48,7 +61,7 @@ const BuildUtils = {
48
61
  throw new EntryPointError(err);
49
62
  }
50
63
  },
51
- compilePackage({ name, entry, externals, debug, minify, }) {
64
+ compilePackage({ name, entry, externals, debug, minify, outputPath, }) {
52
65
  const startTime = performance.now();
53
66
  const options = makeRspackConfig({
54
67
  packageName: name,
@@ -56,20 +69,27 @@ const BuildUtils = {
56
69
  externals,
57
70
  debug,
58
71
  minify,
72
+ outputPath,
59
73
  });
60
74
  const compiler = rspack(options);
61
- return new Promise(resolve => {
62
- compiler.run((error, stats) => {
63
- if (!stats) {
64
- throw new Error('stats is null');
65
- }
66
- resolve({ stats, error });
67
- if (error) {
68
- console.error(error);
69
- Telemetry.compilePackage(name, false, startTime, {}, error);
75
+ return new Promise((resolve, reject) => {
76
+ compiler.run(async (error, stats) => {
77
+ try {
78
+ if (!stats) {
79
+ throw new Error('stats is null');
80
+ }
81
+ await closeCompiler(compiler);
82
+ if (error) {
83
+ console.error(error);
84
+ Telemetry.compilePackage(name, false, startTime, {}, error);
85
+ }
86
+ else {
87
+ Telemetry.compilePackage(name, true, startTime, {});
88
+ }
89
+ resolve({ stats, error });
70
90
  }
71
- else {
72
- Telemetry.compilePackage(name, true, startTime, {});
91
+ catch (closeError) {
92
+ reject(closeError);
73
93
  }
74
94
  });
75
95
  });
@@ -101,6 +121,7 @@ const BuildUtils = {
101
121
  },
102
122
  async buildPackage({ name, installPath, externals, options, }) {
103
123
  var _a;
124
+ const outputPath = config.tmp;
104
125
  let entry = {};
105
126
  if (options.splitCustomImports) {
106
127
  if (!options.customImports || !options.customImports.length) {
@@ -126,6 +147,7 @@ const BuildUtils = {
126
147
  externals,
127
148
  debug: options.debug,
128
149
  minify: options.minify,
150
+ outputPath,
129
151
  });
130
152
  const jsonStatsStartTime = performance.now();
131
153
  let jsonStats = stats.toJson({
@@ -177,7 +199,7 @@ const BuildUtils = {
177
199
  }
178
200
  else {
179
201
  const getAssetStats = async (asset) => {
180
- const bundle = path.join(process.cwd(), 'dist', asset.name);
202
+ const bundle = path.join(outputPath, asset.name);
181
203
  const bundleContents = await fs.promises.readFile(bundle);
182
204
  const gzip = gzipSync(bundleContents, {}).length;
183
205
  const matches = asset.name.match(/(.+?)\.bundle\.(.+)$/);
@@ -198,7 +220,8 @@ const BuildUtils = {
198
220
  var _a;
199
221
  return !((_a = asset.chunkNames) === null || _a === void 0 ? void 0 : _a.some(name => name === 'runtime' ||
200
222
  (typeof name === 'string' && name.startsWith('runtime~'))));
201
- }).filter(asset => !asset.name.endsWith('LICENSE.txt')).map(getAssetStats)) || [];
223
+ }).filter(asset => typeof asset.name === 'string' &&
224
+ !asset.name.endsWith('LICENSE.txt')).map(getAssetStats)) || [];
202
225
  const assetStats = await Promise.all(assetStatsPromises);
203
226
  Telemetry.assetsGZIPParseTime(name, performance.now());
204
227
  let dependencySizeResults = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "package-build-stats",
3
- "version": "8.2.4",
3
+ "version": "8.2.6",
4
4
  "type": "module",
5
5
  "author": "Shubham Kanodia <shubham.kanodia10@gmail.com>",
6
6
  "repository": "https://github.com/pastelsky/package-build-stats",
@@ -61,6 +61,7 @@
61
61
  "@types/rimraf": "^4.0.5",
62
62
  "@types/sanitize-filename": "^1.6.3",
63
63
  "@types/semver": "^7",
64
+ "@types/server": "^1",
64
65
  "@types/yargs": "^17.0.34",
65
66
  "@vitest/coverage-v8": "^4.0.6",
66
67
  "@vitest/ui": "4.0.6",
@@ -105,6 +106,7 @@
105
106
  "sanitize-filename": "^1.6.3",
106
107
  "sass": "^1.69.0",
107
108
  "sass-loader": "^14.0.0",
109
+ "server": "^1.0.42",
108
110
  "svelte": "^4.0.0",
109
111
  "svelte-loader": "^3.1.0",
110
112
  "terser": "^5.44.0",
@@ -15,6 +15,7 @@ type MakeRspackConfigOptions = {
15
15
  debug?: boolean
16
16
  minify?: boolean
17
17
  entry: Entry
18
+ outputPath: string
18
19
  }
19
20
 
20
21
  export default function makeRspackConfig({
@@ -23,6 +24,7 @@ export default function makeRspackConfig({
23
24
  externals,
24
25
  debug: _debug,
25
26
  minify = true,
27
+ outputPath,
26
28
  }: MakeRspackConfigOptions): Configuration {
27
29
  const externalsRegex = makeExternalsRegex(externals.externalPackages)
28
30
  const isExternalRequest = (request: string) => {
@@ -176,6 +178,7 @@ export default function makeRspackConfig({
176
178
  },
177
179
  output: {
178
180
  filename: '[name].bundle.js',
181
+ path: outputPath,
179
182
  },
180
183
  externals: ({ request }, callback) =>
181
184
  isExternalRequest(request || '')
@@ -1,4 +1,5 @@
1
1
  import path from 'path'
2
+ import config from '../config/config.js'
2
3
  import { Entry, rspack } from '@rspack/core'
3
4
  import isValidNPMName from 'is-valid-npm-name'
4
5
  import { gzipSync } from 'zlib'
@@ -28,6 +29,7 @@ type CompilePackageArgs = {
28
29
  entry: Entry
29
30
  debug?: boolean
30
31
  minify?: boolean
32
+ outputPath: string
31
33
  }
32
34
 
33
35
  type CompilePackageReturn = {
@@ -35,6 +37,8 @@ type CompilePackageReturn = {
35
37
  error: Error | null
36
38
  }
37
39
 
40
+ type Compiler = NonNullable<ReturnType<typeof rspack>>
41
+
38
42
  type BuildPackageArgs = {
39
43
  name: string
40
44
  installPath: string
@@ -70,6 +74,18 @@ function getCompilationErrors(stats: Stats) {
70
74
  return [...stats.compilation.errors].filter(notEmpty).flat()
71
75
  }
72
76
 
77
+ function closeCompiler(compiler: Compiler) {
78
+ return new Promise<void>((resolve, reject) => {
79
+ compiler.close(error => {
80
+ if (error) {
81
+ reject(error)
82
+ } else {
83
+ resolve()
84
+ }
85
+ })
86
+ })
87
+ }
88
+
73
89
  const BuildUtils = {
74
90
  createEntryPoint(
75
91
  packageName: string,
@@ -119,6 +135,7 @@ const BuildUtils = {
119
135
  externals,
120
136
  debug,
121
137
  minify,
138
+ outputPath,
122
139
  }: CompilePackageArgs) {
123
140
  const startTime = performance.now()
124
141
 
@@ -128,22 +145,30 @@ const BuildUtils = {
128
145
  externals,
129
146
  debug,
130
147
  minify,
148
+ outputPath,
131
149
  })
132
150
 
133
151
  const compiler = rspack(options)
134
152
 
135
- return new Promise<CompilePackageReturn>(resolve => {
136
- compiler.run((error, stats) => {
137
- if (!stats) {
138
- throw new Error('stats is null')
139
- }
140
- resolve({ stats, error })
141
-
142
- if (error) {
143
- console.error(error)
144
- Telemetry.compilePackage(name, false, startTime, {}, error)
145
- } else {
146
- Telemetry.compilePackage(name, true, startTime, {})
153
+ return new Promise<CompilePackageReturn>((resolve, reject) => {
154
+ compiler.run(async (error, stats) => {
155
+ try {
156
+ if (!stats) {
157
+ throw new Error('stats is null')
158
+ }
159
+
160
+ await closeCompiler(compiler)
161
+
162
+ if (error) {
163
+ console.error(error)
164
+ Telemetry.compilePackage(name, false, startTime, {}, error)
165
+ } else {
166
+ Telemetry.compilePackage(name, true, startTime, {})
167
+ }
168
+
169
+ resolve({ stats, error })
170
+ } catch (closeError) {
171
+ reject(closeError)
147
172
  }
148
173
  })
149
174
  })
@@ -193,6 +218,7 @@ const BuildUtils = {
193
218
  externals,
194
219
  options,
195
220
  }: BuildPackageArgs) {
221
+ const outputPath = config.tmp
196
222
  let entry: any = {}
197
223
 
198
224
  if (options.splitCustomImports) {
@@ -219,6 +245,7 @@ const BuildUtils = {
219
245
  externals,
220
246
  debug: options.debug,
221
247
  minify: options.minify,
248
+ outputPath,
222
249
  })
223
250
 
224
251
  const jsonStatsStartTime = performance.now()
@@ -279,7 +306,7 @@ const BuildUtils = {
279
306
  }
280
307
  } else {
281
308
  const getAssetStats = async (asset: RspackStatsAsset) => {
282
- const bundle = path.join(process.cwd(), 'dist', asset.name)
309
+ const bundle = path.join(outputPath, asset.name)
283
310
  const bundleContents = await fs.promises.readFile(bundle)
284
311
 
285
312
  const gzip = gzipSync(bundleContents, {}).length
@@ -313,7 +340,11 @@ const BuildUtils = {
313
340
  (typeof name === 'string' && name.startsWith('runtime~')),
314
341
  ),
315
342
  )
316
- .filter(asset => !asset.name.endsWith('LICENSE.txt'))
343
+ .filter(
344
+ asset =>
345
+ typeof asset.name === 'string' &&
346
+ !asset.name.endsWith('LICENSE.txt'),
347
+ )
317
348
  .map(getAssetStats) || []
318
349
  const assetStats = await Promise.all(assetStatsPromises)
319
350
  Telemetry.assetsGZIPParseTime(name, performance.now())