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

feat(*): Add initial version #1

Merged
merged 3 commits into from
Apr 10, 2017
Merged
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Dependencies
node_modules

# Logs
*.log
8 changes: 8 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Dependencies
node_modules

# Unnecessary stuff
.nvmrc

# Logs
*.log
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.8.0
Empty file added CHANGELOG.md
Empty file.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
<div align="center">

# simple-progress-webpack-plugin
WIP

**A simple text-based progress plugin for Webpack, coming with four different logging output formats.**

</div>

<br>

*TODO*
39 changes: 39 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

// Disable the deprecation warning, popping up in the middle of the process
process.noDeprecation = true;

const CompactLogger = require( './logger/compact-logger' );
const MinimalLogger = require( './logger/minimal-logger' );
const VerboseLogger = require( './logger/verbose-logger' );
const ExpandedLogger = require( './logger/expanded-logger' );

/**
* Simple Progress Plugin for Webpack
*
* @param options - Custom options
*/
module.exports = function SimpleProgressWebpackPlugin( options ) {

// Merge options
let internalOptions = {
format: ( options && options.hasOwnProperty( 'format' ) ) ? options.format : 'compact'
};

// Return the correct progress plugin
switch( internalOptions.format ) {
case 'minimal':
return MinimalLogger();
case 'compact':
return CompactLogger();
case 'expanded':
case 'extended':
return ExpandedLogger();
case 'verbose':
case 'debug':
return VerboseLogger();
default:
return CompactLogger();
}

};
175 changes: 175 additions & 0 deletions logger/compact-logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
'use strict';

const path = require( 'path' );

const chalk = require( 'chalk' );
const figures = require( 'figures' );
const log = require( 'log-update' );

const ProgressPlugin = require( 'webpack/lib/ProgressPlugin' );

