Skip to content

Commit 3007f58

Browse files
LukeSheardKyleAMathews
authored andcommitted
Fix linting errors and transfer from original PR (#464)
1 parent da7b090 commit 3007f58

File tree

5 files changed

+87
-49
lines changed

5 files changed

+87
-49
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
language: cpp
33
sudo: false
44
env:
5-
- export NODE_VERSION="0.12"
65
- export NODE_VERSION="4"
76
- export NODE_VERSION="6"
87
os:

bin/gatsby.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@ global.appStartTime = Date.now()
99
var sysPath = require('path')
1010
var fs = require('fs')
1111
var version = process.version
12-
var versionDigits = version.split('.')
13-
.map(function (d) { return d.match(/\d+/)[0] })
14-
.slice(0, 2).join('.')
15-
var verDigit = Number(versionDigits)
12+
var verDigit = Number(version.match(/\d+/)[0])
1613

17-
if (verDigit < 0.12) {
14+
if (verDigit < 4) {
1815
console.error(
19-
'Error: Gatsby 0.9+ requires node.js v0.12 or higher (you have ' + version + ') ' +
16+
'Error: Gatsby 1.0+ requires node.js v4 or higher (you have ' + version + ') ' +
2017
'Upgrade node to the latest stable release.'
2118
)
2219
process.exit()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import _ from 'lodash'
2+
import invariant from 'invariant'
3+
import path from 'path'
4+
import validate from 'webpack-validator'
5+
6+
let modifyWebpackConfig
7+
try {
8+
const gatsbyNodeConfig = path.resolve(process.cwd(), `./gatsby-node`)
9+
const nodeConfig = require(gatsbyNodeConfig)
10+
modifyWebpackConfig = nodeConfig.modifyWebpackConfig
11+
} catch (e) {
12+
if (e.code !== `MODULE_NOT_FOUND` && !_.includes(e.Error, `gatsby-node`)) {
13+
console.log(e)
14+
}
15+
}
16+
17+
export default function ValidateWebpackConfig (module, config, stage) {
18+
let userWebpackConfig = module(config)
19+
if (modifyWebpackConfig) {
20+
userWebpackConfig = modifyWebpackConfig(userWebpackConfig, stage)
21+
22+
invariant(_.isObject(userWebpackConfig) && _.isFunction(userWebpackConfig.resolve),
23+
`
24+
You must return an webpack-configurator instance when modifying the Webpack config.
25+
Returned: ${userWebpackConfig}
26+
stage: ${stage}
27+
`)
28+
}
29+
30+
const validationState = validate(userWebpackConfig.resolve(), {
31+
returnValidation: true,
32+
})
33+
34+
if (!validationState.error) {
35+
return userWebpackConfig
36+
}
37+
38+
console.log(`There were errors with your webpack config:`)
39+
validationState.error.details.forEach((err, index) => {
40+
console.log(`[${index + 1}]`)
41+
console.log(err.path)
42+
console.log(err.type, `,`, err.message)
43+
console.log(`\n`)
44+
})
45+
46+
return process.exit(1)
47+
}

lib/utils/webpack.config.js

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import _ from 'lodash'
2+
import fs from 'fs'
3+
import path from 'path'
14
import webpack from 'webpack'
2-
import StaticSiteGeneratorPlugin from 'static-site-generator-webpack-plugin'
3-
import ExtractTextPlugin from 'extract-text-webpack-plugin'
45
import Config from 'webpack-configurator'
5-
import path from 'path'
6-
import _ from 'lodash'
7-
import invariant from 'invariant'
6+
import ExtractTextPlugin from 'extract-text-webpack-plugin'
7+
import StaticSiteGeneratorPlugin from 'static-site-generator-webpack-plugin'
88
import { StatsWriterPlugin } from 'webpack-stats-plugin'
99

10+
import webpackModifyValidate from './webpack-modify-validate'
11+
1012
const debug = require(`debug`)(`gatsby:webpack-config`)
1113
const WebpackMD5Hash = require(`webpack-md5-hash`)
1214
const OfflinePlugin = require(`offline-plugin`)
@@ -15,17 +17,6 @@ const { pagesDB, siteDB } = require(`../utils/globals`)
1517
const { layoutComponentChunkName } = require(`./js-chunk-names`)
1618
const babelConfig = require(`./babel-config`)
1719

18-
let modifyWebpackConfig
19-
try {
20-
const gatsbyNodeConfig = path.resolve(process.cwd(), `./gatsby-node`)
21-
const nodeConfig = require(gatsbyNodeConfig)
22-
modifyWebpackConfig = nodeConfig.modifyWebpackConfig
23-
} catch (e) {
24-
if (e.code !== `MODULE_NOT_FOUND` && !_.includes(e.Error, `gatsby-node`)) {
25-
console.log(e)
26-
}
27-
}
28-
2920
// Five stages or modes:
3021
// 1) develop: for `gatsby develop` command, hot reload and CSS injection into page
3122
// 2) develop-html: same as develop without react-hmre in the babel config for html renderer
@@ -379,6 +370,33 @@ module.exports = (program, directory, suppliedStage, webpackPort = 1500, pages =
379370
}
380371
}
381372

373+
function resolveLoader () {
374+
const root = [
375+
path.resolve(__dirname, '..', 'loaders'),
376+
path.resolve(directory, `node_modules`),
377+
path.resolve(directory, `node_modules/gatsby/node_modules`),
378+
]
379+
380+
const userLoaderDirectoryPath = path.resolve(directory, 'loaders')
381+
382+
try {
383+
if (fs.statSync(userLoaderDirectoryPath).isDirectory()) {
384+
root.push(userLoaderDirectoryPath)
385+
}
386+
} catch (e) {
387+
if (e && e.code !== 'ENOENT') {
388+
console.log(e)
389+
}
390+
}
391+
392+
return {
393+
root,
394+
modulesDirectories: [
395+
'node_modules',
396+
],
397+
}
398+
}
399+
382400
const config = new Config()
383401

384402
config.merge({
@@ -391,33 +409,10 @@ module.exports = (program, directory, suppliedStage, webpackPort = 1500, pages =
391409
profile: stage === `production`,
392410
devtool: devtool(),
393411
output: output(),
394-
resolveLoader: {
395-
// Hierarchy of directories for Webpack to look for loaders.
396-
// First is the /loaders/ directory in the site.
397-
// Then in the special directory of loaders Gatsby ships with.
398-
// Then the site's node_modules directory
399-
// and last the Gatsby node_modules directory.
400-
root: [
401-
path.resolve(directory, `loaders`),
402-
path.resolve(__dirname, `..`, `loaders`),
403-
path.resolve(directory, `node_modules`),
404-
path.resolve(directory, `node_modules/gatsby/node_modules`),
405-
],
406-
},
412+
resolveLoader: resolveLoader(),
407413
plugins: plugins(),
408414
resolve: resolve(),
409415
})
410416

411-
if (modifyWebpackConfig) {
412-
const modifiedWebpackConfig = modifyWebpackConfig(module(config), stage)
413-
invariant(_.isObject(modifiedWebpackConfig),
414-
`
415-
You must return an object when modifying the Webpack config.
416-
Returned: ${modifiedWebpackConfig}
417-
stage: ${stage}
418-
`)
419-
return modifiedWebpackConfig
420-
} else {
421-
return module(config)
422-
}
417+
return webpackModifyValidate(module, config, stage)
423418
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
"nyc": "^7.0.0"
126126
},
127127
"engines": {
128-
"node": ">0.12.0"
128+
"node": ">4.0.0"
129129
},
130130
"homepage": "https://github.com/gatsbyjs/gatsby#readme",
131131
"keywords": [

0 commit comments

Comments
 (0)