Skip to content

Commit 5fe6b29

Browse files
committed
Avoid double normalization
1 parent c14f933 commit 5fe6b29

File tree

2 files changed

+49
-60
lines changed

2 files changed

+49
-60
lines changed

packages/bundle-size-checker/src/cli.js

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,6 @@ const DEFAULT_CONCURRENCY = os.availableParallelism();
2121

2222
const rootDir = process.cwd();
2323

24-
/**
25-
* Normalizes entries to ensure they have a consistent format and ids are unique
26-
* @param {ObjectEntry[]} entries - The array of entries from the config
27-
* @returns {ObjectEntry[]} - Normalized entries with uniqueness enforced
28-
*/
29-
function normalizeEntries(entries) {
30-
const usedIds = new Set();
31-
32-
return entries.map((entry) => {
33-
if (!entry.id) {
34-
throw new Error('Object entries must have an id property');
35-
}
36-
37-
if (!entry.code && !entry.import) {
38-
throw new Error(`Entry "${entry.id}" must have either code or import property defined`);
39-
}
40-
41-
if (usedIds.has(entry.id)) {
42-
throw new Error(`Duplicate entry id found: "${entry.id}". Entry ids must be unique.`);
43-
}
44-
45-
usedIds.add(entry.id);
46-
47-
return entry;
48-
});
49-
}
50-
5124
/**
5225
* creates size snapshot for every bundle that built with webpack
5326
* @param {CommandLineArgs} args
@@ -74,14 +47,11 @@ async function getWebpackSizes(args, config) {
7447
);
7548
}
7649

77-
// Normalize and validate entries
78-
const entries = normalizeEntries(config.entrypoints);
79-
8050
// Apply filters if provided
81-
let validEntries = entries;
51+
let validEntries = config.entrypoints;
8252
const filter = args.filter;
8353
if (filter && filter.length > 0) {
84-
validEntries = entries.filter((entry) => {
54+
validEntries = config.entrypoints.filter((entry) => {
8555
return filter.some((pattern) => {
8656
if (pattern.includes('*') || pattern.includes('?') || pattern.includes('[')) {
8757
return micromatch.isMatch(entry.id, pattern, { nocase: true });

packages/bundle-size-checker/src/configLoader.js

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,52 @@ export function applyUploadConfigDefaults(uploadConfig, ciInfo) {
7777
};
7878
}
7979

80+
/**
81+
* Normalizes entries to ensure they have a consistent format and ids are unique
82+
* @param {EntryPoint[]} entries - The array of entries from the config
83+
* @returns {ObjectEntry[]} - Normalized entries with uniqueness enforced
84+
*/
85+
function normalizeEntries(entries) {
86+
const usedIds = new Set();
87+
88+
return entries.map((entry) => {
89+
if (typeof entry === 'string') {
90+
// Transform string entries into object entries
91+
const [importSrc, importName] = entry.split('#');
92+
if (importName) {
93+
// For entries like '@mui/material#Button', create an object with import and importedNames
94+
entry = {
95+
id: entry,
96+
import: importSrc,
97+
importedNames: [importName],
98+
};
99+
} else {
100+
// For entries like '@mui/material', create an object with import only
101+
entry = {
102+
id: entry,
103+
import: importSrc,
104+
};
105+
}
106+
}
107+
108+
if (!entry.id) {
109+
throw new Error('Object entries must have an id property');
110+
}
111+
112+
if (!entry.code && !entry.import) {
113+
throw new Error(`Entry "${entry.id}" must have either code or import property defined`);
114+
}
115+
116+
if (usedIds.has(entry.id)) {
117+
throw new Error(`Duplicate entry id found: "${entry.id}". Entry ids must be unique.`);
118+
}
119+
120+
usedIds.add(entry.id);
121+
122+
return entry;
123+
});
124+
}
125+
80126
/**
81127
* Apply default values to the configuration using CI environment
82128
* @param {BundleSizeCheckerConfigObject} config - The loaded configuration
@@ -99,34 +145,7 @@ function applyConfigDefaults(config) {
99145
// Clone the config to avoid mutating the original
100146
/** @type {NormalizedBundleSizeCheckerConfig} */
101147
const result = {
102-
entrypoints: config.entrypoints.map((entry, i) => {
103-
if (typeof entry === 'string') {
104-
// Transform string entries into object entries
105-
const [importSrc, importName] = entry.split('#');
106-
if (importName) {
107-
// For entries like '@mui/material#Button', create an object with import and importedNames
108-
return {
109-
id: entry,
110-
import: importSrc,
111-
importedNames: [importName],
112-
};
113-
}
114-
// For entries like '@mui/material', create an object with import only
115-
return {
116-
id: entry,
117-
import: importSrc,
118-
};
119-
}
120-
121-
if (entry && typeof entry === 'object') {
122-
// For existing object entries, return them as is
123-
return entry;
124-
}
125-
126-
throw new Error(
127-
`Invalid entry format config.entrypoints[${i}]. Must be a string or an object.`,
128-
);
129-
}),
148+
entrypoints: normalizeEntries(config.entrypoints),
130149
upload: null, // Default to disabled
131150
};
132151

0 commit comments

Comments
 (0)