Skip to content

Commit

Permalink
Use babel-plugin-lodash to modularize lodash imports without hassle
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews committed Jul 5, 2016
1 parent 406ba1a commit 364a549
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 34 deletions.
7 changes: 4 additions & 3 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
"presets": [
'react',
'es2015',
'stage-0'
'stage-0',
],
"plugins": [
'transform-flow-strip-types'
]
'transform-flow-strip-types',
'lodash',
],
}
2 changes: 1 addition & 1 deletion lib/bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const program = require('commander')
const packageJson = require('../../package.json')
const _ = require('lodash')
const fs = require('fs-extra')
const path = require('path')
import _ from 'lodash'

// Copy our load-context function to root of site in a dot file.
const gatsbyFile = `${__dirname}/../utils/load-context.js`
Expand Down
22 changes: 10 additions & 12 deletions lib/isomorphic/create-routes.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* @flow weak */
import filter from 'lodash/filter'
import sortBy from 'lodash/sortBy'
import last from 'lodash/last'
import _ from 'lodash'
import { prefixLink } from './gatsby-helpers'

module.exports = (files, pagesReq) => {
// Remove files that start with an underscore as this indicates
// the file shouldn't be turned into a page.
const pages = filter(files, (file) => file.file.name.slice(0, 1) !== '_')
const templates = filter(files, (file) =>
const pages = _.filter(files, (file) => file.file.name.slice(0, 1) !== '_')
const templates = _.filter(files, (file) =>
file.file.name === '_template'
)

Expand All @@ -34,28 +32,28 @@ module.exports = (files, pagesReq) => {
// 3. For each index file paired with a template, create a default route
// 4. Create normal routes for each remaining file under the appropriate
// template
const templatesWithoutRoot = filter(files, (file) =>
const templatesWithoutRoot = _.filter(files, (file) =>
file.file.name === '_template' && file.file.dirname !== ''
)

// Find the parent template for each template file and create a route
// with it.
templatesWithoutRoot.forEach((templateFile) => {
let parentTemplates = filter(templatesWithoutRoot, (template) =>
let parentTemplates = _.filter(templatesWithoutRoot, (template) =>
templateFile.requirePath.indexOf(template.file.dirname) === 0
)
// Sort parent templates by directory length. In cases
// where a template has multiple parents
// e.g. /_template.js/blog/_template.js/archive/_template.js
// we want to nest this template under its most immediate parent.
parentTemplates = sortBy(parentTemplates, (template) => {
parentTemplates = _.sortBy(parentTemplates, (template) => {
if (template) {
return template.file.dirname.length
} else {
return 0
}
})
const parentTemplateFile = last(parentTemplates)
const parentTemplateFile = _.last(parentTemplates)
let parentRoute
if (parentTemplateFile) {
parentRoute = templatesHash[parentTemplateFile.file.dirname]
Expand Down Expand Up @@ -122,13 +120,13 @@ module.exports = (files, pagesReq) => {
}

// Determine parent template for page.
const parentTemplates = filter(templatesWithoutRoot, (templateFile) =>
const parentTemplates = _.filter(templatesWithoutRoot, (templateFile) =>
page.requirePath.indexOf(templateFile.file.dirname) === 0
)

const sortedParentTemplates = sortBy(parentTemplates, (route) => route.file.dirname.length)
const sortedParentTemplates = _.sortBy(parentTemplates, (route) => route.file.dirname.length)

const parentTemplateFile = last(sortedParentTemplates)
const parentTemplateFile = _.last(sortedParentTemplates)
let parentRoute
if (parentTemplateFile) {
parentRoute = templatesHash[parentTemplateFile.file.dirname]
Expand Down
4 changes: 2 additions & 2 deletions lib/isomorphic/gatsby-helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow weak */
import { config } from 'config'
import invariant from 'invariant'
import isString from 'lodash/isString'
import _ from 'lodash'

// Function to add prefix to links.
const prefixLink = (_link) => {
Expand All @@ -13,7 +13,7 @@ const prefixLink = (_link) => {
You're trying to build your site with links prefixed
but you haven't set 'linkPrefix' in your config.toml.
`
invariant(isString(config.linkPrefix), invariantMessage)
invariant(_.isString(config.linkPrefix), invariantMessage)

return config.linkPrefix + _link
} else {
Expand Down
7 changes: 3 additions & 4 deletions lib/utils/babel-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import resolve from 'babel-core/lib/helpers/resolve'
import fs from 'fs'
import path from 'path'
import json5 from 'json5'
import startsWith from 'lodash/startsWith'
import isArray from 'lodash/isArray'
import _ from 'lodash'
import objectAssign from 'object-assign'
import invariant from 'invariant'

Expand Down Expand Up @@ -32,7 +31,7 @@ function resolvePlugin (pluginName, directory, type) {
resolve(pluginName, directory) ||
resolve(pluginName, gatsbyPath)

const name = startsWith(pluginName, 'babel') ? pluginName : `babel-${type}-${pluginName}`
const name = _.startsWith(pluginName, 'babel') ? pluginName : `babel-${type}-${pluginName}`
const pluginInvariantMessage = `
You are trying to use a Babel plugin which Gatsby cannot find. You
can install it using "npm install --save ${name}".
Expand Down Expand Up @@ -69,7 +68,7 @@ function normalizeConfig (config, directory) {
plugins.forEach(plugin => {
let normalizedPlugin

if (isArray(plugin)) {
if (_.isArray(plugin)) {
normalizedPlugin = [resolvePlugin(plugin[0], directory, 'plugin'), plugin[1]]
} else {
normalizedPlugin = resolvePlugin(plugin, directory, 'plugin')
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/build-page/url-resolver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow weak */
import { posix as path } from 'path'
import _, { startsWith } from 'lodash'
import _ from 'lodash'
import invariant from 'invariant'

let rewritePath
Expand Down Expand Up @@ -61,7 +61,7 @@ export default function pathResolver (pageData, parsedPath) {
* Returns a path for a page. If the page name starts with an underscore,
* undefined is returned as it does not become a page.
*/
if (!startsWith(parsedPath.name, '_')) {
if (!_.startsWith(parsedPath.name, '_')) {
return hardcodedPath() || rewrittenPath() || defaultPath()
} else {
return undefined
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ReactDOMServer from 'react-dom/server'
import webpack from 'webpack'
import Negotiator from 'negotiator'
import parsePath from 'parse-filepath'
import find from 'lodash/find'
import _ from 'lodash'
import webpackRequire from 'webpack-require'
import WebpackPlugin from 'hapi-webpack-plugin'
import opn from 'opn'
Expand Down Expand Up @@ -94,7 +94,7 @@ module.exports = (program) => {

// Try to map the url path to match an actual path of a file on disk.
const parsed = parsePath(request.path)
const page = find(pages, (p) => p.path === (`${parsed.dirname}/`))
const page = _.find(pages, (p) => p.path === (`${parsed.dirname}/`))

let absolutePath = `${directory}/pages`
let path
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/post-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import glob from 'glob'
import fs from 'fs-extra'
import async from 'async'
import parsePath from 'parse-filepath'
import find from 'lodash/find'
import _ from 'lodash'
const debug = require('debug')('gatsby:post-build')

import globPages from './glob-pages'
Expand Down Expand Up @@ -32,7 +32,7 @@ module.exports = (program, cb) => {
}

if (!(oldDirectory === '/')) {
const page = find(pages, (p) => {
const page = _.find(pages, (p) => {
// Ignore files that start with underscore (they're not pages).
if (p.file.name.slice(0, 1) !== '_') {
return parsePath(p.requirePath).dirname === oldDirectory
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"ava-http": "^0.2.1",
"babel-cli": "^6.9.0",
"babel-eslint": "^6.1.0",
"babel-plugin-lodash": "^3.2.0",
"babel-plugin-transform-flow-strip-types": "^6.8.0",
"babel-register": "^6.9.0",
"bluebird": "^3.4.1",
Expand Down Expand Up @@ -125,6 +126,7 @@
},
"scripts": {
"build": "babel lib --out-dir dist/",
"clean-test-bundles": "find test/ -type f -name bundle.js* -exec rm -rf {} +",
"lint": "eslint --ext .js,.jsx --ignore-path .gitignore .",
"lint:flow": "babel-node scripts/flow-check.js",
"test": "npm run lint && npm run test-node && npm run test-integration",
Expand Down
4 changes: 2 additions & 2 deletions test/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs-extra'
import path from 'path'
import cheerio from 'cheerio'
import Promise from 'bluebird'
import { concat } from 'lodash'
import _ from 'lodash'
import { spawn as spawnNative } from 'child_process'
const remove = Promise.promisify(fs.remove)
const gatsbyCli = path.resolve('..', '..', 'lib', 'bin', 'cli.js')
Expand All @@ -27,7 +27,7 @@ export function spawn (command, args = [], options = {}) {
}

export function gatsby (args = [], options = {}) {
const spawnArguments = concat(['--', gatsbyCli], args)
const spawnArguments = _.concat(['--', gatsbyCli], args)
return spawn(babel, spawnArguments, options)
}

Expand Down
4 changes: 2 additions & 2 deletions test/utils/babel-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava'
import path from 'path'
import includes from 'lodash/includes'
import _ from 'lodash'
import babelConfig from '../../lib/utils/babel-config'

function programStub (fixture) {
Expand Down Expand Up @@ -51,7 +51,7 @@ test('when in development has hmre', t => {

// regex matches: babel followed by any amount of hyphen or word characters
const presetNames = config.presets.map(p => p.match(/babel[-|\w]+/)[0])
t.true(includes(presetNames, 'babel-preset-react-hmre'))
t.true(_.includes(presetNames, 'babel-preset-react-hmre'))
})

test('throws when a plugin is not available', t => {
Expand Down
4 changes: 2 additions & 2 deletions test/utils/build-page/path-resolver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava'
import path from 'path'
import { startsWith } from 'lodash'
import _ from 'lodash'
import pathResolver from '../../../lib/utils/build-page/path-resolver'

test('it returns an object', t => {
Expand Down Expand Up @@ -52,5 +52,5 @@ test('the directory name has / slashes', t => {

test('the ext doesnt have a leading .', t => {
const result = pathResolver('/index.md')
t.false(startsWith(result.file.ext, '.'))
t.false(_.startsWith(result.file.ext, '.'))
})

0 comments on commit 364a549

Please sign in to comment.