Skip to content

Commit

Permalink
feat: support Next.js v5 and "config as a function" (#5)
Browse files Browse the repository at this point in the history
* drop Node 4 from testing
* bump patch or minor dev deps
* bump tap major
* temporarily comment out Next 6 default config prop
  • Loading branch information
nexdrew committed May 24, 2018
1 parent ba10149 commit 2b02037
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ test/fixture/.next/
test/fixture/nextoutput/
test/fixture/next.config.js
test/fixture2/.next/BUILD_ID
test/fixture-next5/build/
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ node_js:
- 'node'
- '8'
- '6'
- '4'
after_success: npm run coverage
24 changes: 21 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ const path = require('path')

const buildIdFiles = ['BUILD_ID', 'build-stats.json']

// from https://github.com/zeit/next.js/blob/canary/server/config.js
const defaultConfig = {
webpack: null,
webpackDevMiddleware: null,
poweredByHeader: true,
distDir: '.next',
assetPrefix: '',
configOrigin: 'default',
useFileSystemPublicRoutes: true,
// generateBuildId: () => '9f2a37be-4545-445e-91bd-' + String(new Date().getTime()).slice(1, 13),
generateEtags: true,
pageExtensions: ['jsx', 'js']
}

module.exports = function nextBuildId (opts) {
// TODO support opts.conf object similar to next(opts) ??
opts = opts || {}
Expand Down Expand Up @@ -38,13 +52,17 @@ function resolveInputDir (dir) {
}

function resolveOutputDir (inputDir) {
let outputDir = '.next'
let outputDir = defaultConfig.distDir
const nextConfigFile = path.join(inputDir, 'next.config.js')
// avoid slow require if file doesn't exist where it should
if (fs.existsSync(nextConfigFile)) {
try {
const nextConfig = require(nextConfigFile)
if (nextConfig && nextConfig.distDir) outputDir = nextConfig.distDir
const userConfigModule = require(nextConfigFile)
let userConfig = userConfigModule.default || userConfigModule
if (typeof userConfigModule === 'function') {
userConfig = userConfigModule('phase-production-build', { defaultConfig })
}
if (userConfig && userConfig.distDir) outputDir = userConfig.distDir
} catch (e) {}
}
outputDir = path.resolve(inputDir, outputDir)
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
"sywac": "^1.2.0"
},
"devDependencies": {
"coveralls": "^3.0.0",
"coveralls": "^3.0.1",
"rimraf": "^2.6.2",
"standard": "^11.0.1",
"standard-version": "^4.3.0",
"tap": "^11.1.3"
"standard-version": "^4.4.0",
"tap": "^12.0.1"
}
}
24 changes: 24 additions & 0 deletions test/fixture-next5/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const withPlugins = require('next-compose-plugins')
const images = require('next-images')
const sass = require('@zeit/next-sass')

// next.js configuration
const nextConfig = {
useFileSystemPublicRoutes: false,
distDir: 'build'
}

module.exports = withPlugins([

// add a plugin with specific configuration
[sass, {
cssModules: true,
cssLoaderOptions: {
localIdentName: '[local]___[hash:base64:5]'
}
}],

// add a plugin without a configuration
images

], nextConfig)
20 changes: 20 additions & 0 deletions test/fixture-next5/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "fixture",
"version": "1.0.0",
"description": "Fixture to test against Next.js 5",
"private": true,
"main": "index.js",
"scripts": {
"build": "next build"
},
"author": "",
"license": "ISC",
"dependencies": {
"@zeit/next-sass": "^0.2.0",
"next": "^5.1.0",
"next-compose-plugins": "^2.1.1",
"next-images": "^0.10.2",
"react": "^16.3.2",
"react-dom": "^16.3.2"
}
}
1 change: 1 addition & 0 deletions test/fixture-next5/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <div>Welcome to next.js!</div>
43 changes: 43 additions & 0 deletions test/test-next5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const path = require('path')

const tap = require('tap')
const rimraf = require('rimraf')

const utils = require('./utils')

const exec = utils.exec
const cli = utils.cli
const readTextFile = utils.readTextFile
const fixturePath = path.resolve(__dirname, 'fixture-next5')

let npmi = false
tap.beforeEach(() => {
if (npmi) return Promise.resolve()
npmi = true
return exec('npm', 'i', fixturePath)
})

tap.afterEach(() => {
return Promise.all(['build'].map(dir => {
return new Promise((resolve, reject) => {
rimraf(path.resolve(fixturePath, dir), err => {
if (err) return reject(err)
resolve()
})
})
}))
})

tap.test('with next.config.js that exports a function', t => {
return exec('npm', 'run build', fixturePath).then(() => {
return cli('', fixturePath)
}).then(io => {
t.notOk(io.err)
t.notOk(io.stderr)
t.match(io.stdout, /Build ID: 0123456789abcdef0123456789abcdef01234567/)
t.match(io.stdout, /Updated:.*BUILD_ID/)
return readTextFile(path.resolve(fixturePath, 'build', 'BUILD_ID'))
}).then(buildId => {
t.equal(buildId, '0123456789abcdef0123456789abcdef01234567')
})
})

0 comments on commit 2b02037

Please sign in to comment.