Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.

Commit

Permalink
Move logging into plugin, use svgo for svg compression
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Sep 2, 2017
1 parent 93d8559 commit 1ac3a30
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 76 deletions.
75 changes: 0 additions & 75 deletions lib/logger.js

This file was deleted.

25 changes: 24 additions & 1 deletion lib/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const rimraf = require('rimraf')
const mkdirp = require('mkdirp')
const LogPlugin = require('./webpack/log-plugin')
const getConfig = require('./get-config')

const rimrafAsync = promisify(rimraf)
Expand Down Expand Up @@ -36,6 +37,7 @@ module.exports = async function() {
const port = parsedUrl.port || 8080
const entry = {}
const plugins = [
new LogPlugin(),
new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify(env) }
})
Expand Down Expand Up @@ -122,7 +124,28 @@ module.exports = async function() {
}
},
{
test: /\.(ttf|woff2?|eot|svg|otf)$/,
test: /\.svg$/,
use: [
{
loader: 'file-loader',
options: {
name: 'svg/[name].[ext]'
}
},
{
loader: 'svgo-loader',
options: {
plugins: [
{ removeTitle: true },
{ removeUselessDefs: false },
{ convertPathData: false }
]
}
}
]
},
{
test: /\.(ttf|woff2?|eot|otf)$/,
loader: 'file-loader',
options: {
name: 'fonts/[name].[ext]'
Expand Down
79 changes: 79 additions & 0 deletions lib/webpack/log-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const readline = require('readline')
const formatMessages = require('webpack-format-messages')
const chalk = require('chalk')
const clearScreen = require('brighten')

function getCompileTime(stats) {
if (isMultiStats(stats)) {
// Webpack multi compilations run in parallel so using the longest duration.
// https://webpack.github.io/docs/configuration.html#multiple-configurations
return stats.stats.reduce(
(time, stats) => Math.max(time, getCompileTime(stats)),
0
)
}
return stats.endTime - stats.startTime
}

function isMultiStats(stats) {
return stats.stats
}

function displayTook(stats) {
const time = getCompileTime(stats)

if (time > 1000) {
return time / 1000 + 's'
}

return time + 'ms'
}

module.exports = class {
apply(compiler) {
clearScreen()

let first = true

compiler.plugin('invalid', () => {
clearScreen()
console.log(chalk.yellow('Compiling...'))
})

compiler.plugin('done', stats => {
if (first) {
// Leave the first line showing the webserver address
readline.cursorTo(process.stdout, 0, 1)
readline.clearScreenDown(process.stdout)
} else {
// Whipe the screen including contents
readline.cursorTo(process.stdout, 0, 0)
readline.clearScreenDown(process.stdout)
}

const messages = formatMessages(stats)

if (messages.errors.length === 0 && messages.warnings.length === 0) {
// Don't clear `Starting server on HOST`
console.log(
`${chalk.green('Compiled successfully!')} ${chalk.dim(
`in ${displayTook(stats)}`
)}`
)
}

if (messages.errors.length > 0) {
console.log(chalk.inverse('Failed to compile.'))
messages.errors.forEach(e => console.log(e))
return
}

if (messages.warnings.length > 0) {
console.log('Compiled with warnings.')
messages.warnings.forEach(w => console.log(w))
}

first = false
})
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"sass-loader": "^6.0.6",
"style-loader": "^0.18.2",
"styled-jsx": "^1.0.10",
"svgo-loader": "^1.2.1",
"webpack": "^3.5.5",
"webpack-dev-server": "^2.6.0",
"webpack-format-messages": "^1.0.0",
Expand Down

0 comments on commit 1ac3a30

Please sign in to comment.