Skip to content

Commit a173a43

Browse files
committed
Implement transformModuleId option
1 parent cfe79fc commit a173a43

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@ const reporter = (analysis, opts) => {
143143
};
144144

145145
const analyzer = (bundle, opts = {}) => {
146-
let { root, limit, filter } = opts;
146+
let { root, limit, filter, transformModuleId } = opts;
147147
root = root || (process && process.cwd ? process.cwd() : null);
148+
if (typeof transformModuleId !== 'function') transformModuleId = undefined;
148149
let deps = {};
149150
let bundleSize = 0;
150151
let bundleOrigSize = 0;
@@ -161,6 +162,7 @@ const analyzer = (bundle, opts = {}) => {
161162
unusedExports
162163
} = m;
163164
id = id.replace(root, '');
165+
if (transformModuleId) id = transformModuleId(id);
164166
let size = renderedLength;
165167
if (!size && size !== 0) size = code ? Buffer.byteLength(code, 'utf8') : 0;
166168
bundleSize += size;
@@ -171,11 +173,12 @@ const analyzer = (bundle, opts = {}) => {
171173

172174
m.dependencies.forEach((d) => {
173175
d = d.replace(root, '');
176+
if (transformModuleId) d = transformModuleId(d);
174177
deps[d] = deps[d] || [];
175178
deps[d].push(id);
176179
});
177180

178-
return {id, size, origSize, usedExports, unusedExports}
181+
return { id, size, origSize, usedExports, unusedExports }
179182
}).filter((m) => m);
180183

181184
modules.sort((a, b) => b.size - a.size);
@@ -189,7 +192,7 @@ const analyzer = (bundle, opts = {}) => {
189192

190193
let bundleReduction = shakenPct(bundleSize, bundleOrigSize);
191194

192-
return {bundleSize, bundleOrigSize, bundleReduction, modules, moduleCount}
195+
return { bundleSize, bundleOrigSize, bundleReduction, modules, moduleCount }
193196
};
194197

195198
const analyze = (bundle, opts) => new Promise((resolve, reject) => {
@@ -212,7 +215,9 @@ const plugin = (opts = {}) => {
212215

213216
let onAnalysis = (analysis) => {
214217
if (typeof opts.onAnalysis === 'function') opts.onAnalysis(analysis);
215-
if (typeof opts.htmlReportPath === 'string') writeHtmlReport(analysis.modules, opts.htmlReportPath);
218+
if (typeof opts.htmlReportPath === 'string') {
219+
writeHtmlReport(analysis.modules, opts.htmlReportPath);
220+
}
216221
if (!opts.skipFormatted) writeTo(reporter(analysis, opts));
217222
};
218223

@@ -222,7 +227,7 @@ const plugin = (opts = {}) => {
222227
let modules = bundle.modules;
223228

224229
if (Array.isArray(modules)) {
225-
return analyze({modules}, opts).then(onAnalysis).catch(console.error)
230+
return analyze({ modules }, opts).then(onAnalysis).catch(console.error)
226231
}
227232

228233
modules = Object.keys(modules).map((k) => {
@@ -231,16 +236,16 @@ const plugin = (opts = {}) => {
231236
module.unusedExports = module.removedExports;
232237
return module
233238
});
234-
return analyze({modules}, opts).then(onAnalysis).catch(console.error)
239+
return analyze({ modules }, opts).then(onAnalysis).catch(console.error)
235240
});
236241

237242
return {
238243
name: 'rollup-plugin-analyzer',
239244
transformChunk: (_a, _b, chunk) => new Promise((resolve, reject) => {
240245
resolve(null);
241246
if (!chunk || !chunk.orderedModules) return
242-
chunk.orderedModules.forEach(({id, dependencies}) => {
243-
depMap[id] = {id, dependencies: dependencies.map((d) => d.id)};
247+
chunk.orderedModules.forEach(({ id, dependencies }) => {
248+
depMap[id] = { id, dependencies: dependencies.map((d) => d.id) };
244249
});
245250
}),
246251
generateBundle: runAnalysis,

module.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ export const reporter = (analysis, opts) => {
5959
}
6060

6161
const analyzer = (bundle, opts = {}) => {
62-
let { root, limit, filter } = opts
62+
let { root, limit, filter, transformModuleId } = opts
6363
root = root || (process && process.cwd ? process.cwd() : null)
64+
if (typeof transformModuleId !== 'function') transformModuleId = undefined
6465
let deps = {}
6566
let bundleSize = 0
6667
let bundleOrigSize = 0
@@ -77,6 +78,7 @@ const analyzer = (bundle, opts = {}) => {
7778
unusedExports
7879
} = m
7980
id = id.replace(root, '')
81+
if (transformModuleId) id = transformModuleId(id)
8082
let size = renderedLength
8183
if (!size && size !== 0) size = code ? Buffer.byteLength(code, 'utf8') : 0
8284
bundleSize += size
@@ -87,6 +89,7 @@ const analyzer = (bundle, opts = {}) => {
8789

8890
m.dependencies.forEach((d) => {
8991
d = d.replace(root, '')
92+
if (transformModuleId) d = transformModuleId(d)
9093
deps[d] = deps[d] || []
9194
deps[d].push(id)
9295
})
@@ -128,7 +131,9 @@ export const plugin = (opts = {}) => {
128131

129132
let onAnalysis = (analysis) => {
130133
if (typeof opts.onAnalysis === 'function') opts.onAnalysis(analysis)
131-
if (typeof opts.htmlReportPath === 'string') writeHtmlReport(analysis.modules, opts.htmlReportPath)
134+
if (typeof opts.htmlReportPath === 'string') {
135+
writeHtmlReport(analysis.modules, opts.htmlReportPath)
136+
}
132137
if (!opts.skipFormatted) writeTo(reporter(analysis, opts))
133138
}
134139

test/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ rollers.forEach(({ rollup, version, opts, noTreeshake }) => {
103103
assert.is((await analyze(bundle, { filter: hasMatch })).modules.length, 1)
104104
})
105105

106-
test.failing(`${version}: transformModuleId works`, async (assert) => {
106+
test(`${version}: transformModuleId works`, async (assert) => {
107107
let bundle = await rollup(opts)
108-
let transformModuleId = (id) => `transformed-${id}`
109-
let expect = `transformed-import-a`
108+
let transformModuleId = (id) => `transformed-${basename(id)}`
109+
let expect = `transformed-import-a.js`
110110
let modules = (await analyze(bundle, { transformModuleId })).modules
111111
let firstModule = basename(modules[0].id)
112112
assert.is(firstModule, expect)

0 commit comments

Comments
 (0)