Skip to content

Commit

Permalink
Moved utils
Browse files Browse the repository at this point in the history
Fixed bug with bundle analyzer
  • Loading branch information
Marvin Heilemann committed Sep 26, 2019
1 parent a3408b3 commit 2bc4dd5
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 69 deletions.
10 changes: 7 additions & 3 deletions gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const pkg = require('./package.json')
const { isProd, isDev, consoleImage } = require('./gatsby/utils')
const {
isProd,
isDev,
consoleImage,
getPkgVersion,
} = require('./scripts/utils')
const dayjs = require('dayjs')
const UTC = require('dayjs/plugin/utc')
const LocalizedFormat = require('dayjs/plugin/localizedFormat')
Expand Down Expand Up @@ -59,7 +63,7 @@ function printCorporateMessage() {
border-bottom-right-radius: 3px;
`

console.log(`%cMarvin/Design%cv${pkg.version}`, styleName, styleVersion)
console.log(`%cMarvin/Design%cv${getPkgVersion()}`, styleName, styleVersion)
console.log(
`%c
Welcome fellow %cdeveloper%c! 🎉
Expand Down
22 changes: 8 additions & 14 deletions gatsby-config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path')
const { yellow } = require('kleur')
const { activeEnv, isDev } = require('./gatsby/utils')
const { activeEnv, isDev, getPkgVersion } = require('./scripts/utils')
const metadata = require('./metadata')
const pkg = require('./package.json')
const { parsed } = require('dotenv').config()

console.log(`Using environment: ${yellow(activeEnv)}\n`)
Expand Down Expand Up @@ -107,19 +107,13 @@ module.exports = {
resolve: 'gatsby-plugin-webpack-bundle-analyzer',
options: {
analyzerMode: 'static',
reportFilename: `./reports/v${pkg.version.replace(
/\.(?:.(?!\.))+$/, // remove path number including dot
'.0' // add zero path number including dot
)}/treemap.html`,
reportFilename: path.resolve(
__dirname,
`reports/v${getPkgVersion()}/treemap.html`
),
openAnalyzer: false,
logLevel: 'error',
defaultSizes: 'parsed',
production: !isDev,
// TODO: exclude react stuff
excludeAssets: /node_modules|react/,
statsOptions: {
exclude: /node_modules/,
},
production: true,
disabled: isDev,
},
},
],
Expand Down
2 changes: 1 addition & 1 deletion gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
const { bgYellow } = require('kleur')
const { slugify } = require('./gatsby/utils')
const { slugify } = require('./scripts/utils')

exports.onCreateNode = async ({ node, getNode, actions }) => {
const { createNodeField } = actions
Expand Down
44 changes: 0 additions & 44 deletions gatsby/utils.js

This file was deleted.

4 changes: 2 additions & 2 deletions reports/v3.0.0/treemap.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions scripts/lighthouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const { dim, green } = require('kleur')
const taskz = require('taskz')
const chromeLauncher = require('chrome-launcher')
const lighthouse = require('lighthouse')
const { getNewVersion } = require('./utils')
const { getPkgVersion } = require('./utils')

const pkgVersion = getNewVersion()
const pkgVersion = getPkgVersion()
const BASE = path.resolve(__dirname, '..')
const REPORTS = path.resolve(BASE, 'reports')
const DEST = path.resolve(REPORTS, `v${pkgVersion}`)
Expand Down
63 changes: 61 additions & 2 deletions scripts/utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,75 @@
const pkg = require('../package.json')

/**
* Get env info.
*/
const activeEnv =
process.env.GATSBY_ACTIVE_ENV || process.env.NODE_ENV || 'development'
const isDev = activeEnv === 'development'
const isProd = activeEnv === 'production'

/**
* Slugify a string.
*/
const slugify = (text, separator) => {
text = text
.toString()
.toLowerCase()
.trim()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[^\w-]+/g, '') // Remove all non-word chars
.replace(/-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, '') // Trim - from end of text
.replace(/[·/_,:;']/g, '-') // Replace unwanted characters with -

if (separator && separator !== '-') {
text = text.replace(/-/g, separator)
}

return text
}

/**
* Print a image to the console.
*/
const consoleImage = (src, scale = 50) => {
const img = new Image()

img.onload = function() {
const width = Math.floor((this.width / 100) * scale)
const height = Math.floor((this.height / 100) * scale)
const style = `
font-size: 1px;
padding: ${height}px ${width}px;
background: url(${src}) no-repeat;
background-size: contain;
`
console.log('%c ', style)
}

img.src = src
}

/**
* Return current version, but only minor, major, patches will always be zero.
*
* @param {boolean} dots Remove dots from version
*/
function getNewVersion(dots = true) {
const getPkgVersion = (dots = true) => {
let version = pkg.version.replace(/\.(?:.(?!\.))+$/, '.0')
if (!dots) {
version = version.replace(/\./g, '')
}
return version
}

module.exports = { getNewVersion }
module.exports = {
getPkgVersion,
activeEnv,
isDev,
isProd,
slugify,
consoleImage,
}
1 change: 0 additions & 1 deletion src/html.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default function HTML(props) {
<html>
<head>
<meta charSet="utf-8" />
<meta name="generator" content="GatsbyJS" />
<meta httpEquiv="x-ua-compatible" content="ie=edge" />
<meta
name="viewport"
Expand Down

0 comments on commit 2bc4dd5

Please sign in to comment.