11import { resolve , join } from 'path'
22import { createHash } from 'crypto'
3- import { existsSync } from 'fs'
43import webpack from 'webpack'
54import glob from 'glob-promise'
65import WriteFilePlugin from 'write-file-webpack-plugin'
@@ -12,6 +11,7 @@ import DynamicEntryPlugin from './plugins/dynamic-entry-plugin'
1211import DetachPlugin from './plugins/detach-plugin'
1312import JsonPagesPlugin from './plugins/json-pages-plugin'
1413import getConfig from '../config'
14+ import findClosestPath from '../find-closest-path'
1515
1616const documentPage = join ( 'pages' , '_document.js' )
1717const defaultPages = [
@@ -49,6 +49,12 @@ export default async function createCompiler (dir, { dev = false, quiet = false
4949 }
5050
5151 const nextNodeModulesDir = join ( __dirname , '..' , '..' , '..' , 'node_modules' )
52+ const appNodeModulesDir = await findClosestPath ( dir , 'node_modules' )
53+ if ( ! appNodeModulesDir ) {
54+ console . error ( '> Cannot find a node_modules directory on the app root' )
55+ process . exit ( - 1 )
56+ }
57+
5258 const minChunks = pages . filter ( ( p ) => p !== documentPage ) . length
5359
5460 const plugins = [
@@ -99,16 +105,16 @@ export default async function createCompiler (dir, { dev = false, quiet = false
99105 )
100106 }
101107
108+ const babelrcPath = await findClosestPath ( dir , '.babelrc' )
102109 const mainBabelOptions = {
103- babelrc : true ,
110+ babelrc : Boolean ( babelrcPath ) ,
104111 cacheDirectory : true ,
105112 sourceMaps : dev ? 'both' : false ,
106113 presets : [ ]
107114 }
108115
109- const hasBabelRc = existsSync ( join ( dir , '.babelrc' ) )
110- if ( hasBabelRc ) {
111- console . log ( '> Using .babelrc defined in your app root' )
116+ if ( babelrcPath ) {
117+ console . log ( `> Using .babelrc defined in: ${ babelrcPath } ` )
112118 } else {
113119 mainBabelOptions . presets . push ( require . resolve ( './babel/preset' ) )
114120 }
@@ -187,7 +193,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false
187193 resolve : {
188194 modules : [
189195 nextNodeModulesDir ,
190- 'node_modules'
196+ appNodeModulesDir
191197 ] . concat (
192198 ( process . env . NODE_PATH || '' )
193199 . split ( process . platform === 'win32' ? ';' : ':' )
@@ -197,7 +203,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false
197203 resolveLoader : {
198204 modules : [
199205 nextNodeModulesDir ,
200- 'node_modules' ,
206+ appNodeModulesDir ,
201207 join ( __dirname , 'loaders' )
202208 ]
203209 } ,
0 commit comments