| 170 | }); |
| 171 | |
| 172 | const plugin = (opts = {}) => { |
| 173 | const writeTo = opts.writeTo || (opts.stdout ? console.log : console.error); |
| 174 | |
| 175 | const onAnalysis = (analysis) => { |
| 176 | if (typeof opts.onAnalysis === 'function') opts.onAnalysis(analysis); |
| 177 | if (!opts.skipFormatted) writeTo(reporter(analysis, opts)); |
| 178 | }; |
| 179 | |
| 180 | return { |
| 181 | name: 'rollup-plugin-analyzer', |
| 182 | buildStart: function () { |
| 183 | const ctx = this || {}; |
| 184 | if (!ctx.meta || +(ctx.meta.rollupVersion || 0).charAt(0) < 1) { |
| 185 | const msg = '' + |
| 186 | `rollup-plugin-analyzer: Rollup version not supported\n${tab}` + |
| 187 | `Starting with 3.0.0 only Rollup >= 1.0.0 is supported\n${tab}` + |
| 188 | 'Use rollup-plugin-analyzer 2.1.0 for prior Rollup versions'; |
| 189 | console.error(msg); |
| 190 | } |
| 191 | }, |
| 192 | generateBundle: function (outOpts, bundle, isWrite) { |
| 193 | const ctx = this || {}; |
| 194 | if (!ctx.meta || +(ctx.meta.rollupVersion || 0).charAt(0) < 1) return null |
| 195 | const getDeps = (id) => { |
| 196 | return ctx.getModuleInfo ? ctx.getModuleInfo(id).importedIds : [] |
| 197 | }; |
| 198 | |
| 199 | return new Promise((resolve, reject) => { |
| 200 | resolve(); |
| 201 | |
| 202 | const modules = []; |
| 203 | Object.entries(bundle).forEach(([outId, { modules: bundleMods }]) => { |
| 204 | bundleMods = bundleMods || {}; |
| 205 | Object.entries(bundleMods).forEach(([id, moduleInfo]) => { |
| 206 | const dependencies = getDeps(id); |
| 207 | modules.push(Object.assign({}, moduleInfo, { id, dependencies })); |
| 208 | }); |
| 209 | }); |
| 210 | |
| 211 | return analyze({ modules }, opts).then(onAnalysis).catch(console.error) |
| 212 | }) |
| 213 | } |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | Object.assign(plugin, { plugin, analyze, formatted, reporter }); |
| 218 | |