Skip to content

Commit 07f5c9a

Browse files
committed
Don't swallows errors, as noted in #1
1 parent 0bdc851 commit 07f5c9a

2 files changed

Lines changed: 102 additions & 90 deletions

File tree

index.js

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const reporter = (analysis, opts) => {
5858
return formatted
5959
};
6060

61-
const analyze = (bundle, opts = {}) => {
61+
const analyzer = (bundle, opts = {}) => {
6262
let { root, limit, filter } = opts;
6363
root = root || (process && process.cwd ? process.cwd() : null);
6464
let deps = {};
@@ -67,54 +67,60 @@ const analyze = (bundle, opts = {}) => {
6767
let bundleModules = bundle.modules || [];
6868
let moduleCount = bundleModules.length;
6969

70-
return new Promise((resolve, reject) => {
71-
let modules = bundleModules.map((m, i) => {
72-
let {
73-
id,
74-
originalLength: origSize,
75-
renderedLength,
76-
code,
77-
usedExports,
78-
unusedExports
79-
} = m;
80-
id = id.replace(root, '');
81-
let size = renderedLength;
82-
if (!size && size !== 0) size = code ? Buffer.byteLength(code, 'utf8') : 0;
83-
bundleSize += size;
84-
bundleOrigSize += origSize;
85-
86-
if (Array.isArray(filter) && !filter.some((f) => match(id, f))) return null
87-
if (typeof filter === 'string' && !match(id, filter)) return null
88-
89-
m.dependencies.forEach((d) => {
90-
d = d.replace(root, '');
91-
deps[d] = deps[d] || [];
92-
deps[d].push(id);
93-
});
70+
let modules = bundleModules.map((m, i) => {
71+
let {
72+
id,
73+
originalLength: origSize,
74+
renderedLength,
75+
code,
76+
usedExports,
77+
unusedExports
78+
} = m;
79+
id = id.replace(root, '');
80+
let size = renderedLength;
81+
if (!size && size !== 0) size = code ? Buffer.byteLength(code, 'utf8') : 0;
82+
bundleSize += size;
83+
bundleOrigSize += origSize;
84+
85+
if (Array.isArray(filter) && !filter.some((f) => match(id, f))) return null
86+
if (typeof filter === 'string' && !match(id, filter)) return null
87+
88+
m.dependencies.forEach((d) => {
89+
d = d.replace(root, '');
90+
deps[d] = deps[d] || [];
91+
deps[d].push(id);
92+
});
9493

95-
return {id, size, origSize, usedExports, unusedExports}
96-
}).filter((m) => m);
94+
return {id, size, origSize, usedExports, unusedExports}
95+
}).filter((m) => m);
9796

98-
modules.sort((a, b) => b.size - a.size);
99-
if (limit || limit === 0) modules = modules.slice(0, limit);
100-
modules.forEach((m) => {
101-
m.dependents = deps[m.id] || [];
102-
m.percent = Math.min(((m.size / bundleSize) * 100).toFixed(2), 100);
103-
m.reduction = shakenPct(m.size, m.origSize);
104-
});
105-
if (typeof filter === 'function') modules = modules.filter(filter);
97+
modules.sort((a, b) => b.size - a.size);
98+
if (limit || limit === 0) modules = modules.slice(0, limit);
99+
modules.forEach((m) => {
100+
m.dependents = deps[m.id] || [];
101+
m.percent = Math.min(((m.size / bundleSize) * 100).toFixed(2), 100);
102+
m.reduction = shakenPct(m.size, m.origSize);
103+
});
104+
if (typeof filter === 'function') modules = modules.filter(filter);
106105

107-
let bundleReduction = shakenPct(bundleSize, bundleOrigSize);
106+
let bundleReduction = shakenPct(bundleSize, bundleOrigSize);
108107

109-
return resolve({
110-
bundleSize, bundleOrigSize, bundleReduction, modules, moduleCount
111-
})
112-
})
108+
return {bundleSize, bundleOrigSize, bundleReduction, modules, moduleCount}
113109
};
114110

115-
const formatted = (bndl, opts) => {
116-
return analyze(bndl, opts).then((analysis) => reporter(analysis, opts))
117-
};
111+
const analyze = (bundle, opts) => new Promise((resolve, reject) => {
112+
try {
113+
let analysis = analyzer(bundle, opts);
114+
return resolve(analysis)
115+
} catch (ex) { return reject(ex) }
116+
});
117+
118+
const formatted = (bundle, opts) => new Promise((resolve, reject) => {
119+
try {
120+
let analysis = analyzer(bundle, opts);
121+
return resolve(reporter(analysis, opts))
122+
} catch (ex) { return resolve(ex.toString()) }
123+
});
118124

119125
const plugin = (opts = {}) => {
120126
let writeTo = opts.writeTo || (opts.stdout ? console.log : console.error);
@@ -131,7 +137,7 @@ const plugin = (opts = {}) => {
131137
let modules = bundle.modules;
132138

133139
if (Array.isArray(modules)) {
134-
return analyze({modules}, opts).then(onAnalysis)
140+
return analyze({modules}, opts).then(onAnalysis).catch(console.error)
135141
}
136142

137143
modules = Object.keys(modules).map((k) => {
@@ -140,7 +146,7 @@ const plugin = (opts = {}) => {
140146
module.unusedExports = module.removedExports;
141147
return module
142148
});
143-
return analyze({modules}, opts).then(onAnalysis)
149+
return analyze({modules}, opts).then(onAnalysis).catch(console.error)
144150
});
145151

146152
return {

module.js

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const reporter = (analysis, opts) => {
5656
return formatted
5757
}
5858

59-
export const analyze = (bundle, opts = {}) => {
59+
const analyzer = (bundle, opts = {}) => {
6060
let { root, limit, filter } = opts
6161
root = root || (process && process.cwd ? process.cwd() : null)
6262
let deps = {}
@@ -65,55 +65,61 @@ export const analyze = (bundle, opts = {}) => {
6565
let bundleModules = bundle.modules || []
6666
let moduleCount = bundleModules.length
6767

68-
return new Promise((resolve, reject) => {
69-
let modules = bundleModules.map((m, i) => {
70-
let {
71-
id,
72-
originalLength: origSize,
73-
renderedLength,
74-
code,
75-
usedExports,
76-
unusedExports
77-
} = m
78-
id = id.replace(root, '')
79-
let size = renderedLength
80-
if (!size && size !== 0) size = code ? Buffer.byteLength(code, 'utf8') : 0
81-
bundleSize += size
82-
bundleOrigSize += origSize
83-
84-
if (Array.isArray(filter) && !filter.some((f) => match(id, f))) return null
85-
if (typeof filter === 'string' && !match(id, filter)) return null
86-
87-
m.dependencies.forEach((d) => {
88-
d = d.replace(root, '')
89-
deps[d] = deps[d] || []
90-
deps[d].push(id)
91-
})
92-
93-
return {id, size, origSize, usedExports, unusedExports}
94-
}).filter((m) => m)
95-
96-
modules.sort((a, b) => b.size - a.size)
97-
if (limit || limit === 0) modules = modules.slice(0, limit)
98-
modules.forEach((m) => {
99-
m.dependents = deps[m.id] || []
100-
m.percent = Math.min(((m.size / bundleSize) * 100).toFixed(2), 100)
101-
m.reduction = shakenPct(m.size, m.origSize)
68+
let modules = bundleModules.map((m, i) => {
69+
let {
70+
id,
71+
originalLength: origSize,
72+
renderedLength,
73+
code,
74+
usedExports,
75+
unusedExports
76+
} = m
77+
id = id.replace(root, '')
78+
let size = renderedLength
79+
if (!size && size !== 0) size = code ? Buffer.byteLength(code, 'utf8') : 0
80+
bundleSize += size
81+
bundleOrigSize += origSize
82+
83+
if (Array.isArray(filter) && !filter.some((f) => match(id, f))) return null
84+
if (typeof filter === 'string' && !match(id, filter)) return null
85+
86+
m.dependencies.forEach((d) => {
87+
d = d.replace(root, '')
88+
deps[d] = deps[d] || []
89+
deps[d].push(id)
10290
})
103-
if (typeof filter === 'function') modules = modules.filter(filter)
10491

105-
let bundleReduction = shakenPct(bundleSize, bundleOrigSize)
92+
return {id, size, origSize, usedExports, unusedExports}
93+
}).filter((m) => m)
10694

107-
return resolve({
108-
bundleSize, bundleOrigSize, bundleReduction, modules, moduleCount
109-
})
95+
modules.sort((a, b) => b.size - a.size)
96+
if (limit || limit === 0) modules = modules.slice(0, limit)
97+
modules.forEach((m) => {
98+
m.dependents = deps[m.id] || []
99+
m.percent = Math.min(((m.size / bundleSize) * 100).toFixed(2), 100)
100+
m.reduction = shakenPct(m.size, m.origSize)
110101
})
111-
}
102+
if (typeof filter === 'function') modules = modules.filter(filter)
103+
104+
let bundleReduction = shakenPct(bundleSize, bundleOrigSize)
112105

113-
export const formatted = (bndl, opts) => {
114-
return analyze(bndl, opts).then((analysis) => reporter(analysis, opts))
106+
return {bundleSize, bundleOrigSize, bundleReduction, modules, moduleCount}
115107
}
116108

109+
export const analyze = (bundle, opts) => new Promise((resolve, reject) => {
110+
try {
111+
let analysis = analyzer(bundle, opts)
112+
return resolve(analysis)
113+
} catch (ex) { return reject(ex) }
114+
})
115+
116+
export const formatted = (bundle, opts) => new Promise((resolve, reject) => {
117+
try {
118+
let analysis = analyzer(bundle, opts)
119+
return resolve(reporter(analysis, opts))
120+
} catch (ex) { return resolve(ex.toString()) }
121+
})
122+
117123
export const plugin = (opts = {}) => {
118124
let writeTo = opts.writeTo || (opts.stdout ? console.log : console.error)
119125
let depMap = {}
@@ -129,7 +135,7 @@ export const plugin = (opts = {}) => {
129135
let modules = bundle.modules
130136

131137
if (Array.isArray(modules)) {
132-
return analyze({modules}, opts).then(onAnalysis)
138+
return analyze({modules}, opts).then(onAnalysis).catch(console.error)
133139
}
134140

135141
modules = Object.keys(modules).map((k) => {
@@ -138,7 +144,7 @@ export const plugin = (opts = {}) => {
138144
module.unusedExports = module.removedExports
139145
return module
140146
})
141-
return analyze({modules}, opts).then(onAnalysis)
147+
return analyze({modules}, opts).then(onAnalysis).catch(console.error)
142148
})
143149

144150
return {

0 commit comments

Comments
 (0)