Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: Split treemap UI from webpack analyzer code #1

Open
wants to merge 3 commits into
base: split-reporter
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Generated code
lib
public
reporter-treemap/public

# Vendor code
client/vendor
reporter-treemap/client/vendor

# Test fixtures
test/bundles
Expand Down
5 changes: 3 additions & 2 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<task> [options]')
Expand Down Expand Up @@ -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
});
Expand All @@ -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'], () =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../.eslintrc.json",
"extends": "../../.eslintrc.json",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions webpack.config.js → reporter-treemap/webpack.config.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -12,7 +13,7 @@ module.exports = opts => {
context: __dirname,
entry: './client/viewer',
output: {
path: 'public',
path: resolve(__dirname, 'public'),
filename: 'viewer.js',
publicPath: '/'
},
Expand Down
8 changes: 5 additions & 3 deletions src/BundleAnalyzerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { bold } = require('chalk');

const getChartData = require('./chartData');
const Logger = require('./Logger');
const viewer = require('./viewer');

class BundleAnalyzerPlugin {

Expand All @@ -19,6 +18,7 @@ class BundleAnalyzerPlugin {
statsFilename: 'stats.json',
statsOptions: null,
logLevel: 'info',
reporter: require('../reporter-treemap'),
// deprecated
startAnalyzer: true,
...opts
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
27 changes: 26 additions & 1 deletion src/bin/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,6 +37,12 @@ const program = commander
Number,
8888
)
.option(
'-R, --reporter <node package>',
'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 <file>',
'Path to bundle report file that will be generated in `static` mode.' +
Expand All @@ -54,6 +59,7 @@ let {
mode,
port,
report: reportFilename,
reporter: reporterModulePath,
open: openBrowser,
args: [bundleStatsFile, bundleDir]
} = program;
Expand All @@ -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 });

Expand All @@ -84,13 +99,23 @@ 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,
bundleDir,
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),
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { startServer } = require('./viewer');
const { startServer } = require('../reporter-treemap');

module.exports = {
// Deprecated
Expand Down