Skip to content

Commit cfe79fc

Browse files
committed
Add failing tests, updates readme, standard fixes
Doing some maintenance prior to the next minor release. Added a test for the not yet implemented transformModuleId. Also added stub for htmlReportPath test. Fixed object literal notation to match updated standardjs specs. Added documentation for transformModuleId.
1 parent ba0436a commit cfe79fc

5 files changed

Lines changed: 89 additions & 55 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ jspm_packages
4040
# Output files
4141
test/fixtures/output.js
4242
test/fixtures/multi
43+
test/fixtures/report.html

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2016 Andrew Carpenter
3+
Copyright (c) 2018 Andrew Carpenter
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,26 @@ dependents: 1
139139
- type: Function
140140
- default: `null`
141141
- description: Callback to be invoked with formatted string
142-
- callback invoked with:
142+
- function will be invoked with:
143143
- **analysisString** *(String)*
144+
- **htmlReportPath** - *optional*
145+
- type: String
146+
- default: `null`
147+
- description: Write HTML report to this path
148+
- **transformModuleId** - *optional*
149+
- type: Function
150+
- default: `null`
151+
- description: Modify module ids
152+
- function will be invoked with:
153+
- **id** *(String)* - path of module / rollup module id
154+
- function should return:
155+
- **id** *(String)* - desired final module id to display in analysis results
156+
- example: `(id) => id.replace(/^\0(?:commonjs-proxy:)?/, '')`
144157
- **onAnalysis** - *optional*
145158
- type: Function
146159
- default: `null`
147160
- description: Callback to be invoked with analysis object
148-
- callback invoked with:
161+
- function will be invoked with:
149162
- **analysisObject** *(Object)*
150163
- **bundleSize** *(Number)* - rendered bundle size in bytes
151164
- **bundleOrigSize** *(Number)* - original bundle size in bytes
@@ -161,10 +174,6 @@ dependents: 1
161174
- **reduction** *(Number)* - percentage of rendered size reduction
162175
- **usedExports** *(Array)* - list of used named exports
163176
- **unusedExports** *(Array)* - list of unused named exports
164-
- **htmlReportPath** - *optional*
165-
- type: String
166-
- default: `null`
167-
- description: Write HTML report to this path
168177

169178
## Other considerations
170179

module.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const analyzer = (bundle, opts = {}) => {
9191
deps[d].push(id)
9292
})
9393

94-
return {id, size, origSize, usedExports, unusedExports}
94+
return { id, size, origSize, usedExports, unusedExports }
9595
}).filter((m) => m)
9696

9797
modules.sort((a, b) => b.size - a.size)
@@ -105,7 +105,7 @@ const analyzer = (bundle, opts = {}) => {
105105

106106
let bundleReduction = shakenPct(bundleSize, bundleOrigSize)
107107

108-
return {bundleSize, bundleOrigSize, bundleReduction, modules, moduleCount}
108+
return { bundleSize, bundleOrigSize, bundleReduction, modules, moduleCount }
109109
}
110110

