From 4146cefe9f992d3086510ce00c63a7ab389760d1 Mon Sep 17 00:00:00 2001 From: nkzawa Date: Wed, 15 Feb 2017 20:42:12 +0900 Subject: [PATCH] build on tmp dir --- .gitignore | 1 - server/build/clean.js | 4 ++-- server/build/gzip.js | 4 ++-- server/build/index.js | 31 +++++++++++++++++++------------ server/build/replace.js | 28 +++++++++++++--------------- server/build/webpack.js | 4 ++-- server/hot-reloader.js | 2 +- 7 files changed, 39 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index 30d546ad9762a..5f0fa2bda018c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,6 @@ npm-debug.log # other .next -.next-* # coverage .nyc_output diff --git a/server/build/clean.js b/server/build/clean.js index 1e46fd6e51911..f0d0876a47dbf 100644 --- a/server/build/clean.js +++ b/server/build/clean.js @@ -1,6 +1,6 @@ import { resolve } from 'path' import del from 'del' -export default function clean (dir, folderName = '.next') { - return del(resolve(dir, folderName)) +export default function clean (dir) { + return del(resolve(dir, '.next')) } diff --git a/server/build/gzip.js b/server/build/gzip.js index 1a03eb4703e99..c3ae485fbb734 100644 --- a/server/build/gzip.js +++ b/server/build/gzip.js @@ -3,8 +3,8 @@ import path from 'path' import zlib from 'zlib' import glob from 'glob-promise' -export default async function gzipAssets (dir, buildFolder = '.next') { - const nextDir = path.resolve(dir, buildFolder) +export default async function gzipAssets (dir) { + const nextDir = path.resolve(dir, '.next') const coreAssets = [ path.join(nextDir, 'commons.js'), diff --git a/server/build/index.js b/server/build/index.js index b2f9942d9a614..841571c10bf99 100644 --- a/server/build/index.js +++ b/server/build/index.js @@ -1,22 +1,29 @@ +import { tmpdir } from 'os' +import { join } from 'path' import fs from 'mz/fs' import uuid from 'uuid' -import path from 'path' +import del from 'del' import webpack from './webpack' -import clean from './clean' import gzipAssets from './gzip' import replaceCurrentBuild from './replace' export default async function build (dir) { - const distFolder = '.next' - const buildFolder = `.next-${uuid.v4()}` - const compiler = await webpack(dir, buildFolder) + const buildDir = join(tmpdir(), uuid.v4()) + const compiler = await webpack(dir, { buildDir }) - await runCompiler(compiler) - const oldFolder = await replaceCurrentBuild(dir, buildFolder, distFolder) - await gzipAssets(dir, distFolder) - await writeBuildId(dir, distFolder) + try { + await runCompiler(compiler) + await gzipAssets(buildDir) + await writeBuildId(buildDir) + } catch (err) { + console.error(`> Failed to build on ${buildDir}`) + throw err + } - clean(dir, oldFolder) + await replaceCurrentBuild(dir, buildDir) + + // no need to wait + del(buildDir) } function runCompiler (compiler) { @@ -37,8 +44,8 @@ function runCompiler (compiler) { }) } -async function writeBuildId (dir, distFolder) { - const buildIdPath = path.resolve(dir, distFolder, 'BUILD_ID') +async function writeBuildId (dir) { + const buildIdPath = join(dir, '.next', 'BUILD_ID') const buildId = uuid.v4() await fs.writeFile(buildIdPath, buildId, 'utf8') } diff --git a/server/build/replace.js b/server/build/replace.js index 09e5bd1a53fac..ba55702ead5e2 100644 --- a/server/build/replace.js +++ b/server/build/replace.js @@ -1,18 +1,16 @@ -import fs from 'fs' -import path from 'path' -import uuid from 'uuid' +import { rename } from 'mz/fs' +import { join } from 'path' -export default function replaceCurrentBuild (dir, buildFolder, distFolder) { - const distDir = path.resolve(dir, distFolder) - const buildDir = path.resolve(dir, buildFolder) - const oldDir = path.resolve(dir, `.next-${uuid.v4()}`) +export default async function replaceCurrentBuild (dir, buildDir) { + const _dir = join(dir, '.next') + const _buildDir = join(buildDir, '.next') + const oldDir = join(buildDir, '.next.old') - return new Promise((resolve, reject) => { - fs.rename(distDir, oldDir, () => { - fs.rename(buildDir, distDir, (err) => { - if (err) return reject(err) - resolve(oldDir) - }) - }) - }) + try { + await rename(_dir, oldDir) + } catch (err) { + if (err.code !== 'ENOENT') throw err + } + await rename(_buildDir, _dir) + return oldDir } diff --git a/server/build/webpack.js b/server/build/webpack.js index 1af62b8e2fb14..987470e6424a0 100644 --- a/server/build/webpack.js +++ b/server/build/webpack.js @@ -23,7 +23,7 @@ const interpolateNames = new Map(defaultPages.map((p) => { return [join(nextPagesDir, p), `dist/pages/${p}`] })) -export default async function createCompiler (dir, buildFolder, { dev = false, quiet = false } = {}) { +export default async function createCompiler (dir, { dev = false, quiet = false, buildDir } = {}) { dir = resolve(dir) const config = getConfig(dir) const defaultEntries = dev @@ -228,7 +228,7 @@ export default async function createCompiler (dir, buildFolder, { dev = false, q context: dir, entry, output: { - path: join(dir, buildFolder || '.next'), + path: join(buildDir || dir, '.next'), filename: '[name]', libraryTarget: 'commonjs2', publicPath: '/_webpack/', diff --git a/server/hot-reloader.js b/server/hot-reloader.js index 7d837dc7fdf12..37eeecbd4cc06 100644 --- a/server/hot-reloader.js +++ b/server/hot-reloader.js @@ -35,7 +35,7 @@ export default class HotReloader { async start () { const [compiler] = await Promise.all([ - webpack(this.dir, null, { dev: true, quiet: this.quiet }), + webpack(this.dir, { dev: true, quiet: this.quiet }), clean(this.dir) ])