package-build-stats 7.3.8 → 8.0.0-beta.3

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 (74) hide show
  1. package/.parcelrc +37 -0
  2. package/build/common.types.d.ts +3 -8
  3. package/build/common.types.d.ts.map +1 -0
  4. package/build/common.types.js +1 -0
  5. package/build/common.types.js.map +1 -0
  6. package/build/config/{config.d.ts → index.d.ts} +2 -0
  7. package/build/config/index.d.ts.map +1 -0
  8. package/build/config/{config.js → index.js} +2 -0
  9. package/build/config/index.js.map +1 -0
  10. package/build/errors/CustomError.d.ts +1 -0
  11. package/build/errors/CustomError.d.ts.map +1 -0
  12. package/build/errors/CustomError.js +1 -0
  13. package/build/errors/CustomError.js.map +1 -0
  14. package/build/fixed/fixed/parseReference.js +5388 -0
  15. package/build/fixed/parseReference.js +762 -727
  16. package/build/getPackageExportSizes.d.ts +5 -29
  17. package/build/getPackageExportSizes.d.ts.map +1 -0
  18. package/build/getPackageExportSizes.js +18 -10
  19. package/build/getPackageExportSizes.js.map +1 -0
  20. package/build/getPackageStats.d.ts +13 -54
  21. package/build/getPackageStats.d.ts.map +1 -0
  22. package/build/getPackageStats.js +30 -7
  23. package/build/getPackageStats.js.map +1 -0
  24. package/build/getParseTime.d.ts +1 -0
  25. package/build/getParseTime.d.ts.map +1 -0
  26. package/build/getParseTime.js +5 -4
  27. package/build/getParseTime.js.map +1 -0
  28. package/build/index.d.ts +1 -0
  29. package/build/index.d.ts.map +1 -0
  30. package/build/index.js +1 -0
  31. package/build/index.js.map +1 -0
  32. package/build/utils/build.utils.d.ts +39 -69
  33. package/build/utils/build.utils.d.ts.map +1 -0
  34. package/build/utils/build.utils.js +177 -159
  35. package/build/utils/build.utils.js.map +1 -0
  36. package/build/utils/common.utils.d.ts +22 -0
  37. package/build/utils/common.utils.d.ts.map +1 -0
  38. package/build/utils/common.utils.js +94 -1
  39. package/build/utils/common.utils.js.map +1 -0
  40. package/build/utils/exports.utils.d.ts +1 -0
  41. package/build/utils/exports.utils.d.ts.map +1 -0
  42. package/build/utils/exports.utils.js +35 -23
  43. package/build/utils/exports.utils.js.map +1 -0
  44. package/build/utils/installation.utils.d.ts +1 -0
  45. package/build/utils/installation.utils.d.ts.map +1 -0
  46. package/build/utils/installation.utils.js +25 -7
  47. package/build/utils/installation.utils.js.map +1 -0
  48. package/build/utils/telemetry.utils.d.ts +1 -2
  49. package/build/utils/telemetry.utils.d.ts.map +1 -0
  50. package/build/utils/telemetry.utils.js +7 -20
  51. package/build/utils/telemetry.utils.js.map +1 -0
  52. package/package.json +75 -73
  53. package/src/common.types.ts +3 -10
  54. package/src/config/{config.ts → index.ts} +1 -0
  55. package/src/errors/CustomError.d.ts +42 -0
  56. package/src/errors/CustomError.js +81 -0
  57. package/src/errors/CustomError.js.map +1 -0
  58. package/src/fixed/parseReference.js +762 -727
  59. package/src/getPackageExportSizes.ts +33 -14
  60. package/src/getPackageStats.ts +55 -16
  61. package/src/typings/is-valid-npm-name.d.ts +3 -0
  62. package/src/utils/build.utils.ts +258 -230
  63. package/src/utils/common.utils.ts +138 -0
  64. package/src/utils/exports.utils.ts +34 -21
  65. package/src/utils/installation.utils.ts +28 -5
  66. package/src/utils/telemetry.utils.ts +0 -21
  67. package/LICENSE +0 -21
  68. package/README.md +0 -67
  69. package/build/config/makeWebpackConfig.d.ts +0 -11
  70. package/build/config/makeWebpackConfig.js +0 -225
  71. package/build/getDependencySizeTree.d.ts +0 -6
  72. package/build/getDependencySizeTree.js +0 -238
  73. package/src/config/makeWebpackConfig.ts +0 -251
  74. package/src/getDependencySizeTree.ts +0 -266
