Skip to content

Commit

Permalink
Add default export (fixes #6), warn on rollup < 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
doesdev committed Jan 5, 2019
1 parent ee60a38 commit dc6eb51
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $ npm install --save-dev rollup-plugin-analyzer

### from rollup config
```js
import { plugin as analyze } from 'rollup-plugin-analyzer'
import analyze from 'rollup-plugin-analyzer'

export default {
entry: 'module.js',
Expand All @@ -42,7 +42,7 @@ export default {
### from build script
```js
import { rollup } from 'rollup'
import { plugin as analyze } from 'rollup-plugin-analyzer'
import analyze from 'rollup-plugin-analyzer'

rollup({
entry: 'main.js',
Expand All @@ -53,7 +53,7 @@ rollup({
### CI usage example
```js
import { rollup } from 'rollup'
import { plugin as analyze } from 'rollup-plugin-analyzer'
import analyze from 'rollup-plugin-analyzer'
const limitBytes = 1e6

const onAnalysis = ({bundleSize}) => {
Expand Down
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,19 @@ const plugin = (opts = {}) => {

return {
name: 'rollup-plugin-analyzer',
buildStart: function () {
const ctx = this || {};
if (!ctx.meta || +(ctx.meta.rollupVersion || 0).charAt(0) < 1) {
const msg = `` +
`rollup-plugin-analyzer: Rollup version not supported\n${tab}` +
`Starting with 3.0.0 only Rollup >= 1.0.0 is supported\n${tab}` +
`Use rollup-plugin-analyzer 2.1.0 for prior Rollup versions`;
console.error(msg);
}
},
generateBundle: function (outOpts, bundle, isWrite) {
const ctx = this || {};
if (!ctx.meta || +(ctx.meta.rollupVersion || 0).charAt(0) < 1) return null
const getDeps = (id) => {
return ctx.getModuleInfo ? ctx.getModuleInfo(id).importedIds : []
};
Expand All @@ -160,7 +171,10 @@ const plugin = (opts = {}) => {
}
};

Object.assign(plugin, { plugin, analyze, formatted, reporter });

exports.reporter = reporter;
exports.analyze = analyze;
exports.formatted = formatted;
exports.plugin = plugin;
exports.default = plugin;
15 changes: 15 additions & 0 deletions module.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,19 @@ export const plugin = (opts = {}) => {

return {
name: 'rollup-plugin-analyzer',
buildStart: function () {
const ctx = this || {}
if (!ctx.meta || +(ctx.meta.rollupVersion || 0).charAt(0) < 1) {
const msg = `` +
`rollup-plugin-analyzer: Rollup version not supported\n${tab}` +
`Starting with 3.0.0 only Rollup >= 1.0.0 is supported\n${tab}` +
`Use rollup-plugin-analyzer 2.1.0 for prior Rollup versions`
console.error(msg)
}
},
generateBundle: function (outOpts, bundle, isWrite) {
const ctx = this || {}
if (!ctx.meta || +(ctx.meta.rollupVersion || 0).charAt(0) < 1) return null
const getDeps = (id) => {
return ctx.getModuleInfo ? ctx.getModuleInfo(id).importedIds : []
}
Expand All @@ -157,3 +168,7 @@ export const plugin = (opts = {}) => {
}
}
}

Object.assign(plugin, { plugin, analyze, formatted, reporter })

export default plugin
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rollup-plugin-analyzer",
"version": "2.1.0",
"version": "3.0.0",
"description": "Mad metrics for your rollup bundles, know all the things",
"main": "index.js",
"module": "module.js",
Expand All @@ -9,8 +9,8 @@
"module.js"
],
"scripts": {
"test": "rollup -c && ava --verbose --failFast --serial",
"precommit": "rollup -c && ava && git add index.js",
"test": "node ./node_modules/rollup/bin/rollup -c && ava --verbose --failFast --serial",
"precommit": "node ./node_modules/rollup/bin/rollup -c && ava && git add index.js",
"changelog": "auto-changelog -u -l 50 && git add CHANGELOG.md"
},
"repository": {
Expand Down Expand Up @@ -42,6 +42,7 @@
"ava": "^1.0.1",
"husky": "^1.3.1",
"rollup": "npm:rollup@latest",
"rollup60": "npm:[email protected]",
"rollup100": "npm:[email protected]"
},
"dependencies": {}
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

import { plugin } from './module'
import analyzer from './module'

export default {
plugins: [plugin()],
plugins: [analyzer()],
input: 'module.js',
output: {
file: 'index.js',
Expand Down
15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import test from 'ava'
import { analyze, formatted, plugin } from './../index'
import { resolve as resolvePath, join, basename } from 'path'
import { rollup as rollupLatest } from 'rollup'
import { rollup as rollup60 } from 'rollup60'
import { rollup as rollup100 } from 'rollup100'
const skipFormatted = true
const fixtures = resolvePath(__dirname, 'fixtures')
Expand Down Expand Up @@ -234,3 +235,17 @@ rollers.forEach(({ rollup, version, opts, noTreeshake }) => {
})
}
})

test(`rollup < 1.0.0 prints warning about support`, async (assert) => {
let results = ''
const oldCslErr = console.error
console.error = (...args) => {
results += args.join()
}
let rollOpts = Object.assign({ plugins: [plugin()] }, baseOpts)
let bundle = await rollup60(rollOpts)
await bundle.generate({ format: 'cjs' })
const expect = `rollup-plugin-analyzer: Rollup version not supported`
assert.is(results.split('\n')[0], expect)
console.error = oldCslErr
})
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2783,6 +2783,14 @@ rimraf@^2.2.8, rimraf@^2.6.1:
"@types/node" "*"
acorn "^6.0.4"

"rollup60@npm:[email protected]":
version "0.60.7"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.60.7.tgz#2b62ef9306f719b1ab85a7814b3e6596ac51fae8"
integrity sha512-Uj5I1A2PnDgA79P+v1dsNs1IHVydNgeJdKWRfoEJJdNMmyx07TRYqUtPUINaZ/gDusncFy1SZsT3lJnBBI8CGw==
dependencies:
"@types/estree" "0.0.39"
"@types/node" "*"

run-node@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/run-node/-/run-node-1.0.0.tgz#46b50b946a2aa2d4947ae1d886e9856fd9cabe5e"
Expand Down

0 comments on commit dc6eb51

Please sign in to comment.