1+ import _ from 'lodash'
2+ import fs from 'fs'
3+ import path from 'path'
14import webpack from 'webpack'
2- import StaticSiteGeneratorPlugin from 'static-site-generator-webpack-plugin'
3- import ExtractTextPlugin from 'extract-text-webpack-plugin'
45import 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'
88import { StatsWriterPlugin } from 'webpack-stats-plugin'
99
10+ import webpackModifyValidate from './webpack-modify-validate'
11+
1012const debug = require ( `debug` ) ( `gatsby:webpack-config` )
1113const WebpackMD5Hash = require ( `webpack-md5-hash` )
1214const OfflinePlugin = require ( `offline-plugin` )
@@ -15,17 +17,6 @@ const { pagesDB, siteDB } = require(`../utils/globals`)
1517const { layoutComponentChunkName } = require ( `./js-chunk-names` )
1618const 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}
0 commit comments