111111
export const analyze = (bundle, opts) => new Promise((resolve, reject) => {
@@ -138,7 +138,7 @@ export const plugin = (opts = {}) => {
138138
let modules = bundle.modules
139139

140140
if (Array.isArray(modules)) {
141-
return analyze({modules}, opts).then(onAnalysis).catch(console.error)
141+
return analyze({ modules }, opts).then(onAnalysis).catch(console.error)
142142
}
143143

144144
modules = Object.keys(modules).map((k) => {
@@ -147,16 +147,16 @@ export const plugin = (opts = {}) => {
147147
module.unusedExports = module.removedExports
148148
return module
149149
})
150-
return analyze({modules}, opts).then(onAnalysis).catch(console.error)
150+
return analyze({ modules }, opts).then(onAnalysis).catch(console.error)
151151
})
152152

153153
return {
154154
name: 'rollup-plugin-analyzer',
155155
transformChunk: (_a, _b, chunk) => new Promise((resolve, reject) => {
156156
resolve(null)
157157
if (!chunk || !chunk.orderedModules) return
158-
chunk.orderedModules.forEach(({id, dependencies}) => {
159-
depMap[id] = {id, dependencies: dependencies.map((d) => d.id)}
158+
chunk.orderedModules.forEach(({ id, dependencies }) => {
159+
depMap[id] = { id, dependencies: dependencies.map((d) => d.id) }
160160
})
161161
}),
162162
generateBundle: runAnalysis,

test/test.js

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
// setup
44
import test from 'ava'
5+
// import { promises as fs } from 'fs'
56
import { analyze, formatted, plugin } from './../index'
6-
import { resolve, join } from 'path'
7+
import { resolve, join, basename } from 'path'
78
import { rollup as rollupLatest } from 'rollup'
89
import { rollup as rollup60 } from 'rollup60'
910
import { rollup as rollup55 } from 'rollup55'
@@ -14,15 +15,15 @@ const skipFormatted = true
1415
const fixtures = resolve(__dirname, 'fixtures')
1516
const baseOpts = {
1617
input: join(fixtures, 'bundle-a.js'),
17-
output: {format: 'cjs'}
18+
output: { format: 'cjs' }
1819
}
1920
const oldOpts = {
2021
entry: join(fixtures, 'bundle-a.js'),
2122
format: 'cjs'
2223
}
2324
const multiInputOpts = {
2425
input: [join(fixtures, 'bundle-a.js'), join(fixtures, 'bundle-b.js')],
25-
output: {format: 'cjs', dir: join(fixtures, 'multi')}
26+
output: { format: 'cjs', dir: join(fixtures, 'multi') }
2627
}
2728
const expectHeader = `
2829
-----------------------------
@@ -33,16 +34,16 @@ const headerLength = expectHeader.length
3334

3435
// test against many versions of rollup
3536
const rollers = [
36-
{rollup: rollupLatest, version: 'latest', opts: baseOpts},
37-
{rollup: rollup60, version: '0.60.x', opts: baseOpts},
38-
{rollup: rollup55, version: '0.55.x', opts: baseOpts, noTreeshake: true},
39-
{rollup: rollup50, version: '0.50.x', opts: baseOpts, noTreeshake: true},
40-
{rollup: rollup45, version: '0.45.x', opts: oldOpts, noTreeshake: true},
41-
{rollup: rollup40, version: '0.40.x', opts: oldOpts, noTreeshake: true}
37+
{ rollup: rollupLatest, version: 'latest', opts: baseOpts },
38+
{ rollup: rollup60, version: '0.60.x', opts: baseOpts },
39+
{ rollup: rollup55, version: '0.55.x', opts: baseOpts, noTreeshake: true },
40+
{ rollup: rollup50, version: '0.50.x', opts: baseOpts, noTreeshake: true },
41+
{ rollup: rollup45, version: '0.45.x', opts: oldOpts, noTreeshake: true },
42+
{ rollup: rollup40, version: '0.40.x', opts: oldOpts, noTreeshake: true }
4243
]
4344

4445
// main
45-
rollers.forEach(({rollup, version, opts, noTreeshake}) => {
46+
rollers.forEach(({ rollup, version, opts, noTreeshake }) => {
4647
test(`${version}: formatted returns expected string`, async (assert) => {
4748
let bundle = await rollup(opts)
4849
let results = await formatted(bundle)
@@ -71,52 +72,61 @@ rollers.forEach(({rollup, version, opts, noTreeshake}) => {
7172

7273
test(`${version}: limit works`, async (assert) => {
7374
let bundle = await rollup(opts)
74-
assert.is((await analyze(bundle, {limit: 0})).modules.length, 0)
75-
assert.is((await analyze(bundle, {limit: 1})).modules.length, 1)
76-
assert.is((await analyze(bundle, {limit: 0})).modules.length, 0)
75+
assert.is((await analyze(bundle, { limit: 0 })).modules.length, 0)
76+
assert.is((await analyze(bundle, { limit: 1 })).modules.length, 1)
77+
assert.is((await analyze(bundle, { limit: 0 })).modules.length, 0)
7778
})
7879

7980
test(`${version}: filter with array works`, async (assert) => {
8081
let bundle = await rollup(opts)
8182
assert.is(
82-
(await analyze(bundle, {filter: ['jimmy', 'jerry']})).modules.length,
83+
(await analyze(bundle, { filter: ['jimmy', 'jerry'] })).modules.length,
8384
0
8485
)
8586
assert.is(
86-
(await analyze(bundle, {filter: ['import-a', 'jessie']})).modules.length,
87+
(await analyze(bundle, { filter: ['import-a', 'jessie'] })).modules.length,
8788
1
8889
)
8990
})
9091

9192
test(`${version}: filter with string works`, async (assert) => {
9293
let bundle = await rollup(opts)
93-
assert.is((await analyze(bundle, {filter: 'jimmy'})).modules.length, 0)
94-
assert.is((await analyze(bundle, {filter: 'import-b'})).modules.length, 1)
94+
assert.is((await analyze(bundle, { filter: 'jimmy' })).modules.length, 0)
95+
assert.is((await analyze(bundle, { filter: 'import-b' })).modules.length, 1)
9596
})
9697

9798
test(`${version}: filter with callback works`, async (assert) => {
9899
let bundle = await rollup(opts)
99100
let noMatch = (m) => m.id.indexOf('jimmy') !== -1
100101
let hasMatch = (m) => m.id.indexOf('import-b') !== -1
101-
assert.is((await analyze(bundle, {filter: noMatch})).modules.length, 0)
102-
assert.is((await analyze(bundle, {filter: hasMatch})).modules.length, 1)
102+
assert.is((await analyze(bundle, { filter: noMatch })).modules.length, 0)
103+
assert.is((await analyze(bundle, { filter: hasMatch })).modules.length, 1)
104+
})
105+
106+
test.failing(`${version}: transformModuleId works`, async (assert) => {
107+
let bundle = await rollup(opts)
108+
let transformModuleId = (id) => `transformed-${id}`
109+
let expect = `transformed-import-a`
110+
let modules = (await analyze(bundle, { transformModuleId })).modules
111+
let firstModule = basename(modules[0].id)
112+
assert.is(firstModule, expect)
103113
})
104114

105115
test(`${version}: root works as expected`, async (assert) => {
106116
let bundle = await rollup(opts)
107117
assert.not(
108-
join(__dirname, (await analyze(bundle, {root: 'fakepath'})).modules[0].id),
118+
join(__dirname, (await analyze(bundle, { root: 'fakepath' })).modules[0].id),
109119
resolve(fixtures, 'import-a.js')
110120
)
111121
assert.is(
112-
join(__dirname, (await analyze(bundle, {root: __dirname})).modules[0].id),
122+
join(__dirname, (await analyze(bundle, { root: __dirname })).modules[0].id),
113123
resolve(fixtures, 'import-a.js')
114124
)
115125
})
116126

117127
test(`${version}: it works with generated bundle as well`, async (assert) => {
118128
let bundle = await rollup(opts)
119-
await bundle.generate({format: 'cjs'})
129+
await bundle.generate({ format: 'cjs' })
120130
let results = await formatted(bundle)
121131
assert.is(typeof results, 'string')
122132
})
@@ -125,33 +135,41 @@ rollers.forEach(({rollup, version, opts, noTreeshake}) => {
125135
let results
126136
let writeTo = (r) => { results = r }
127137
let showExports = true
128-
let rollOpts = Object.assign({plugins: [plugin({writeTo, showExports})]}, opts)
138+
let rollOpts = Object.assign(
139+
{ plugins: [plugin({ writeTo, showExports })] },
140+
opts
141+
)
129142
let bundle = await rollup(rollOpts)
130-
await bundle.generate({format: 'cjs'})
143+
await bundle.generate({ format: 'cjs' })
131144
assert.is(results.substr(0, expectHeader.length), expectHeader)
132145
})
133146

147+
test.failing(`${version}: htmlReportPath produces report`, async (assert) => {
148+
assert.true(false)
149+
})
150+
134151
if (!noTreeshake) {
135152
test(`${version}: tree shaking is accounted for`, async (assert) => {
136153
let results
137154
let onAnalysis = (r) => { results = r.modules }
138-
let plugins = [plugin({onAnalysis, skipFormatted})]
139-
let rollOpts = Object.assign({}, opts, {plugins})
155+
let plugins = [plugin({ onAnalysis, skipFormatted })]
156+
let rollOpts = Object.assign({}, opts, { plugins })
140157
let bundle = await rollup(rollOpts)
141-
let output = {file: join(fixtures, 'output.js'), format: 'cjs'}
158+
let output = { file: join(fixtures, 'output.js'), format: 'cjs' }
142159
await bundle.write(output)
143160
let imported = results.find((r) => r.id.indexOf('import-a') !== -1)
144-
assert.is(imported.size, 27)
161+
let expectSize = 27
162+
assert.true(Math.abs(imported.size - expectSize) < 5)
145163
})
146164

147165
test(`${version}: treeshaken bundle filters with callback`, async (assert) => {
148166
let results
149167
let filter = (m) => m.size > 2000
150168
let onAnalysis = (r) => { results = r.modules }
151-
let plugins = [plugin({onAnalysis, filter, skipFormatted})]
152-
let rollOpts = Object.assign({}, opts, {plugins})
169+
let plugins = [plugin({ onAnalysis, filter, skipFormatted })]
170+
let rollOpts = Object.assign({}, opts, { plugins })
153171
let bundle = await rollup(rollOpts)
154-
let output = {file: join(fixtures, 'output.js'), format: 'cjs'}
172+
let output = { file: join(fixtures, 'output.js'), format: 'cjs' }
155173
await bundle.write(output)
156174
assert.is(results.length, 1)
157175
})
@@ -162,39 +180,45 @@ rollers.forEach(({rollup, version, opts, noTreeshake}) => {
162180
test(split1, async (assert) => {
163181
let results
164182
let writeTo = (r) => { results = r }
165-
let rollOpts = Object.assign({plugins: [plugin({writeTo})]}, multiInputOpts)
183+
let rollOpts = Object.assign(
184+
{ plugins: [plugin({ writeTo })] },
185+
multiInputOpts
186+
)
166187
rollOpts.experimentalCodeSplitting = true
167188
let bundle = await rollup(rollOpts)
168-
await bundle.generate({format: 'cjs'})
189+
await bundle.generate({ format: 'cjs' })
169190
assert.is(results.substr(0, expectHeader.length), expectHeader)
170191
})
171192

172193
let split2 = `${version}: data as expected with experimentalCodeSplitting`
173194
test(split2, async (assert) => {
174195
let results = []
175196
let onAnalysis = (r) => { results.push(r.modules) }
176-
let plugins = [plugin({onAnalysis, skipFormatted})]
177-
let rollOpts = Object.assign({}, multiInputOpts, {plugins})
197+
let plugins = [plugin({ onAnalysis, skipFormatted })]
198+
let rollOpts = Object.assign({}, multiInputOpts, { plugins })
178199
rollOpts.experimentalCodeSplitting = true
179200
let bundle = await rollup(rollOpts)
180201
await bundle.write(rollOpts.output)
181202

182-
let imports = [{id: 'import-a', size: 8238}, {id: 'import-b', size: 33}]
203+
let imports = [{ id: 'import-a', size: 8238 }, { id: 'import-b', size: 33 }]
183204
imports.forEach((imp, i) => {
184205
let result = results[i]
185206
let imported = result.find((r) => r.id.indexOf(imp.id) !== -1)
186-
assert.is(imported.size, imp.size)
207+
assert.true(Math.abs(imported.size - imp.size) < 5)
187208
})
188209
})
189210

190211
test.failing(`${version}: callback is only invoked once`, async (assert) => {
191212
let count = 0
192213
let writeTo = () => ++count
193214

194-
let rollOpts = Object.assign({plugins: [plugin({writeTo})]}, multiInputOpts)
215+
let rollOpts = Object.assign(
216+
{ plugins: [plugin({ writeTo })] },
217+
multiInputOpts
218+
)
195219
rollOpts.experimentalCodeSplitting = true
196220
let bundle = await rollup(rollOpts)
197-
await bundle.generate({format: 'cjs'})
221+
await bundle.generate({ format: 'cjs' })
198222

199223
assert.is(count, 1)
200224
})
@@ -210,18 +234,18 @@ rollers.forEach(({rollup, version, opts, noTreeshake}) => {
210234
start = Date.now()
211235
for (let i = 0; i < runs; i++) {
212236
let bundle = await rollup(opts)
213-
await bundle.generate({format: 'cjs'})
237+
await bundle.generate({ format: 'cjs' })
214238
}
215239
let noPlugin = (Date.now() - start) / runs
216240

217241
// now with the plugin
218242
let count = 0
219243
let writeTo = (r) => ++count
220-
let rollOpts = Object.assign({plugins: [plugin({writeTo})]}, opts)
244+
let rollOpts = Object.assign({ plugins: [plugin({ writeTo })] }, opts)
221245
start = Date.now()
222246
for (let i = 0; i < runs; i++) {
223247
let bundle = await rollup(rollOpts)
224-
await bundle.generate({format: 'cjs'})
248+
await bundle.generate({ format: 'cjs' })
225249
}
226250
let withPlugin = (Date.now() - start) / runs
227251

0 commit comments

Comments
 (0)