Skip to content

Commit 9deacfb

Browse files
authored
fix: handle Storybook and other sites that don't build (#496)
1 parent 1601604 commit 9deacfb

File tree

5 files changed

+74
-13
lines changed

5 files changed

+74
-13
lines changed

helpers/doesNotNeedPlugin.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Checks all the cases for which the plugin should do nothing
2-
const { redBright } = require('chalk')
2+
const { redBright, yellowBright } = require('chalk')
33

44
const doesSiteUseNextOnNetlify = require('./doesSiteUseNextOnNetlify')
55
const isStaticExportProject = require('./isStaticExportProject')
6+
const usesBuildCommand = require('./usesBuildCommand')
67

78
const doesNotNeedPlugin = ({ netlifyConfig, packageJson }) => {
89
const { build } = netlifyConfig
@@ -15,6 +16,13 @@ const doesNotNeedPlugin = ({ netlifyConfig, packageJson }) => {
1516
return true
1617
}
1718

19+
if (usesBuildCommand({ build, scripts, command: 'build-storybook' })) {
20+
console.log(
21+
yellowBright`Site seems to be building a Storybook rather than the Next.js site, so the Essential Next.js plugin will not run.`,
22+
)
23+
return true
24+
}
25+
1826
return isStaticExportProject({ build, scripts }) || doesSiteUseNextOnNetlify({ packageJson })
1927
}
2028

helpers/isStaticExportProject.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1+
const usesBuildCommand = require('./usesBuildCommand')
2+
13
// Takes 1. Netlify config's build details and
24
// 2. the project's package.json scripts to determine if
35
// the Next.js app uses static HTML export
46
const isStaticExportProject = ({ build, scripts }) => {
57
const NEXT_EXPORT_COMMAND = 'next export'
68

7-
if (!build.command) return false
8-
9-
const isSetInNetlifyConfig = build.command.includes(NEXT_EXPORT_COMMAND)
10-
11-
const isSetInNpmScript = Object.keys(scripts).find((script) => {
12-
const scriptValue = scripts[script]
13-
return build.command.includes(script) && scriptValue.includes(NEXT_EXPORT_COMMAND)
14-
})
15-
16-
const isStaticExport = isSetInNetlifyConfig || isSetInNpmScript
9+
const isStaticExport = usesBuildCommand({ build, scripts, command: NEXT_EXPORT_COMMAND })
1710

1811
if (isStaticExport) {
1912
console.log(

helpers/usesBuildCommand.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Does the build command include this value, either directly or via an npm script?
2+
const usesBuildCommand = ({ build, scripts, command }) => {
3+
if (!build.command) return false
4+
5+
if (build.command.includes(command)) {
6+
return true
7+
}
8+
9+
return Object.entries(scripts).some(
10+
// Search for a npm script that is being called by the build command, and includes the searched-for command
11+
([scriptName, scriptValue]) => build.command.includes(scriptName) && scriptValue.includes(command),
12+
)
13+
}
14+
15+
module.exports = usesBuildCommand

index.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { readdirSync } = require('fs')
1+
const { readdirSync, existsSync } = require('fs')
22
const path = require('path')
33

44
const makeDir = require('make-dir')
@@ -71,6 +71,19 @@ module.exports = {
7171
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
7272
return
7373
}
74+
console.log('Detected Next.js site. Copying files...')
75+
76+
const { distDir } = await getNextConfig(failBuild, nextRoot)
77+
78+
const dist = path.resolve(nextRoot, distDir)
79+
if (!existsSync(dist)) {
80+
failBuild(`
81+
Could not find "${distDir}" after building the site, which indicates that "next build" was not run.
82+
Check that your build command includes "next build". If the site is a monorepo, see https://ntl.fyi/next-monorepo
83+
for information on configuring the site. If this is not a Next.js site you should remove the Essential Next.js plugin.
84+
See https://ntl.fyi/remove-plugin for instructions.
85+
`)
86+
}
7487

7588
console.log(`** Running Next on Netlify package **`)
7689

@@ -87,7 +100,7 @@ module.exports = {
87100
utils.status.show({
88101
title: 'Essential Next.js Build Plugin did not run',
89102
summary: netlifyConfig.build.command
90-
? 'The site either uses static export, or manually runs next-on-netlify'
103+
? 'The site either uses static export, manually runs next-on-netlify, or is not a Next.js site'
91104
: 'The site config does not specify a build command',
92105
})
93106
return

test/index.js

+32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable max-lines */
2+
/* eslint-disable max-lines-per-function */
13
const path = require('path')
24
const process = require('process')
35

@@ -156,6 +158,33 @@ describe('preBuild()', () => {
156158
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
157159
})
158160

161+
test('do nothing if build command includes "build-storybook"', async () => {
162+
await plugin.onPreBuild({
163+
netlifyConfig,
164+
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'build-storybook' } },
165+
utils,
166+
})
167+
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
168+
})
169+
170+
test('do nothing if build command calls a script that includes "build-storybook"', async () => {
171+
await plugin.onPreBuild({
172+
netlifyConfig: { build: { command: 'npm run storybook' } },
173+
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { storybook: 'build-storybook' } },
174+
utils,
175+
})
176+
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
177+
})
178+
179+
test('run plugin if app has build-storybook in an unused script', async () => {
180+
await plugin.onPreBuild({
181+
netlifyConfig,
182+
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { storybook: 'build-storybook' } },
183+
utils,
184+
})
185+
expect(process.env.NEXT_PRIVATE_TARGET).toBe('serverless')
186+
})
187+
159188
test('fail build if the app has no package.json', async () => {
160189
await expect(
161190
plugin.onPreBuild({
@@ -289,3 +318,6 @@ describe('onPostBuild', () => {
289318
expect(path.normalize(manifestPath.digests[0])).toBe(path.normalize('build/build-manifest.json'))
290319
})
291320
})
321+
322+
/* eslint-enable max-lines */
323+
/* eslint-enable max-lines-per-function */

0 commit comments

Comments
 (0)