const util = require('util'); | |
const black = (s) => util.format('\x1b[30m%s\x1b[0m', s); | |
const red = (s) => util.format('\x1b[31m%s\x1b[0m', s); | |
const green = (s) => util.format('\x1b[32m%s\x1b[0m', s); | |
const yellow = (s) => util.format('\x1b[33m%s\x1b[0m', s); | |
const blue = (s) => util.format('\x1b[34m%s\x1b[0m', s); | |
const magenta = (s) => util.format('\x1b[35m%s\x1b[0m', s); | |
const cyan = (s) => util.format('\x1b[36m%s\x1b[0m', s); | |
const white = (s) => util.format('\x1b[37m%s\x1b[0m', s); |
// Builds array of everything ahead of time | |
function collectAllItems() { | |
return [calculateFirst(), calculateSecond(), ...] | |
} | |
// This loop will end as soon as `isMatch(item)` is truthy. | |
// If the very first item in the array is a match, then we | |
// wasted all this time building the array in the first place. | |
for (let item of collectAllItems()) { | |
if (isMatch(item)) { |
When Babel 6 came out, it was hard for a lot of packages to upgrade because it was essentially an entirely different category of thing than Babel 5. So what happened was that some packages upgraded, and some didn't — at least not straight away.
Some projects took the prima facie enlightened view that packages should expose untranspiled code, so that the consumers of that code could determine for themselves what needed to get transpiled based on the environments they supported.
That was a costly decision. If I was the author of an app that was using Babel 6, I couldn't import a library that was still using Babel 5 and shipping untranspiled code (because the configs were completely incompatible), and vice versa. Frankly, it was a bloody nuisance. We are bad at anticipating these sorts of issues. It will happen again at some point.
Adding a few extra bytes to pkg.main
or pkg.module
is a small price to pay for things just working. As well as avoiding the aforementioned headaches, it means that your
#!/usr/bin/env bash | |
# | |
# Author: Stefan Buck | |
# License: MIT | |
# https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447 | |
# | |
# | |
# This script accepts the following parameters: | |
# | |
# * owner |
// getComponent is a function that returns a promise for a component | |
// It will not be called until the first mount | |
function asyncComponent(getComponent) { | |
return class AsyncComponent extends React.Component { | |
static Component = null; | |
state = { Component: AsyncComponent.Component }; | |
componentWillMount() { | |
if (!this.state.Component) { | |
getComponent().then(Component => { |
// connect() is a function that injects Redux-related props into your component. | |
// You can inject data and callbacks that change that data by dispatching actions. | |
function connect(mapStateToProps, mapDispatchToProps) { | |
// It lets us inject component as the last step so people can use it as a decorator. | |
// Generally you don't need to worry about it. | |
return function (WrappedComponent) { | |
// It returns a component | |
return class extends React.Component { | |
render() { | |
return ( |
const webpack = require('webpack') | |
const Configs = require('react-project/webpack') | |
Configs.ServerConfig.module.loaders.unshift({ | |
test: /modules\/client-only\//, | |
loader: 'null-loader' | |
}) | |
Configs.ClientConfig.plugins.push( | |
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en/) |
# Version key/value should be on his own line | |
PACKAGE_VERSION=$(cat package.json \ | |
| grep version \ | |
| head -1 \ | |
| awk -F: '{ print $2 }' \ | |
| sed 's/[",]//g') | |
echo $PACKAGE_VERSION |
'use strict'; | |
var profiler = require('v8-profiler'); | |
profiler.startProfiling(); | |
// ... | |
var cpuProfile = profiler.stopProfiling(); | |
require('fs').writeFileSync(__dirname + '/foo.cpuprofile', JSON.stringify(cpuProfile)); |