1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Remove mz-fs and glob-promise (#4026)

* Remove mz and add promisify util

* Remove glob-promise and promisify glob

* Use access instead of exists

* Use promisify for access
This commit is contained in:
Samatar 2018-03-30 15:59:42 +01:00 committed by Arunoda Susiripala
parent b6b88e5f81
commit 56998a6193
7 changed files with 51 additions and 10 deletions

View file

@ -77,7 +77,6 @@
"fresh": "0.5.2",
"friendly-errors-webpack-plugin": "1.6.1",
"glob": "7.1.2",
"glob-promise": "3.3.0",
"hoist-non-react-statics": "2.3.1",
"htmlescape": "1.1.1",
"http-errors": "1.6.2",
@ -85,7 +84,6 @@
"loader-utils": "1.1.0",
"minimist": "1.2.0",
"mkdirp-then": "1.2.0",
"mz": "2.7.0",
"path-to-regexp": "2.1.0",
"prop-types": "15.6.0",
"prop-types-exact": "1.1.1",

View file

@ -1,17 +1,21 @@
import { join } from 'path'
import fs from 'mz/fs'
import promisify from '../lib/promisify'
import fs from 'fs'
import uuid from 'uuid'
import webpack from 'webpack'
import getConfig from '../config'
import {PHASE_PRODUCTION_BUILD} from '../../lib/constants'
import getBaseWebpackConfig from './webpack'
const access = promisify(fs.access)
const writeFile = promisify(fs.writeFile)
export default async function build (dir, conf = null) {
const config = getConfig(PHASE_PRODUCTION_BUILD, dir, conf)
const buildId = uuid.v4()
try {
await fs.access(dir, fs.constants.W_OK)
await access(dir, (fs.constants || fs).W_OK)
} catch (err) {
console.error(`> Failed, build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable`)
throw err
@ -54,5 +58,5 @@ function runCompiler (compiler) {
async function writeBuildId (dir, buildId, config) {
const buildIdPath = join(dir, config.distDir, 'BUILD_ID')
await fs.writeFile(buildIdPath, buildId, 'utf8')
await writeFile(buildIdPath, buildId, 'utf8')
}

View file

@ -1,7 +1,10 @@
import { join } from 'path'
import { unlink } from 'mz/fs'
import promisify from '../../lib/promisify'
import fs from 'fs'
import { IS_BUNDLED_PAGE } from '../../utils'
const unlink = promisify(fs.unlink)
export default class UnlinkFilePlugin {
constructor () {
this.prevAssets = {}

View file

@ -1,5 +1,8 @@
import path from 'path'
import glob from 'glob-promise'
import promisify from '../../lib/promisify'
import globModule from 'glob'
const glob = promisify(globModule)
const nextPagesDir = path.join(__dirname, '..', '..', '..', 'pages')

View file

@ -4,9 +4,9 @@ import { resolve, join, sep } from 'path'
import { parse as parseUrl } from 'url'
import { parse as parseQs } from 'querystring'
import fs from 'fs'
import fsAsync from 'mz/fs'
import http, { STATUS_CODES } from 'http'
import updateNotifier from '@zeit/check-updates'
import promisify from './lib/promisify'
import {
renderToHTML,
renderErrorToHTML,
@ -24,6 +24,8 @@ import * as asset from '../lib/asset'
import * as envConfig from '../lib/runtime-config'
import { isResSent } from '../lib/utils'
const access = promisify(fs.access)
const blockedPages = {
'/_document': true,
'/_error': true
@ -267,7 +269,9 @@ export default class Server {
// [production] If the page is not exists, we need to send a proper Next.js style 404
// Otherwise, it'll affect the multi-zones feature.
if (!(await fsAsync.exists(p))) {
try {
await access(p, (fs.constants || fs).R_OK)
} catch (err) {
return await renderScriptError(req, res, page, { code: 'ENOENT' })
}

26
server/lib/promisify.js Normal file
View file

@ -0,0 +1,26 @@
const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs')
module.exports = function promisify (original) {
// Names to create an object from in case the callback receives multiple
// arguments, e.g. ['stdout', 'stderr'] for child_process.exec.
const argumentNames = original[kCustomPromisifyArgsSymbol]
return function fn (...args) {
return new Promise((resolve, reject) => {
try {
original.call(this, ...args, (err, ...values) => {
if (err) {
reject(err)
} else if (argumentNames !== undefined && values.length > 1) {
const obj = {}
for (var i = 0; i < argumentNames.length; i++) { obj[argumentNames[i]] = values[i] }
resolve(obj)
} else {
resolve(values[0])
}
})
} catch (err) {
reject(err)
}
})
}
}

View file

@ -3,7 +3,8 @@ import { EventEmitter } from 'events'
import { join } from 'path'
import { parse } from 'url'
import touch from 'touch'
import glob from 'glob-promise'
import promisify from './lib/promisify'
import globModule from 'glob'
import {normalizePagePath, pageNotFoundError} from './require'
import {createEntry} from './build/webpack/utils'
import { MATCH_ROUTE_NAME, IS_BUNDLED_PAGE } from './utils'
@ -12,6 +13,8 @@ const ADDED = Symbol('added')
const BUILDING = Symbol('building')
const BUILT = Symbol('built')
const glob = promisify(globModule)
export default function onDemandEntryHandler (devMiddleware, compilers, {
dir,
dev,