/**
* Compact Logger
*/
module.exports = function CompactLogger() {

const absoluteProjectPath = `${ path.resolve( '.' ).toString() }`;
const startTime = new Date().getTime();
let previousStep = 0;
let logLines = [];

// Initial log
log( 'Webpack: Starting ...' );

/**
* Use the webpack-internal progress plugin as the base of the logger
*/
return new ProgressPlugin( ( progress, message, moduleProgress, activeModules, moduleName ) => {

// Reset log output
logLines = [];

// STEP 0: HEADER
logLines.push( chalk.white( 'Webpack: Starting ...\n' ) );

// STEP 1: COMPILATION
if ( progress >= 0 && progress < 0.1 ) {

// Skip if we jumped back a step, else update the step counter
if ( previousStep > 1 ) {
return;
}
previousStep = 1;

logLines.push( chalk.white( ` ${ figures.pointer } Compile modules` ) );

} else if ( progress >= 0.1 ) {

logLines.push( chalk.green( ` ${ figures.tick } Compile modules` ) );

}

// STEP 2: BUILDING
if ( progress >= 0.1 && progress <= 0.7 ) {

// Skip if we jumped back a step, else update the step counter
if ( previousStep > 2 ) {
return;
}
previousStep = 2;

// Log progress line (with sub-progress indicator)
const subProgress = Math.round( ( progress - 0.1 ) * 10000 / 60 );
logLines.push( chalk.white( ` ${ figures.pointer } Build modules (${ subProgress }%)` ) );

// Log additional information (if possible)
if ( moduleName !== undefined ) {

let betterModuleName = moduleName;

// Only show the file that is actually being processed (and remove all details about used loaders)
if ( betterModuleName.indexOf( '!' ) !== -1 ) {
let splitModuleName = betterModuleName.split( '!' );
betterModuleName = splitModuleName[ splitModuleName.length - 1 ];
}

// Transform absolute paths into relative ones (to shorten the so so incredible long path)
if ( betterModuleName.indexOf( absoluteProjectPath ) !== -1 ) {
betterModuleName = betterModuleName.split( `${ absoluteProjectPath }\\` )[ 1 ];
}

// Improve the path presentation further by enforcing style consistency and removing unnecessary details
betterModuleName = betterModuleName
.replace( /\\/g, '/' )
.replace( './', '' )
.replace( 'multi ', '' );

// Add extra details about whether the currently processed module is an internal or external one
if ( betterModuleName.startsWith( 'node_modules' ) ) {
betterModuleName = `${ betterModuleName } ~ external`;
}
if ( betterModuleName.startsWith( 'src' ) ) {
betterModuleName = `${ betterModuleName } ~ internal`;
}

const [ betterModulesDone, betterAllModules ] = moduleProgress.split( '/' );
const moduleDetails = `${ betterModulesDone } of ${ betterAllModules } :: ${ betterModuleName }`;
logLines.push( chalk.grey( ` ${ figures.arrowRight } ${ moduleDetails }` ) );

}

} else if ( progress > 0.7 ) {

logLines.push( chalk.green( ` ${ figures.tick } Build modules` ) );

}

// STEP 3: OPTIMIZATION
if ( progress > 0.7 && progress < 0.95 ) {

// Skip if we jumped back a step, else update the step counter
if ( previousStep > 3 ) {
return;
}
previousStep = 3;

// Log progress line (with sub-progress indicator)
const subProgress = Math.round( ( progress - 0.71 ) * 10000 / 23 );
logLines.push( chalk.white( ` ${ figures.pointer } Optimize modules (${ subProgress }%)` ) );

const formattedMessage = `${ message[ 0 ].toUpperCase() }${ message.slice( 1 ) }`;
const formattedMessageExtra = progress === 0.91 ? ' -- may take a while' : ''; // Add some extra info (calming devs down)

logLines.push( chalk.grey( ` ${ figures.arrowRight } ${ formattedMessage }${ formattedMessageExtra } ...` ) );

} else if ( progress >= 0.95 ) {

logLines.push( chalk.green( ` ${ figures.tick } Optimize modules` ) );

}

// STEP 4: EMIT
if ( progress >= 0.95 && progress < 1 ) {

// Skip if we jumped back a step, else update the step counter
if ( previousStep > 4 ) {
return;
}
previousStep = 4;

logLines.push( chalk.white( ` ${ figures.pointer } Emit files` ) );

} else if ( progress === 1 ) {

logLines.push( chalk.green( ` ${ figures.tick } Emit files` ) );

}

// STEP 5: FOOTER
if ( progress === 1 ) {

// Skip if we already are done, else update the step counter
if ( previousStep === 5 ) {
return;
}
previousStep = 5;

// Calculate process time
const finishTime = new Date().getTime();
const processTime = ( ( finishTime - startTime ) / 1000 ).toFixed( 3 );

logLines.push( chalk.white( `\nWebpack: Finished after ${ processTime } seconds.\n` ) );

}

// Finally, let's bring those logs to da screen
log( logLines.join( '\n' ) );

// Finish log when we're done here
if ( progress === 1 ) {
log.done();
}

} );

};
143 changes: 143 additions & 0 deletions logger/expanded-logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
'use strict';

const path = require( 'path' );

const chalk = require( 'chalk' );
const figures = require( 'figures' );

const ProgressPlugin = require( 'webpack/lib/ProgressPlugin' );

