8000 Merge branch 'main' into chore/canary-script · opennextjs/opennextjs-netlify@35cb7f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 35cb7f4

Browse files
authored
Merge branch 'main' into chore/canary-script
2 parents 1935963 + 9477e0e commit 35cb7f4

25 files changed

+728
-708
lines changed

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ module.exports = {
2525
'unicorn/filename-case': 0,
2626
'unicorn/no-array-push-push': 0,
2727
},
28+
env: {
29+
jest: true,
30+
},
2831
overrides: [...overrides],
2932
}

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
# Changelog
22

3+
### [3.7.1](https://www.github.com/netlify/netlify-plugin-nextjs/compare/v3.7.0...v3.7.1) (2021-07-15)
4+
5+
6+
### Bug Fixes
7+
8+
* catch more export cases ([#529](https://www.github.com/netlify/netlify-plugin-nextjs/issues/529)) ([41184bc](https://www.github.com/netlify/netlify-plugin-nextjs/commit/41184bc6895a1e4b8f5a72ffd97ab81d86da5492))
9+
10+
## [3.7.0](https://www.github.com/netlify/netlify-plugin-nextjs/compare/v3.6.3...v3.7.0) (2021-07-13)
11+
12+
13+
### Features
14+
15+
* add support for using image config from next.config.js ([#518](https://www.github.com/netlify/netlify-plugin-nextjs/issues/518)) ([bc7695d](https://www.github.com/netlify/netlify-plugin-nextjs/commit/bc7695df698202d77ff65facf101e13a2f318997))
16+
17+
18+
### Bug Fixes
19+
20+
* disable png palette ([#525](https://www.github.com/netlify/netlify-plugin-nextjs/issues/525)) ([e0b4bf7](https://www.github.com/netlify/netlify-plugin-nextjs/commit/e0b4bf76d2b010ebacd6c544064c439051aee1ef))
21+
* more robust static site detection ([#505](https://www.github.com/netlify/netlify-plugin-nextjs/issues/505)) ([b1fd513](https://www.github.com/netlify/netlify-plugin-nextjs/commit/b1fd513c8ac142c335bc3ae950bd093da20466fe))
22+
* use POSIX paths for Windows require()s ([#520](https://www.github.com/netlify/netlify-plugin-nextjs/issues/520)) ([c34dbb0](https://www.github.com/netlify/netlify-plugin-nextjs/commit/c34dbb0a81c282efd4e49248cf1f383dde7c11e1))
23+
324
### [3.6.3](https://www.github.com/netlify/netlify-plugin-nextjs/compare/v3.6.2...v3.6.3) (2021-07-07)
425

526

helpers/cacheBuild.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ const path = require('path')
33
const DEFAULT_DIST_DIR = '.next'
44

55
// Account for possible custom distDir
6-
const getPath = (distDir, source) => {
7-
return path.join(distDir || DEFAULT_DIST_DIR, source)
8-
}
6+
const getPath = (nextRoot, distDir, source) => path.join(nextRoot, distDir || DEFAULT_DIST_DIR, source)
97

10-
const restoreCache = async ({ cache, distDir }) => {
11-
const cacheDir = getPath(distDir, 'cache')
8+
const restoreCache = async ({ cache, distDir, nextRoot }) => {
9+
const cacheDir = getPath(nextRoot, distDir, 'cache')
1210
if (await cache.restore(cacheDir)) {
1311
console.log('Next.js cache restored.')
1412
} else {
1513
console.log('No Next.js cache to restore.')
1614
}
1715
}
1816

19-
const saveCache = async ({ cache, distDir }) => {
20-
const cacheDir = getPath(distDir, 'cache')
21-
const buildManifest = getPath(distDir, 'build-manifest.json')
17+
const saveCache = async ({ cache, distDir, nextRoot }) => {
18+
const cacheDir = getPath(nextRoot, distDir, 'cache')
19+
20+
const buildManifest = getPath(nextRoot, distDir, 'build-manifest.json')
2221
if (await cache.save(cacheDir, { digests: [buildManifest] })) {
2322
console.log('Next.js cache saved.')
2423
} else {

helpers/usesBuildCommand.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const parseNpmScript = require('@netlify/parse-npm-script')
2+
3+
const COMMAND_PLACEHOLDER = '___netlifybuildcommand'
4+
25
// Does the build command include this value, either directly or via an npm script?
36
const usesBuildCommand = ({ build, scripts, command }) => {
47
if (!build.command) return false
@@ -10,12 +13,23 @@ const usesBuildCommand = ({ build, scripts, command }) => {
1013
if (!build.command.includes('npm run') && !build.command.includes('yarn')) {
1114
return false
1215
}
16+
17+
// Insert a fake script to represent the build command
18+
19+
const commands = { ...scripts, [COMMAND_PLACEHOLDER]: build.command }
20+
1321
// This resolves the npm script that is actually being run
1422
try {
15-
const { raw } = parseNpmScript({ scripts }, build.command)
23+
const { raw } = parseNpmScript({ scripts: commands }, COMMAND_PLACEHOLDER)
1624
return raw.some((script) => script.includes(command))
1725
} catch (error) {
18-
console.error('There was an error parsing your build command', error)
26+
console.error(
27+
'There was an error parsing your build command:',
28+
error.message,
29+
`\n\nThe build command is "${build.command}" and the available npm scripts are: ${Object.keys(scripts)
30+
.map((script) => `"${script}"`)
31+
.join(', ')}`,
32+
)
1933
}
2034
}
2135

index.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { readdirSync, existsSync } = require('fs')
22
const path = require('path')
33

44
const makeDir = require('make-dir')
5+
const { satisfies } = require('semver')
56

67
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
78
const checkNxConfig = require('./helpers/checkNxConfig')
@@ -12,12 +13,16 @@ const getNextRoot = require('./helpers/getNextRoot')
1213
const validateNextUsage = require('./helpers/validateNextUsage')
1314
const verifyBuildTarget = require('./helpers/verifyBuildTarget')
1415
const nextOnNetlify = require('./src')
16+
17+
// This is when the esbuild dynamic import support was added
18+
const REQUIRED_BUILD_VERSION = '>=15.11.5'
19+
1520
// * Helpful Plugin Context *
1621
// - Between the prebuild and build steps, the project's build command is run
1722
// - Between the build and postbuild steps, any functions are bundled
1823

1924
module.exports = {
20-
async onPreBuild({ netlifyConfig, packageJson, utils, constants }) {
25+
async onPreBuild({ netlifyConfig, packageJson, utils, constants = {} }) {
2126
const { failBuild } = utils.build
2227

2328
validateNextUsage({ failBuild, netlifyConfig })
@@ -30,6 +35,13 @@ module.exports = {
3035
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
3136
return
3237
}
38+
// We check for build version because that's what's available to us, but prompt about the cli because that's what they can upgrade
39+
if (constants.IS_LOCAL && !satisfies(constants.NETLIFY_BUILD_VERSION, REQUIRED_BUILD_VERSION)) {
40+
return failBuild(
41+
`This version of the Essential Next.js plugin requires netlify-cli@4.4.2 or higher. Please upgrade and try again.
42+
You can do this by running: "npm install -g netlify-cli@latest" or "yarn global add netlify-cli@latest"`,
43+
)
44+
}
3345

3446
// Populates the correct config if needed
3547
await verifyBuildTarget({ netlifyConfig, packageJson, failBuild })
@@ -54,7 +66,7 @@ module.exports = {
5466
`The Essential Next.js plugin now supports reading image domains from your Next config, so using NEXT_IMAGE_ALLOWED_DOMAINS is now deprecated. Please set images.domains in next.config.js instead, and remove the NEXT_IMAGE_ALLOWED_DOMAINS variable.`,
5567
)
5668
}
57-
await restoreCache({ cache: utils.cache, distDir: nextConfig.distDir })
69+
await restoreCache({ cache: utils.cache, distDir: nextConfig.distDir, nextRoot })
5870
},
5971
async onBuild({
6072
netlifyConfig,
@@ -71,14 +83,15 @@ module.exports = {
7183
}
7284
console.log('Detected Next.js site. Copying files...')
7385

74-
const { distDir } = await getNextConfig(failBuild, nextRoot)
75-
86+
const { distDir, configFile } = await getNextConfig(failBuild, nextRoot)
7687
const dist = path.resolve(nextRoot, distDir)
88+
7789
if (!existsSync(dist)) {
7890
failBuild(`
79-
Could not find "${distDir}" after building the site, which indicates that "next build" was not run.
80-
Check that your build command includes "next build". If the site is a monorepo, see https://ntl.fyi/next-monorepo
81-
for information on configuring the site. If this is not a Next.js site you should remove the Essential Next.js plugin.
91+
Could not find "${distDir}" after building the site, which indicates that "next build" was not run or that we're looking in the wrong place.
92+
We're using the config file ${configFile}, and looking for the dist directory at ${dist}. If this is incorrect, try deleting the config file, or
93+
moving it to the correct place. Check that your build command includes "next build". If the site is a monorepo, see https://ntl.fyi/next-monorepo
94+
for information on configuring the site. If this is not a Next.js site, or if it uses static export, you should remove the Essential Next.js plugin.
8295
See https://ntl.fyi/remove-plugin for instructions.
8396
`)
8497
}
@@ -106,7 +119,7 @@ See https://ntl.fyi/remove-plugin for instructions.
106119
const nextRoot = getNextRoot({ netlifyConfig })
107120

108121
const nextConfig = await getNextConfig(utils.failBuild, nextRoot)
109-
await saveCache({ cache: utils.cache, distDir: nextConfig.distDir })
122+
await saveCache({ cache: utils.cache, distDir: nextConfig.distDir, nextRoot })
110123
copyUnstableIncludedDirs({ nextConfig, functionsDist: path.resolve(FUNCTIONS_DIST) })
111124
utils.status.show({
112125
title: 'Essential Next.js Build Plugin ran successfully',

0 commit comments

Comments
 (0)
0