diff --git a/README.md b/README.md index 28f35c1..4c51534 100644 --- a/README.md +++ b/README.md @@ -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', @@ -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', @@ -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}) => { diff --git a/index.js b/index.js index 664fcca..2fa9abc 100644 --- a/index.js +++ b/index.js @@ -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 : [] }; @@ -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; diff --git a/module.js b/module.js index 63ad73b..d83d027 100644 --- a/module.js +++ b/module.js @@ -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 : [] } @@ -157,3 +168,7 @@ export const plugin = (opts = {}) => { } } } + +Object.assign(plugin, { plugin, analyze, formatted, reporter }) + +export default plugin diff --git a/package.json b/package.json index b831551..098e2d9 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": { @@ -42,6 +42,7 @@ "ava": "^1.0.1", "husky": "^1.3.1", "rollup": "npm:rollup@latest", + "rollup60": "npm:rollup@0.60.x", "rollup100": "npm:rollup@1.0.x" }, "dependencies": {} diff --git a/rollup.config.js b/rollup.config.js index 170e3ed..b2f7675 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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', diff --git a/test/test.js b/test/test.js index 6dd3d40..769f75a 100644 --- a/test/test.js +++ b/test/test.js @@ -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') @@ -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 +}) diff --git a/yarn.lock b/yarn.lock index 71f1bb5..80c8d0d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2783,6 +2783,14 @@ rimraf@^2.2.8, rimraf@^2.6.1: "@types/node" "*" acorn "^6.0.4" +"rollup60@npm:rollup@0.60.x": + 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"