/**
* Expanded Logger
*/
module.exports = function ExpandedLogger() {

const absoluteProjectPath = `${ path.resolve( '.' ).toString() }`;
const startTime = new Date().getTime();
let previousStep = 0;

// Initial log
console.log( chalk.white( 'Webpack: Starting ...' ) );

/**
* Use the webpack-internal progress plugin as the base of the logger
*/
return new ProgressPlugin( ( progress, message, moduleProgress, activeModules, moduleName ) => {

// STEP 1: COMPILATION
if ( progress >= 0 && progress < 0.1 ) {

// Skip if we jumped back a step, else update the step counter
if ( previousStep > 1 ) {
return;
} else if ( previousStep < 1 ) {
console.log( chalk.white( `\n ${ figures.pointer } Compile modules` ) );
}
previousStep = 1;

}

// STEP 2: BUILDING
if ( progress >= 0.1 && progress <= 0.7 ) {

// Skip if we jumped back a step, else update the step counter
if ( previousStep > 2 ) {
return;
} else if ( previousStep < 2 ) {
console.log( chalk.white( `\n ${ figures.pointer } Build modules` ) );
}
previousStep = 2;

// Log additional information (if possible)
if ( moduleName !== undefined ) {

const roundedSubProgress = Math.round( ( progress - 0.1 ) * 10000 / 60 );
let betterModuleName = moduleName;

// Only show the file that is actually being processed (and remove all details about used loaders)
if ( betterModuleName.indexOf( '!' ) !== -1 ) {
let splitModuleName = betterModuleName.split( '!' );
betterModuleName = splitModuleName[ splitModuleName.length - 1 ];
}

// Transform absolute paths into relative ones (to shorten the so so incredible long path)
if ( betterModuleName.indexOf( absoluteProjectPath ) !== -1 ) {
betterModuleName = betterModuleName.split( `${ absoluteProjectPath }\\` )[ 1 ];
}

// Improve the path presentation further by enforcing style consistency and removing unnecessary details
betterModuleName = betterModuleName
.replace( /\\/g, '/' )
.replace( './', '' )
.replace( 'multi ', '' );

// Add extra details about whether the currently processed module is an internal or external one
if ( betterModuleName.startsWith( 'node_modules' ) ) {
betterModuleName = `${ betterModuleName } ~ external`;
}
if ( betterModuleName.startsWith( 'src' ) ) {
betterModuleName = `${ betterModuleName } ~ internal`;
}

const [ betterModulesDone, betterAllModules ] = moduleProgress.split( '/' );
const moduleDetails = `${ betterModulesDone } of ${ betterAllModules } :: ${ betterModuleName }`;
console.log( chalk.grey( ` ${ figures.arrowRight } [${ roundedSubProgress }%] ${ moduleDetails }` ) );

}

}

// STEP 3: OPTIMIZATION
if ( progress > 0.7 && progress < 0.95 ) {

// Skip if we jumped back a step, else update the step counter
if ( previousStep > 3 ) {
return;
} else if ( previousStep < 3 ) {
console.log( chalk.white( `\n ${ figures.pointer } Optimize modules` ) );
}
previousStep = 3;

// Log progress line (with sub-progress indicator)
const subProgress = Math.round( ( progress - 0.71 ) * 10000 / 23 );

const formattedMessage = `${ message[ 0 ].toUpperCase() }${ message.slice( 1 ) }`;
const formattedMessageExtra = progress === 0.91 ? ' -- may take a while' : ''; // Add some extra info (calming devs down)

console.log( chalk.grey( ` ${ figures.arrowRight } [${ subProgress }%] ${ formattedMessage }${ formattedMessageExtra } ...` ) );

}

// STEP 4: EMIT
if ( progress >= 0.95 && progress < 1 ) {

// Skip if we jumped back a step, else update the step counter
if ( previousStep > 4 ) {
return;
} else if ( previousStep < 4 ) {
console.log( chalk.white( `\n ${ figures.pointer } Emit files` ) );
}
previousStep = 4;

}

// STEP 5: FOOTER
if ( progress === 1 ) {

// Skip if we jumped back a step, else update the step counter
if ( previousStep === 5 ) {
return;
}
previousStep = 5;

// Calculate process time
const finishTime = new Date().getTime();
const processTime = ( ( finishTime - startTime ) / 1000 ).toFixed( 3 );

console.log( chalk.white( `\nWebpack: Finished after ${ processTime } seconds.\n` ) );

}

} );

};
Loading