@@ -1,251 +0,0 @@
1
- import autoprefixer from 'autoprefixer'
2
- import TerserPlugin from 'terser-webpack-plugin'
3
- import MiniCssExtractPlugin from 'mini-css-extract-plugin'
4
- import CssoWebpackPlugin from 'csso-webpack-plugin'
5
- import WriteFilePlugin from 'write-file-webpack-plugin'
6
-
7
- const log = require('debug')('bp:webpack')
8
- import escapeRegex from 'escape-string-regexp'
9
- import builtinModules from 'builtin-modules'
10
- import webpack, { Entry } from 'webpack'
11
- import { ESBuildMinifyPlugin } from 'esbuild-loader'
12
- // @ts-ignore
13
- import VueLoaderPlugin from 'vue-loader/lib/plugin'
14
-
15
- import { Externals } from '../common.types'
16
-
17
- type MakeWebpackConfigOptions = {
18
- packageName: string
19
- externals: Externals
20
- debug?: boolean
21
- entry: string | string[] | Entry
22
- minifier: 'esbuild' | 'terser'
23
- }
24
-
25
- type NodeBuiltIn = {
26
- [key: string]: boolean | 'empty'
27
- }
28
-
29
- export default function makeWebpackConfig({
30
- packageName,
31
- entry,
32
- externals,
33
- debug,
34
- minifier,
35
- }: MakeWebpackConfigOptions): webpack.Configuration {
36
- const externalsRegex = makeExternalsRegex(externals.externalPackages)
37
- const isExternalRequest = (request: string) => {
38
- const isPeerDep = externals.externalPackages.length
39
- ? externalsRegex.test(request)
40
- : false
41
- const isBuiltIn = externals.externalBuiltIns.includes(request)
42
- return isPeerDep || isBuiltIn
43
- }
44
-
45
- log('external packages %o', externalsRegex)
46
-
47
- const builtInNode: NodeBuiltIn = {}
48
- builtinModules.forEach(mod => {
49
- builtInNode[mod] = 'empty'
50
- })
51
-
52
- builtInNode['setImmediate'] = false
53
- builtInNode['console'] = false
54
- builtInNode['process'] = false
55
- builtInNode['Buffer'] = false
56
-
57
- // Don't mark an import as built in if it is the name of the package itself
58
- // eg. `events`
59
- if (builtInNode[packageName]) {
60
- builtInNode[packageName] = false
61
- }
62
-
63
- // @ts-ignore
64
- // @ts-ignore
65
- // @ts-ignore
66
- return {
67
- entry: entry,
68
- mode: 'production',
69
- // bail: true,
70
- optimization: {
71
- namedChunks: true,
72
- runtimeChunk: { name: 'runtime' },
73
- minimize: true,
74
- splitChunks: {
75
- cacheGroups: {
76
- styles: {
77
- name: 'main',
78
- test: /\.css$/,
79
- chunks: 'all',
80
- enforce: true,
81
- },
82
- },
83
- },
84
- // @ts-ignore: Appears that the library CssoWebpackPlugin might have incorrect definitions
85
- minimizer: [
86
- ...(minifier === 'terser'
87
- ? [
88
- new TerserPlugin({
89
- parallel: true,
90
- terserOptions: {
91
- ie8: false,
92
- output: {
93
- comments: false,
94
- },
95
- },
96
- }),
97
- ]
98
- : [
99
- new ESBuildMinifyPlugin({
100
- target: 'esnext',
101
- }),
102
- ]),
103
- new CssoWebpackPlugin({ restructure: false }),
104
- ],
105
- },
106
- plugins: [
107
- new webpack.IgnorePlugin(/^electron$/),
108
- new VueLoaderPlugin(),
109
- new MiniCssExtractPlugin({
110
- // Options similar to the same options in webpackOptions.output
111
- // both options are optional
112
- filename: '[name].bundle.css',
113
- chunkFilename: '[id].bundle.css',
114
- }),
115
- ...(debug ? [new WriteFilePlugin()] : []),
116
- ],
117
- resolve: {
118
- modules: ['node_modules'],
119
- cacheWithContext: false,
120
- extensions: [
121
- '.web.mjs',
122
- '.mjs',
123
- '.web.js',
124
- '.js',
125
- '.mjs',
126
- '.json',
127
- '.css',
128
- '.sass',
129
- '.scss',
130
- ],
131
- mainFields: ['browser', 'module', 'main', 'style'],
132
- },
133
- module: {
134
- rules: [
135
- {
136
- test: /\.css$/,
137
- use: [MiniCssExtractPlugin.loader, require.resolve('css-loader')],
138
- },
139
- // see https://github.com/apollographql/react-apollo/issues/1737
140
- {
141
- type: 'javascript/auto',
142
- test: /\.mjs$/,
143
- use: [],
144
- },
145
- {
146
- test: /\.js$/,
147
- loader: [
148
- // support CLI tools that start with a #!/usr/bin/node
149
- require.resolve('shebang-loader'),
150
- // ESBuild Minifier doesn't auto-remove license comments from code
151
- // So, we break ESBuild's heuristic for license comments match. See github.com/privatenumber/esbuild-loader/issues/87
152
- {
153
- loader: require.resolve('string-replace-loader'),
154
- options: {
155
- multiple: [
156
- { search: '@license', replace: '@silence' },
157
- { search: /\/\/!/g, replace: '//' },
158
- { search: /\/\*!/g, replace: '/*' },
159
- ],
160
- },
161
- },
162
- ],
163
- },
164
-
165
- {
166
- test: /\.(html|svelte)$/,
167
- use: {
168
- loader: require.resolve('svelte-loader'),
169
- options: {
170
- emitCss: true,
171
- },
172
- },
173
- },
174
- {
175
- test: /\.vue$/,
176
- loader: require.resolve('vue-loader'),
177
- },
178
- {
179
- test: /\.(scss|sass)$/,
180
- loader: [
181
- MiniCssExtractPlugin.loader,
182
- require.resolve('css-loader'),
183
- {
184
- loader: require.resolve('postcss-loader'),
185
- options: {
186
- plugins: () => [autoprefixer()],
187
- },
188
- },
189
- require.resolve('sass-loader'),
190
- ],
191
- },
192
- {
193
- test: /\.less$/,
194
- loader: [
195
- MiniCssExtractPlugin.loader,
196
- require.resolve('css-loader'),
197
- {
198
- loader: require.resolve('postcss-loader'),
199
- options: {
200
- plugins: () => [
201
- autoprefixer({
202
- browsers: [
203
- 'last 5 Chrome versions',
204
- 'last 5 Firefox versions',
205
- 'Safari >= 8',
206
- 'Explorer >= 10',
207
- 'edge >= 12',
208
- ],
209
- }),
210
- ],
211
- },
212
- },
213
- {
214
- loader: require.resolve('less-loader'),
215
- options: {
216
- webpackImporter: true,
217
- },
218
- },
219
- ],
220
- },
221
- {
222
- test: /\.(woff|woff2|eot|ttf|svg|png|jpeg|jpg|gif|webp)$/,
223
- loader: require.resolve('file-loader'),
224
- options: {
225
- name: '[name].bundle.[ext]',
226
- emitFile: true,
227
- },
228
- },
229
- ],
230
- },
231
- node: builtInNode,
232
- output: {
233
- filename: 'bundle.js',
234
- pathinfo: false,
235
- },
236
- externals: (context, request, callback) =>
237
- isExternalRequest(request)
238
- ? callback(null, 'commonjs ' + request)
239
- : callback(),
240
- }
241
- }
242
-
243
- function makeExternalsRegex(externals: string[]) {
244
- let externalsRegex = externals
245
- .map(dep => `^${escapeRegex(dep)}$|^${escapeRegex(dep)}\\/`)
246
- .join('|')
247
-
248
- externalsRegex = `(${externalsRegex})`
249
-
250
- return new RegExp(externalsRegex)
251
- }
@@ -1,266 +0,0 @@
1
- import webpack from 'webpack'
2
- import path from 'path'
3
- import Terser from 'terser'
4
- import * as esbuild from 'esbuild'
5
- import { MinifyError } from './errors/CustomError'
6
- import Telemetry from './utils/telemetry.utils'
7
- import { performance } from 'perf_hooks'
8
-
9
- /**
10
- * A fork of `webpack-bundle-size-analyzer`.
11
- * https://github.com/robertknight/webpack-bundle-size-analyzer
12
- */
13
-
14
- function modulePath(identifier: string) {
15
- // the format of module paths is
16
- // '(<loader expression>!)?/path/to/module.js'
17
- let loaderRegex = /.*!/
18
- return identifier.replace(loaderRegex, '')
19
- }
20
-
21
- function getByteLen(normal_val: string) {
22
- // Force string type
23
- normal_val = String(normal_val)
24
-
25
- let byteLen = 0
26
- for (let i = 0; i < normal_val.length; i++) {
27
- const c = normal_val.charCodeAt(i)
28
- byteLen +=
29
- c < 1 << 7
30
- ? 1
31
- : c < 1 << 11
32
- ? 2
33
- : c < 1 << 16
34
- ? 3
35
- : c < 1 << 21
36
- ? 4
37
- : c < 1 << 26
38
- ? 5
39
- : c < 1 << 31
40
- ? 6
41
- : Number.NaN
42
- }
43
- return byteLen
44
- }
45
-
46
- async function minifyDependencyCode(
47
- source: string,
48
- minifier: 'terser' | 'esbuild' = 'terser'
49
- ) {
50
- if (minifier === 'terser') {
51
- return Terser.minify(source, {
52
- mangle: false,
53
- compress: {
54
- arrows: true,
55
- booleans: true,
56
- collapse_vars: true,
57
- comparisons: true,
58
- conditionals: true,
59
- dead_code: true,
60
- drop_console: false,
61
- drop_debugger: true,
62
- ecma: 5,
63
- evaluate: true,
64
- expression: false,
65
- global_defs: {},
66
- hoist_vars: false,
67
- ie8: false,
68
- if_return: true,
69
- inline: true,
70
- join_vars: true,
71
- keep_fargs: true,
72
- keep_fnames: false,
73
- keep_infinity: false,
74
- loops: true,
75
- negate_iife: true,
76
- passes: 1,
77
- properties: true,
78
- pure_getters: 'strict',
79
- reduce_vars: true,
80
- sequences: true,
81
- side_effects: true,
82
- switches: true,
83
- top_retain: null,
84
- toplevel: false,
85
- typeofs: true,
86
- unsafe: false,
87
- unused: true,
88
- },
89
- output: {
90
- comments: false,
91
- },
92
- })
93
- } else {
94
- return esbuild.transform(
95
- // ESBuild Minifier doesn't auto-remove license comments from code
96
- // So, we break ESBuild's heuristic for license comments match. See github.com/privatenumber/esbuild-loader/issues/87
97
- source
98
- .replace(/@license/g, '@silence')
99
- .replace(/\/\/!/g, '//')
100
- .replace(/\/\*!/g, '//'),
101
- { minify: true }
102
- )
103
- }
104
- }
105
-
106
- type MakeModule = {
107
- path: string
108
- sources: string[]
109
- source: string
110
- }
111
-
112
- type StatsChild = {
113
- path: string
114
- packageName: string
115
- sources: string[]
116
- children: StatsChild[]
117
- }
118
-
119
- type StatsTree = {
120
- packageName: string
121
- sources: string[]
122
- children: StatsChild[]
123
- }
124
-
125
- async function bundleSizeTree(
126
- packageName: string,
127
- stats: webpack.Stats.ToJsonOutput,
128
- minifier: 'terser' | 'esbuild'
129
- ) {
130
- let startTime = performance.now()
131
- let statsTree: StatsTree = {
132
- packageName: '<root>',
133
- sources: [],
134
- children: [],
135
- }
136
-
137
- if (!stats.modules) return []
138
-
139
- // extract source path for each module
140
- let modules: MakeModule[] = []
141
- const makeModule = (mod: webpack.Stats.FnModules): MakeModule => {
142
- // Uglifier cannot minify a json file, hence we need
143
- // to make it valid javascript syntax
144
- const isJSON = mod.identifier.endsWith('.json')
145
- const source = isJSON ? `$a$=${mod.source}` : mod.source
146
-
147
- return {
148
- path: modulePath(mod.identifier),
149
- sources: [source || ''],
150
- source: source || '',
151
- }
152
- }
153
-
154
- stats.modules
155
- .filter(mod => !mod.name.startsWith('external'))
156
- .forEach(mod => {
157
- if (mod.modules) {
158
- mod.modules.forEach(subMod => {
159
- modules.push(makeModule(subMod))
160
- })
161
- } else {
162
- modules.push(makeModule(mod))
163
- }
164
- })
165
-
166
- modules.sort((a, b) => {
167
- if (a === b) {
168
- return 0
169
- } else {
170
- return a < b ? -1 : 1
171
- }
172
- })
173
-
174
- modules.forEach(mod => {
175
- // pnpm will serve packages from a global symlink (.pnpm/package@verison/node_modules/package)
176
- // needs to be stripped off
177
- const pnpmPrefix =
178
- '.pnpm\\' + path.sep + '.+\\' + path.sep + 'node_modules\\' + path.sep
179
- let packages = mod.path.split(
180
- new RegExp(
181
- '\\' + path.sep + 'node_modules\\' + path.sep + `(?:${pnpmPrefix})?`
182
- )
183
- )
184
-
185
- if (packages.length > 1) {
186
- let lastSegment = packages.pop()
187
-
188
- if (!lastSegment) return
189
-
190
- let lastPackageName
191
- if (lastSegment[0] === '@') {
192
- // package is a scoped package
193
- let offset = lastSegment.indexOf(path.sep) + 1
194
- lastPackageName = lastSegment.slice(
195
- 0,
196
- offset + lastSegment.slice(offset).indexOf(path.sep)
197
- )
198
- } else {
199
- lastPackageName = lastSegment.slice(0, lastSegment.indexOf(path.sep))
200
- }
201
- packages.push(lastPackageName)
202
- }
203
- packages.shift()
204
-
205
- let parent = statsTree
206
- packages.forEach(pkg => {
207
- let existing = parent.children.filter(child => child.packageName === pkg)
208
- if (existing.length > 0) {
209
- existing[0].sources.push(mod.source)
210
- parent = existing[0]
211
- } else {
212
- let newChild = {
213
- path: mod.path,
214
- packageName: pkg,
215
- sources: [mod.source],
216
- children: [],
217
- }
218
- parent.children.push(newChild)
219
- parent = newChild
220
- }
221
- })
222
- })
223
-
224
- const resultPromises = statsTree.children
225
- .map(treeItem => ({
226
- ...treeItem,
227
- sources: treeItem.sources.filter(source => !!source),
228
- }))
229
- .filter(treeItem => treeItem.sources.length)
230
- .map(async treeItem => {
231
- const sourceMinifiedPromises = treeItem.sources.map(async code => {
232
- const start = Date.now()
233
- const minified = await minifyDependencyCode(code, minifier)
234
- return minified
235
- })
236
-
237
- try {
238
- const sources = await Promise.all(sourceMinifiedPromises)
239
- const size = sources.reduce((acc, source) => {
240
- return acc + getByteLen(source.code || '')
241
- }, 0)
242
-
243
- return {
244
- name: treeItem.packageName,
245
- approximateSize: size,
246
- }
247
- } catch (error) {
248
- const { message, filename } = error
249
- throw new MinifyError(error, {
250
- message: message,
251
- filePath: filename,
252
- })
253
- }
254
- })
255
-
256
- try {
257
- const results = await Promise.all(resultPromises)
258
- Telemetry.dependencySizes(packageName, startTime, true, { minifier })
259
- return results
260
- } catch (e) {
261
- Telemetry.dependencySizes(packageName, startTime, false, { minifier }, e)
262
- throw e
263
- }
264
- }
265
-
266
- export default bundleSizeTree