forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We don't use a lot of the features of `glob`, so let's remove it in favor of a leaner approach using regex. It's failing on windows and I have no idea why and don't own a windows machine 🤦🏼♂️ (Ignore some of the commits in here, I forgot to create the new branch before I started working)
- Loading branch information
1 parent
993cab8
commit 5514949
Showing
21 changed files
with
184 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fixtures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Uses https://github.com/divmain/fuzzponent | ||
mkdir fixtures | ||
cd fixtures | ||
fuzzponent -d 2 -s 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const { join } = require('path') | ||
const { promisify } = require('util') | ||
const globMod = require('glob') | ||
const glob = promisify(globMod) | ||
const resolveDataDir = join(__dirname, 'fixtures', '**/*') | ||
|
||
async function test () { | ||
const time = process.hrtime() | ||
await glob(resolveDataDir) | ||
|
||
const hrtime = process.hrtime(time) | ||
const nanoseconds = (hrtime[0] * 1e9) + hrtime[1] | ||
const milliseconds = nanoseconds / 1e6 | ||
console.log(milliseconds) | ||
} | ||
|
||
async function run () { | ||
for (let i = 0; i < 50; i++) { | ||
await test() | ||
} | ||
} | ||
|
||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const { join } = require('path') | ||
const { recursiveReadDir } = require('next/dist/lib/recursive-readdir') | ||
const resolveDataDir = join(__dirname, 'fixtures') | ||
|
||
async function test () { | ||
const time = process.hrtime() | ||
await recursiveReadDir(resolveDataDir, /\.js$/) | ||
|
||
const hrtime = process.hrtime(time) | ||
const nanoseconds = (hrtime[0] * 1e9) + hrtime[1] | ||
const milliseconds = nanoseconds / 1e6 | ||
console.log(milliseconds) | ||
} | ||
|
||
async function run () { | ||
for (let i = 0; i < 50; i++) { | ||
await test() | ||
} | ||
} | ||
|
||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import fs from 'fs' | ||
import { join } from 'path' | ||
import { promisify } from 'util' | ||
|
||
const readdir = promisify(fs.readdir) | ||
const stat = promisify(fs.stat) | ||
|
||
/** | ||
* Recursively read directory | ||
* @param {string} dir Directory to read | ||
* @param {RegExp} filter Filter for the file name, only the name part is considered, not the full path | ||
* @param {string[]=[]} arr This doesn't have to be provided, it's used for the recursion | ||
* @param {string=dir`} rootDir Used to replace the initial path, only the relative path is left, it's faster than path.relative. | ||
* @returns Promise array holding all relative paths | ||
*/ | ||
export async function recursiveReadDir(dir: string, filter: RegExp, arr: string[] = [], rootDir: string = dir): Promise<string[]> { | ||
const result = await readdir(dir) | ||
|
||
await Promise.all(result.map(async (part: string) => { | ||
const absolutePath = join(dir, part) | ||
const pathStat = await stat(absolutePath) | ||
|
||
if (pathStat.isDirectory()) { | ||
await recursiveReadDir(absolutePath, filter, arr, rootDir) | ||
return | ||
} | ||
|
||
if (!filter.test(part)) { | ||
return | ||
} | ||
|
||
arr.push(absolutePath.replace(rootDir, '')) | ||
})) | ||
|
||
return arr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { join } from 'path' | ||
import {isWriteable} from '../../build/is-writeable' | ||
|
||
export async function findPageFile(rootDir: string, normalizedPagePath: string, pageExtensions: string[]): Promise<string|null> { | ||
for (const extension of pageExtensions) { | ||
const relativePagePath = `${normalizedPagePath}.${extension}` | ||
const pagePath = join(rootDir, relativePagePath) | ||
|
||
if (await isWriteable(pagePath)) { | ||
return relativePagePath | ||
} | ||
|
||
const relativePagePathWithIndex = join(normalizedPagePath, `index.${extension}`) | ||
const pagePathWithIndex = join(rootDir, relativePagePathWithIndex) | ||
if (await isWriteable(pagePathWithIndex)) { | ||
return relativePagePathWithIndex | ||
} | ||
} | ||
|
||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* eslint-env jest */ | ||
import { findPageFile } from 'next/dist/server/lib/find-page-file' | ||
import { normalizePagePath } from 'next-server/dist/server/normalize-page-path' | ||
|
||
import { join } from 'path' | ||
|
||
const resolveDataDir = join(__dirname, '..', 'isolated', '_resolvedata') | ||
const dirWithPages = join(resolveDataDir, 'readdir', 'pages') | ||
|
||
describe('findPageFile', () => { | ||
it('should work', async () => { | ||
const pagePath = normalizePagePath('/nav/about') | ||
const result = await findPageFile(dirWithPages, pagePath, ['jsx', 'js']) | ||
expect(result).toMatch(/^[\\/]nav[\\/]about\.js/) | ||
}) | ||
|
||
it('should work with nested index.js', async () => { | ||
const pagePath = normalizePagePath('/nested') | ||
const result = await findPageFile(dirWithPages, pagePath, ['jsx', 'js']) | ||
expect(result).toMatch(/^[\\/]nested[\\/]index\.js/) | ||
}) | ||
|
||
it('should prefer prefered.js before prefered/index.js', async () => { | ||
const pagePath = normalizePagePath('/prefered') | ||
const result = await findPageFile(dirWithPages, pagePath, ['jsx', 'js']) | ||
expect(result).toMatch(/^[\\/]prefered\.js/) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* eslint-env jest */ | ||
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir' | ||
import { join } from 'path' | ||
|
||
const resolveDataDir = join(__dirname, '..', 'isolated', '_resolvedata') | ||
const dirWithPages = join(resolveDataDir, 'readdir', 'pages') | ||
|
||
describe('recursiveReadDir', () => { | ||
it('should work', async () => { | ||
const result = await recursiveReadDir(dirWithPages, /\.js/) | ||
const pages = [/^[\\/]index\.js/, /^[\\/]prefered\.js/, /^[\\/]nav[\\/]about\.js/, /^[\\/]nav[\\/]index\.js/, /^[\\/]nested[\\/]index\.js/, /^[\\/]prefered[\\/]index\.js/, /^[\\/]nav[\\/]products[\\/]product\.js/] | ||
expect(result.filter((item) => { | ||
for (const page of pages) { | ||
if (page.test(item)) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
}).length).toBe(0) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1550,7 +1550,7 @@ | |
resolved "https://registry.yarnpkg.com/@types/fresh/-/fresh-0.5.0.tgz#4d09231027d69c4369cfb01a9af5ef083d0d285f" | ||
integrity sha512-eGPzuyc6wZM3sSHJdF7NM2jW6B/xsB014Rqg/iDa6xY02mlfy1w/TE2sYhR8vbHxkzJOXiGo6NuIk3xk35vsgQ== | ||
|
||
"@types/glob@*", "@types/[email protected]": | ||
"@types/glob@*": | ||
version "7.1.1" | ||
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.1.tgz#aa59a1c6e3fbc421e07ccd31a944c30eba521575" | ||
integrity sha512-1Bh06cbWJUHMC97acuD6UMG29nMt0Aqz1vF3guLfG+kHHJhy3AyohZFFxYk2f7Q1SQIrNwvncxAE0N/9s70F2w== | ||
|
@@ -1599,9 +1599,9 @@ | |
"@types/node" "*" | ||
|
||
"@types/node@*": | ||
version "11.9.4" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.9.4.tgz#ceb0048a546db453f6248f2d1d95e937a6f00a14" | ||
integrity sha512-Zl8dGvAcEmadgs1tmSPcvwzO1YRsz38bVJQvH1RvRqSR9/5n61Q1ktcDL0ht3FXWR+ZpVmXVwN1LuH4Ax23NsA== | ||
version "11.9.5" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.9.5.tgz#011eece9d3f839a806b63973e228f85967b79ed3" | ||
integrity sha512-vVjM0SVzgaOUpflq4GYBvCpozes8OgIIS5gVXVka+OfK3hvnkC1i93U8WiY2OtNE4XUWyyy/86Kf6e0IHTQw1Q== | ||
|
||
"@types/prop-types@*": | ||
version "15.5.9" | ||
|
@@ -1981,9 +1981,9 @@ ajv@^5.1.0: | |
json-schema-traverse "^0.3.0" | ||
|
||
ajv@^6.0.1, ajv@^6.1.0, ajv@^6.5.0, ajv@^6.5.5: | ||
version "6.9.1" | ||
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.9.1.tgz#a4d3683d74abc5670e75f0b16520f70a20ea8dc1" | ||
integrity sha512-XDN92U311aINL77ieWHmqCcNlwjoP5cHXDxIxbf2MaPYuCXOHS7gHH8jktxeK5omgd52XbSTX6a4Piwd1pQmzA== | ||
version "6.9.2" | ||
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.9.2.tgz#4927adb83e7f48e5a32b45729744c71ec39c9c7b" | ||
integrity sha512-4UFy0/LgDo7Oa/+wOAlj44tp9K78u38E5/359eSrqEp1Z5PdVfimCcs7SluXMP755RUQu6d2b4AvF0R1C9RZjg== | ||
dependencies: | ||
fast-deep-equal "^2.0.1" | ||
fast-json-stable-stringify "^2.0.0" | ||
|
@@ -5679,18 +5679,6 @@ glob-to-regexp@^0.3.0: | |
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.3.0.tgz#8c5a1494d2066c570cc3bfe4496175acc4d502ab" | ||
integrity sha1-jFoUlNIGbFcMw7/kSWF1rMTVAqs= | ||
|
||
[email protected]: | ||
version "7.1.2" | ||
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" | ||
integrity sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ== | ||
dependencies: | ||
fs.realpath "^1.0.0" | ||
inflight "^1.0.4" | ||
inherits "2" | ||
minimatch "^3.0.4" | ||
once "^1.3.0" | ||
path-is-absolute "^1.0.0" | ||
|
||
glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@~7.1.1: | ||
version "7.1.3" | ||
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.3.tgz#3960832d3f1574108342dafd3a67b332c0969df1" | ||
|