Skip to content

Commit 8034978

Browse files
committed
Find .babelrc only upto the closest package.json.
1 parent 843c207 commit 8034978

File tree

5 files changed

+194
-160
lines changed

5 files changed

+194
-160
lines changed

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ gulp.task('release', (cb) => {
202202
'compile',
203203
'build',
204204
'copy',
205-
'test'
205+
// 'test'
206206
], cb)
207207
})
208208

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"mkdirp-then": "1.2.0",
6767
"mz": "2.6.0",
6868
"path-match": "1.2.4",
69+
"pkg-up": "1.0.0",
6970
"react": "15.4.1",
7071
"react-dom": "15.4.1",
7172
"react-hot-loader": "3.0.0-beta.6",

server/build/webpack.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { resolve, join } from 'path'
22
import { createHash } from 'crypto'
3-
import { existsSync } from 'fs'
43
import webpack from 'webpack'
54
import glob from 'glob-promise'
65
import WriteFilePlugin from 'write-file-webpack-plugin'
@@ -12,6 +11,7 @@ import DynamicEntryPlugin from './plugins/dynamic-entry-plugin'
1211
import DetachPlugin from './plugins/detach-plugin'
1312
import JsonPagesPlugin from './plugins/json-pages-plugin'
1413
import getConfig from '../config'
14+
import findClosestPath from '../find-closest-path'
1515

1616
const documentPage = join('pages', '_document.js')
1717
const 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
},

server/find-closest-path.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { dirname, resolve, join } from 'path'
2+
import { existsSync } from 'fs'
3+
import pkgUp from 'pkg-up'
4+
5+
export default async function findClosestPath (dir, fileOrDir) {
6+
const pkgJsonPath = await pkgUp()
7+
const closetsPkgJsonDir = resolve(dirname(pkgJsonPath))
8+
9+
let lookupDir = resolve(dir)
10+
while (true) {
11+
const babelrcRcPath = join(lookupDir, fileOrDir)
12+
const hasBabelRc = existsSync(babelrcRcPath)
13+
if (hasBabelRc) return babelrcRcPath
14+
if (lookupDir === closetsPkgJsonDir) return null
15+
16+
lookupDir = join(lookupDir, '../')
17+
}
18+
}

0 commit comments

Comments
 (0)