Skip to content

Commit 8e5d30f

Browse files
committed
Beef up test fixtures
1 parent 9dbbc79 commit 8e5d30f

9 files changed

Lines changed: 181 additions & 31 deletions

File tree

index.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@ const analyze = (bundle, opts = {}, format = false) => {
1818
let { root, limit, filter } = opts;
1919
root = root || (process && process.cwd ? process.cwd() : null);
2020
let deps = {};
21-
let entrySize;
2221
let bundleSize = 0;
2322
let bundleModules = bundle.modules;
2423

2524
return new Promise((resolve, reject) => {
2625
let modules = bundleModules.map((m, i) => {
27-
let { id, originalLength: origSize, renderedLength, isEntry, code } = m;
26+
let { id, originalLength: origSize, renderedLength, code } = m;
2827
id = id.replace(root, '');
2928
let size = renderedLength;
3029
if (!size && size !== 0) size = code ? Buffer.byteLength(code, 'utf8') : 0;
31-
if (isEntry) entrySize = size;
3230
bundleSize += size;
3331

3432
if (Array.isArray(filter) && !filter.some((f) => id.match(f))) return null
@@ -40,11 +38,9 @@ const analyze = (bundle, opts = {}, format = false) => {
4038
deps[d].push(id);
4139
});
4240

43-
return {id, size, origSize, isEntry}
41+
return {id, size, origSize}
4442
}).filter((m) => m);
4543

46-
if (entrySize) bundleSize = entrySize;
47-
4844
modules.sort((a, b) => b.size - a.size);
4945
if (limit || limit === 0) modules = modules.slice(0, limit);
5046
modules.forEach((m) => {
@@ -60,7 +56,6 @@ const analyze = (bundle, opts = {}, format = false) => {
6056
let formatted = `${borderX}${heading}${displaySize}${borderX}`;
6157

6258
modules.forEach((m) => {
63-
if (m.isEntry) return
6459
formatted += `file:${buf}${m.id}\n`;
6560
formatted += `size:${buf}${formatBytes(m.size)}\n`;
6661
formatted += `percent:${buf}${m.percent}%\n`;
@@ -94,7 +89,6 @@ const plugin = (opts = {}) => {
9489
modules.forEach((m) => {
9590
let bm = bundle.modules[m.id];
9691
bm.id = bm.id || m.id;
97-
bm.isEntry = bm.isEntry || m.isEntry;
9892
bm.dependencies = m.dependencies || [];
9993
});
10094
modules = Object.keys(bundle.modules).map((k) => bundle.modules[k]);
@@ -109,11 +103,7 @@ const plugin = (opts = {}) => {
109103
resolve(null);
110104
if (!chunk || !chunk.orderedModules) return
111105
modules = chunk.orderedModules.map((m) => {
112-
return {
113-
id: m.id,
114-
isEntry: m.isEntryPoint,
115-
dependencies: m.dependencies.map((d) => d.id)
116-
}
106+
return {id: m.id, dependencies: m.dependencies.map((d) => d.id)}
117107
});
118108
}),
119109
generateBundle: runAnalysis,

module.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@ export const analyze = (bundle, opts = {}, format = false) => {
1616
let { root, limit, filter } = opts
1717
root = root || (process && process.cwd ? process.cwd() : null)
1818
let deps = {}
19-
let entrySize
2019
let bundleSize = 0
2120
let bundleModules = bundle.modules
2221

2322
return new Promise((resolve, reject) => {
2423
let modules = bundleModules.map((m, i) => {
25-
let { id, originalLength: origSize, renderedLength, isEntry, code } = m
24+
let { id, originalLength: origSize, renderedLength, code } = m
2625
id = id.replace(root, '')
2726
let size = renderedLength
2827
if (!size && size !== 0) size = code ? Buffer.byteLength(code, 'utf8') : 0
29-
if (isEntry) entrySize = size
3028
bundleSize += size
3129

3230
if (Array.isArray(filter) && !filter.some((f) => id.match(f))) return null
@@ -38,11 +36,9 @@ export const analyze = (bundle, opts = {}, format = false) => {
3836
deps[d].push(id)
3937
})
4038

41-
return {id, size, origSize, isEntry}
39+
return {id, size, origSize}
4240
}).filter((m) => m)
4341

44-
if (entrySize) bundleSize = entrySize
45-
4642
modules.sort((a, b) => b.size - a.size)
4743
if (limit || limit === 0) modules = modules.slice(0, limit)
4844
modules.forEach((m) => {
@@ -58,7 +54,6 @@ export const analyze = (bundle, opts = {}, format = false) => {
5854
let formatted = `${borderX}${heading}${displaySize}${borderX}`
5955

6056
modules.forEach((m) => {
61-
if (m.isEntry) return
6257
formatted += `file:${buf}${m.id}\n`
6358
formatted += `size:${buf}${formatBytes(m.size)}\n`
6459
formatted += `percent:${buf}${m.percent}%\n`
@@ -92,7 +87,6 @@ export const plugin = (opts = {}) => {
9287
modules.forEach((m) => {
9388
let bm = bundle.modules[m.id]
9489
bm.id = bm.id || m.id
95-
bm.isEntry = bm.isEntry || m.isEntry
9690
bm.dependencies = m.dependencies || []
9791
})
9892
modules = Object.keys(bundle.modules).map((k) => bundle.modules[k])
@@ -107,11 +101,7 @@ export const plugin = (opts = {}) => {
107101
resolve(null)
108102
if (!chunk || !chunk.orderedModules) return
109103
modules = chunk.orderedModules.map((m) => {
110-
return {
111-
id: m.id,
112-
isEntry: m.isEntryPoint,
113-
dependencies: m.dependencies.map((d) => d.id)
114-
}
104+
return {id: m.id, dependencies: m.dependencies.map((d) => d.id)}
115105
})
116106
}),
117107
generateBundle: runAnalysis,

rollup.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict'
22

3-
// how meta
3+
import { plugin } from './module'
44
export default {
5+
plugins: [plugin()],
56
input: 'module.js',
67
output: {
78
file: 'index.js',

test/fixtures/bundle.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
'use strict'
22

33
import { aSmallConst } from './import-a'
4-
import { anotherSmallConst } from './import-b'
4+
import {
5+
anotherSmallConst,
6+
smallNestedConstA,
7+
largeNestedConstB
8+
} from './import-b'
59

6-
console.log(aSmallConst.length, anotherSmallConst.length)
10+
console.log(
11+
aSmallConst.length,
12+
anotherSmallConst.length,
13+
smallNestedConstA.length,
14+
largeNestedConstB.length
15+
)

test/fixtures/import-b.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
'use strict'
22

3+
export { smallNestedConstA, largeNestedConstB } from './import-c'
4+
35
export const anotherSmallConst = 'abcd'

test/fixtures/import-c.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict'
2+
3+
import { virtualInsanity } from './import-d'
4+
5+
export const smallNestedConstA = 'jamiroquai'
6+
export const largeNestedConstB = virtualInsanity

test/fixtures/import-d.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict'
2+
3+
export const virtualInsanity = `
4+
Oh yeah, what we're living in (let me tell ya)
5+
It's a wonder man can eat at all
6+
When things are big that should be small
7+
Who can tell what magic spells we'll be doing for us
8+
And I'm giving all my love to this world
9+
Only to be told
10+
I can't see
11+
I can't breathe
12+
No more will we be
13+
And nothing's going to change the way we live
14+
'Cause we can always take but never give
15+
And now that things are changing for the worse,
16+
See, its a crazy world we're living in
17+
And I just can't see that half of us immersed in sin
18+
Is all we have to give these
19+
Futures made of virtual insanity now
20+
Always seem to, be governed by this love we have
21+
For useless, twisting, our new technology
22+
Oh, now there is no sound for we all live underground
23+
And I'm thinking what a mess we're in
24+
Hard to know where to begin
25+
If I could slip the sickly ties that earthly man has made
26+
And now every mother, can choose the color
27+
Of her child
28+
That's not nature's way
29+
Well that's what they said yesterday
30+
There's nothing left to do but pray
31+
I think it's time I found a new religion
32+
Whoa, it's so insane
33+
To synthesize another strain
34+
There's something in these
35+
Futures that we have to be told
36+
Futures made of virtual insanity now
37+
Always seem to, be governed by this love we have
38+
For useless, twisting, our new technology
39+
Oh, now there is no sound for we all live underground
40+
Now there is no sound
41+
If we all live underground
42+
And now it's virtual insanity
43+
Forget your virtual reality
44+
Oh, there's nothing so bad
45+
I know yeah
46+
Of this virtual insanity, we're livin' in
47+
Has got to change, yeah
48+
Things, will never be the same
49+
And I can't go on
50+
While we're livin' in oh, oh virtual insanity
51+
Oh, this world, has got to change
52+
'Cause I just, I just can't keep going on, it was virtual
53+
Virtual insanity that we're livin' in, that we're livin' in
54+
That virtual insanity is what it is
55+
Futures made of virtual insanity now
56+
Always seem to, be governed by this love we have
57+
For useless, twisting, our new technology
58+
Oh, now there is no sound for we all live underground
59+
Futures made of virtual insanity now
60+
Always seem to, be governed by this love we have
61+
For useless, twisting, our new technology
62+
Oh, now there is no sound for we all live underground
63+
Living, virtual insanity
64+
Living, virtual insanity
65+
Living, virtual insanity
66+
Living, virtual insanity
67+
Virtual insanity is what we're living in
68+
`

test/fixtures/output.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'use strict';
2+
3+
const aSmallConst = '1234';
4+
5+
const virtualInsanity = `
6+
Oh yeah, what we're living in (let me tell ya)
7+
It's a wonder man can eat at all
8+
When things are big that should be small
9+
Who can tell what magic spells we'll be doing for us
10+
And I'm giving all my love to this world
11+
Only to be told
12+
I can't see
13+
I can't breathe
14+
No more will we be
15+
And nothing's going to change the way we live
16+
'Cause we can always take but never give
17+
And now that things are changing for the worse,
18+
See, its a crazy world we're living in
19+
And I just can't see that half of us immersed in sin
20+
Is all we have to give these
21+
Futures made of virtual insanity now
22+
Always seem to, be governed by this love we have
23+
For useless, twisting, our new technology
24+
Oh, now there is no sound for we all live underground
25+
And I'm thinking what a mess we're in
26+
Hard to know where to begin
27+
If I could slip the sickly ties that earthly man has made
28+
And now every mother, can choose the color
29+
Of her child
30+
That's not nature's way
31+
Well that's what they said yesterday
32+
There's nothing left to do but pray
33+
I think it's time I found a new religion
34+
Whoa, it's so insane
35+
To synthesize another strain
36+
There's something in these
37+
Futures that we have to be told
38+
Futures made of virtual insanity now
39+
Always seem to, be governed by this love we have
40+
For useless, twisting, our new technology
41+
Oh, now there is no sound for we all live underground
42+
Now there is no sound
43+
If we all live underground
44+
And now it's virtual insanity
45+
Forget your virtual reality
46+
Oh, there's nothing so bad
47+
I know yeah
48+
Of this virtual insanity, we're livin' in
49+
Has got to change, yeah
50+
Things, will never be the same
51+
And I can't go on
52+
While we're livin' in oh, oh virtual insanity
53+
Oh, this world, has got to change
54+
'Cause I just, I just can't keep going on, it was virtual
55+
Virtual insanity that we're livin' in, that we're livin' in
56+
That virtual insanity is what it is
57+
Futures made of virtual insanity now
58+
Always seem to, be governed by this love we have
59+
For useless, twisting, our new technology
60+
Oh, now there is no sound for we all live underground
61+
Futures made of virtual insanity now
62+
Always seem to, be governed by this love we have
63+
For useless, twisting, our new technology
64+
Oh, now there is no sound for we all live underground
65+
Living, virtual insanity
66+
Living, virtual insanity
67+
Living, virtual insanity
68+
Living, virtual insanity
69+
Virtual insanity is what we're living in
70+
`;
71+
72+
const smallNestedConstA = 'jamiroquai';
73+
const largeNestedConstB = virtualInsanity;
74+
75+
const anotherSmallConst = 'abcd';
76+
77+
console.log(
78+
aSmallConst.length,
79+
anotherSmallConst.length,
80+
smallNestedConstA.length,
81+
largeNestedConstB.length
82+
);

test/index.js renamed to test/test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ rollers.forEach(({rollup, version, opts, noTreeshake}) => {
110110
test(`${version}: tree shaking is accounted for`, async (assert) => {
111111
let results
112112
let onAnalysis = (r) => { results = r }
113-
let rollOpts = Object.assign({plugins: [plugin({onAnalysis})]}, opts)
113+
let plugins = [plugin({onAnalysis})]
114+
let rollOpts = Object.assign({}, opts, {plugins})
114115
let bundle = await rollup(rollOpts)
115-
await bundle.generate({format: 'cjs'})
116+
let output = {file: join(fixtures, 'output.js'), format: 'cjs'}
117+
await bundle.write(output)
116118
let imported = results.find((r) => r.id.indexOf('import-a') !== -1)
117119
assert.is(imported.size, 27)
118120
})

0 commit comments

Comments
 (0)