22
33// setup
44import test from 'ava'
5+ // import { promises as fs } from 'fs'
56import { analyze , formatted , plugin } from './../index'
6- import { resolve , join } from 'path'
7+ import { resolve , join , basename } from 'path'
78import { rollup as rollupLatest } from 'rollup'
89import { rollup as rollup60 } from 'rollup60'
910import { rollup as rollup55 } from 'rollup55'
@@ -14,15 +15,15 @@ const skipFormatted = true
1415const fixtures = resolve ( __dirname , 'fixtures' )
1516const baseOpts = {
1617 input : join ( fixtures , 'bundle-a.js' ) ,
17- output : { format : 'cjs' }
18+ output : { format : 'cjs' }
1819}
1920const oldOpts = {
2021 entry : join ( fixtures , 'bundle-a.js' ) ,
2122 format : 'cjs'
2223}
2324const 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}
2728const expectHeader = `
2829-----------------------------
@@ -33,16 +34,16 @@ const headerLength = expectHeader.length
3334
3435// test against many versions of rollup
3536const 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