diff --git a/.eslintignore b/.eslintignore index 579a91c8..f76b2b97 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,9 +1,9 @@ # Generated code lib -public +reporter-treemap/public # Vendor code -client/vendor +reporter-treemap/client/vendor # Test fixtures test/bundles diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 5dfa1791..55a394b2 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -8,6 +8,7 @@ const { white, cyan } = gutil.colors; const NODE_SRC = ['src/**/*.js']; const NODE_DEST = resolve('lib'); +const VIEWER_DEST = resolve('reporter-treemap', 'public'); const cli = require('commander') .usage(' [options]') @@ -57,7 +58,7 @@ gulp.task('build', ['scripts.viewer', 'scripts.node']); gulp.task('scripts.viewer', ['scripts.node', 'clean.scripts.viewer'], () => { const webpack = require('webpack'); - const config = require('./webpack.config')({ + const config = require('./reporter-treemap/webpack.config')({ env: cli.env, analyze: cli.analyze }); @@ -82,7 +83,7 @@ gulp.task('scripts.viewer', ['scripts.node', 'clean.scripts.viewer'], () => { gulp.task('clean.scripts.viewer', () => { const del = require('del'); - return del('public'); + return del(VIEWER_DEST); }); gulp.task('scripts.node', ['clean.scripts.node'], () => diff --git a/client/.eslintrc.json b/reporter-treemap/client/.eslintrc.json similarity index 98% rename from client/.eslintrc.json rename to reporter-treemap/client/.eslintrc.json index f414673e..894ca55c 100644 --- a/client/.eslintrc.json +++ b/reporter-treemap/client/.eslintrc.json @@ -1,5 +1,5 @@ { - "extends": "../.eslintrc.json", + "extends": "../../.eslintrc.json", "parserOptions": { "ecmaFeatures": { "jsx": true diff --git a/client/components/ModulesTreemap.css b/reporter-treemap/client/components/ModulesTreemap.css similarity index 100% rename from client/components/ModulesTreemap.css rename to reporter-treemap/client/components/ModulesTreemap.css diff --git a/client/components/ModulesTreemap.jsx b/reporter-treemap/client/components/ModulesTreemap.jsx similarity index 100% rename from client/components/ModulesTreemap.jsx rename to reporter-treemap/client/components/ModulesTreemap.jsx diff --git a/client/components/Sidebar.css b/reporter-treemap/client/components/Sidebar.css similarity index 100% rename from client/components/Sidebar.css rename to reporter-treemap/client/components/Sidebar.css diff --git a/client/components/Sidebar.jsx b/reporter-treemap/client/components/Sidebar.jsx similarity index 100% rename from client/components/Sidebar.jsx rename to reporter-treemap/client/components/Sidebar.jsx diff --git a/client/components/Switcher.css b/reporter-treemap/client/components/Switcher.css similarity index 100% rename from client/components/Switcher.css rename to reporter-treemap/client/components/Switcher.css diff --git a/client/components/Switcher.jsx b/reporter-treemap/client/components/Switcher.jsx similarity index 100% rename from client/components/Switcher.jsx rename to reporter-treemap/client/components/Switcher.jsx diff --git a/client/components/SwitcherItem.jsx b/reporter-treemap/client/components/SwitcherItem.jsx similarity index 100% rename from client/components/SwitcherItem.jsx rename to reporter-treemap/client/components/SwitcherItem.jsx diff --git a/client/components/Tooltip.css b/reporter-treemap/client/components/Tooltip.css similarity index 100% rename from client/components/Tooltip.css rename to reporter-treemap/client/components/Tooltip.css diff --git a/client/components/Tooltip.jsx b/reporter-treemap/client/components/Tooltip.jsx similarity index 100% rename from client/components/Tooltip.jsx rename to reporter-treemap/client/components/Tooltip.jsx diff --git a/client/components/Treemap.jsx b/reporter-treemap/client/components/Treemap.jsx similarity index 100% rename from client/components/Treemap.jsx rename to reporter-treemap/client/components/Treemap.jsx diff --git a/client/vendor/carrotsearch.foamtree.js b/reporter-treemap/client/vendor/carrotsearch.foamtree.js similarity index 100% rename from client/vendor/carrotsearch.foamtree.js rename to reporter-treemap/client/vendor/carrotsearch.foamtree.js diff --git a/client/viewer.css b/reporter-treemap/client/viewer.css similarity index 100% rename from client/viewer.css rename to reporter-treemap/client/viewer.css diff --git a/client/viewer.jsx b/reporter-treemap/client/viewer.jsx similarity index 100% rename from client/viewer.jsx rename to reporter-treemap/client/viewer.jsx diff --git a/src/viewer/generateReport.js b/reporter-treemap/generateReport.js similarity index 96% rename from src/viewer/generateReport.js rename to reporter-treemap/generateReport.js index f83d5360..20cd276a 100644 --- a/src/viewer/generateReport.js +++ b/reporter-treemap/generateReport.js @@ -8,7 +8,7 @@ const { bold } = require('chalk'); module.exports = generateReport; -const projectRoot = path.resolve(__dirname, '..', '..'); +const projectRoot = __dirname; function generateReport(chartData, opts) { if (!chartData) { diff --git a/src/viewer/index.js b/reporter-treemap/index.js similarity index 100% rename from src/viewer/index.js rename to reporter-treemap/index.js diff --git a/src/viewer/startServer.js b/reporter-treemap/startServer.js similarity index 93% rename from src/viewer/startServer.js rename to reporter-treemap/startServer.js index 5acd5d95..01acb40c 100644 --- a/src/viewer/startServer.js +++ b/reporter-treemap/startServer.js @@ -1,9 +1,8 @@ -const path = require('path'); const express = require('express'); const opener = require('opener'); const { bold } = require('chalk'); -const projectRoot = path.resolve(__dirname, '..', '..'); +const projectRoot = __dirname; module.exports = startServer; diff --git a/views/script.ejs b/reporter-treemap/views/script.ejs similarity index 100% rename from views/script.ejs rename to reporter-treemap/views/script.ejs diff --git a/views/viewer.ejs b/reporter-treemap/views/viewer.ejs similarity index 100% rename from views/viewer.ejs rename to reporter-treemap/views/viewer.ejs diff --git a/webpack.config.js b/reporter-treemap/webpack.config.js similarity index 93% rename from webpack.config.js rename to reporter-treemap/webpack.config.js index 64d29ec0..9645cb4e 100644 --- a/webpack.config.js +++ b/reporter-treemap/webpack.config.js @@ -1,5 +1,6 @@ const webpack = require('webpack'); -const BundleAnalyzePlugin = require('./lib/BundleAnalyzerPlugin'); +const { resolve } = require('path'); +const BundleAnalyzePlugin = require('../lib/BundleAnalyzerPlugin'); module.exports = opts => { opts = { @@ -12,7 +13,7 @@ module.exports = opts => { context: __dirname, entry: './client/viewer', output: { - path: 'public', + path: resolve(__dirname, 'public'), filename: 'viewer.js', publicPath: '/' }, diff --git a/src/BundleAnalyzerPlugin.js b/src/BundleAnalyzerPlugin.js index b5ba1074..3c190da4 100644 --- a/src/BundleAnalyzerPlugin.js +++ b/src/BundleAnalyzerPlugin.js @@ -5,7 +5,6 @@ const { bold } = require('chalk'); const getChartData = require('./chartData'); const Logger = require('./Logger'); -const viewer = require('./viewer'); class BundleAnalyzerPlugin { @@ -19,6 +18,7 @@ class BundleAnalyzerPlugin { statsFilename: 'stats.json', statsOptions: null, logLevel: 'info', + reporter: require('../reporter-treemap'), // deprecated startAnalyzer: true, ...opts @@ -79,13 +79,14 @@ class BundleAnalyzerPlugin { } startAnalyzerServer(stats) { + const reporter = this.opts.reporter; const bundleDir = this.compiler.outputPath; const logger = this.logger; const chartData = getChartData({ logger, bundleStats: stats, bundleDir }); if (!chartData) return; - viewer.startServer(chartData, { + reporter.startServer(chartData, { openBrowser: this.opts.openAnalyzer, port: this.opts.analyzerPort, bundleDir, @@ -94,13 +95,14 @@ class BundleAnalyzerPlugin { } generateStaticReport(stats) { + const reporter = this.opts.reporter; const bundleDir = this.compiler.outputPath; const logger = this.logger; const chartData = getChartData({ logger, bundleStats: stats, bundleDir }); if (!chartData) return; - viewer.generateReport(chartData, { + reporter.generateReport(chartData, { openBrowser: this.opts.openAnalyzer, reportFilename: this.opts.reportFilename, bundleDir, diff --git a/src/bin/analyzer.js b/src/bin/analyzer.js index 88d789c6..d3484726 100755 --- a/src/bin/analyzer.js +++ b/src/bin/analyzer.js @@ -8,7 +8,6 @@ const commander = require('commander'); const { magenta } = require('chalk'); const getChartData = require('../chartData'); -const viewer = require('../viewer'); const Logger = require('../Logger'); const program = commander @@ -38,6 +37,12 @@ const program = commander Number, 8888 ) + .option( + '-R, --reporter ', + 'Reporter package that will be used to generate the report.' + + br('Default is the built-in `reporter-treemap`.'), + require.resolve('../../reporter-treemap') + ) .option( '-r, --report ', 'Path to bundle report file that will be generated in `static` mode.' + @@ -54,6 +59,7 @@ let { mode, port, report: reportFilename, + reporter: reporterModulePath, open: openBrowser, args: [bundleStatsFile, bundleDir] } = program; @@ -74,6 +80,15 @@ try { process.exit(1); } +let viewer; +try { + viewer = require(reporterModulePath); +} catch (err) { + console.error(`Couldn't resolve reporter for path "${reporterModulePath}".`); + console.error('Reporters should either be installed in node_modules or provided as an absolute path.'); + process.exit(1); +} + const logger = new Logger(); const chartData = getChartData({ logger, bundleStats, bundleDir }); @@ -84,6 +99,11 @@ if (!chartData) { } if (mode === 'server') { + if (typeof viewer.startServer !== 'function') { + console.error(`The given reporter "${reporterModulePath}" does not expose .startServer() ` + + 'function and so can\'t be used for server mode reporting.'); + process.exit(1); + } viewer.startServer(chartData, { openBrowser, port, @@ -91,6 +111,11 @@ if (mode === 'server') { logger }); } else { + if (typeof viewer.generateReport !== 'function') { + console.error(`The given reporter "${reporterModulePath}" does not expose .generateReport() ` + + 'function and so can\'t be used for static mode reporting.'); + process.exit(1); + } viewer.generateReport(chartData, { openBrowser, reportFilename: resolve(reportFilename), diff --git a/src/index.js b/src/index.js index 009695ef..b4189d7f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -const { startServer } = require('./viewer'); +const { startServer } = require('../reporter-treemap'); module.exports = { // Deprecated