Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close #367 Into 1.0 #464

Merged
merged 1 commit into from
Sep 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
language: cpp
sudo: false
env:
- export NODE_VERSION="0.12"
- export NODE_VERSION="4"
- export NODE_VERSION="6"
os:
Expand Down
9 changes: 3 additions & 6 deletions bin/gatsby.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ global.appStartTime = Date.now()
var sysPath = require('path')
var fs = require('fs')
var version = process.version
var versionDigits = version.split('.')
.map(function (d) { return d.match(/\d+/)[0] })
.slice(0, 2).join('.')
var verDigit = Number(versionDigits)
var verDigit = Number(version.match(/\d+/)[0])

if (verDigit < 0.12) {
if (verDigit < 4) {
console.error(
'Error: Gatsby 0.9+ requires node.js v0.12 or higher (you have ' + version + ') ' +
'Error: Gatsby 1.0+ requires node.js v4 or higher (you have ' + version + ') ' +
'Upgrade node to the latest stable release.'
)
process.exit()
Expand Down
47 changes: 47 additions & 0 deletions lib/utils/webpack-modify-validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import _ from 'lodash'
import invariant from 'invariant'
import path from 'path'
import validate from 'webpack-validator'

let modifyWebpackConfig
try {
const gatsbyNodeConfig = path.resolve(process.cwd(), `./gatsby-node`)
const nodeConfig = require(gatsbyNodeConfig)
modifyWebpackConfig = nodeConfig.modifyWebpackConfig
} catch (e) {
if (e.code !== `MODULE_NOT_FOUND` && !_.includes(e.Error, `gatsby-node`)) {
console.log(e)
}
}

export default function ValidateWebpackConfig (module, config, stage) {
let userWebpackConfig = module(config)
if (modifyWebpackConfig) {
userWebpackConfig = modifyWebpackConfig(userWebpackConfig, stage)

invariant(_.isObject(userWebpackConfig) && _.isFunction(userWebpackConfig.resolve),
`
You must return an webpack-configurator instance when modifying the Webpack config.
Returned: ${userWebpackConfig}
stage: ${stage}
`)
}

const validationState = validate(userWebpackConfig.resolve(), {
returnValidation: true,
})

if (!validationState.error) {
return userWebpackConfig
}

console.log(`There were errors with your webpack config:`)
validationState.error.details.forEach((err, index) => {
console.log(`[${index + 1}]`)
console.log(err.path)
console.log(err.type, `,`, err.message)
console.log(`\n`)
})

return process.exit(1)
}
77 changes: 36 additions & 41 deletions lib/utils/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import _ from 'lodash'
import fs from 'fs'
import path from 'path'
import webpack from 'webpack'
import StaticSiteGeneratorPlugin from 'static-site-generator-webpack-plugin'
import ExtractTextPlugin from 'extract-text-webpack-plugin'
import Config from 'webpack-configurator'
import path from 'path'
import _ from 'lodash'
import invariant from 'invariant'
import ExtractTextPlugin from 'extract-text-webpack-plugin'
import StaticSiteGeneratorPlugin from 'static-site-generator-webpack-plugin'
import { StatsWriterPlugin } from 'webpack-stats-plugin'

import webpackModifyValidate from './webpack-modify-validate'

const debug = require(`debug`)(`gatsby:webpack-config`)
const WebpackMD5Hash = require(`webpack-md5-hash`)
const OfflinePlugin = require(`offline-plugin`)
Expand All @@ -15,17 +17,6 @@ const { pagesDB, siteDB } = require(`../utils/globals`)
const { layoutComponentChunkName } = require(`./js-chunk-names`)
const babelConfig = require(`./babel-config`)

let modifyWebpackConfig
try {
const gatsbyNodeConfig = path.resolve(process.cwd(), `./gatsby-node`)
const nodeConfig = require(gatsbyNodeConfig)
modifyWebpackConfig = nodeConfig.modifyWebpackConfig
} catch (e) {
if (e.code !== `MODULE_NOT_FOUND` && !_.includes(e.Error, `gatsby-node`)) {
console.log(e)
}
}

// Five stages or modes:
// 1) develop: for `gatsby develop` command, hot reload and CSS injection into page
// 2) develop-html: same as develop without react-hmre in the babel config for html renderer
Expand Down Expand Up @@ -379,6 +370,33 @@ module.exports = (program, directory, suppliedStage, webpackPort = 1500, pages =
}
}

function resolveLoader () {
const root = [
path.resolve(__dirname, '..', 'loaders'),
path.resolve(directory, `node_modules`),
path.resolve(directory, `node_modules/gatsby/node_modules`),
]

const userLoaderDirectoryPath = path.resolve(directory, 'loaders')

try {
if (fs.statSync(userLoaderDirectoryPath).isDirectory()) {
root.push(userLoaderDirectoryPath)
}
} catch (e) {
if (e && e.code !== 'ENOENT') {
console.log(e)
}
}

return {
root,
modulesDirectories: [
'node_modules',
],
}
}

const config = new Config()

config.merge({
Expand All @@ -391,33 +409,10 @@ module.exports = (program, directory, suppliedStage, webpackPort = 1500, pages =
profile: stage === `production`,
devtool: devtool(),
output: output(),
resolveLoader: {
// Hierarchy of directories for Webpack to look for loaders.
// First is the /loaders/ directory in the site.
// Then in the special directory of loaders Gatsby ships with.
// Then the site's node_modules directory
// and last the Gatsby node_modules directory.
root: [
path.resolve(directory, `loaders`),
path.resolve(__dirname, `..`, `loaders`),
path.resolve(directory, `node_modules`),
path.resolve(directory, `node_modules/gatsby/node_modules`),
],
},
resolveLoader: resolveLoader(),
plugins: plugins(),
resolve: resolve(),
})

if (modifyWebpackConfig) {
const modifiedWebpackConfig = modifyWebpackConfig(module(config), stage)
invariant(_.isObject(modifiedWebpackConfig),
`
You must return an object when modifying the Webpack config.
Returned: ${modifiedWebpackConfig}
stage: ${stage}
`)
return modifiedWebpackConfig
} else {
return module(config)
}
return webpackModifyValidate(module, config, stage)
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
"nyc": "^7.0.0"
},
"engines": {
"node": ">0.12.0"
"node": ">4.0.0"
},
"homepage": "https://github.com/gatsbyjs/gatsby#readme",
"keywords": [
Expand Down