Skip to content

fix: handle Storybook and other sites that don't build #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion helpers/doesNotNeedPlugin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Checks all the cases for which the plugin should do nothing
const { redBright } = require('chalk')
const { redBright, yellowBright } = require('chalk')

const doesSiteUseNextOnNetlify = require('./doesSiteUseNextOnNetlify')
const isStaticExportProject = require('./isStaticExportProject')
const usesBuildCommand = require('./usesBuildCommand')

const doesNotNeedPlugin = ({ netlifyConfig, packageJson }) => {
const { build } = netlifyConfig
Expand All @@ -15,6 +16,13 @@ const doesNotNeedPlugin = ({ netlifyConfig, packageJson }) => {
return true
}

if (usesBuildCommand({ build, scripts, command: 'build-storybook' })) {
console.log(
yellowBright`Site seems to be building a Storybook rather than the Next.js site, so the Essential Next.js plugin will not run.`,
)
return true
}

return isStaticExportProject({ build, scripts }) || doesSiteUseNextOnNetlify({ packageJson })
}

Expand Down
13 changes: 3 additions & 10 deletions helpers/isStaticExportProject.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
const usesBuildCommand = require('./usesBuildCommand')

// Takes 1. Netlify config's build details and
// 2. the project's package.json scripts to determine if
// the Next.js app uses static HTML export
const isStaticExportProject = ({ build, scripts }) => {
const NEXT_EXPORT_COMMAND = 'next export'

if (!build.command) return false

const isSetInNetlifyConfig = build.command.includes(NEXT_EXPORT_COMMAND)

const isSetInNpmScript = Object.keys(scripts).find((script) => {
const scriptValue = scripts[script]
return build.command.includes(script) && scriptValue.includes(NEXT_EXPORT_COMMAND)
})

const isStaticExport = isSetInNetlifyConfig || isSetInNpmScript
const isStaticExport = usesBuildCommand({ build, scripts, command: NEXT_EXPORT_COMMAND })

if (isStaticExport) {
console.log(
Expand Down
15 changes: 15 additions & 0 deletions helpers/usesBuildCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Does the build command include this value, either directly or via an npm script?
const usesBuildCommand = ({ build, scripts, command }) => {
if (!build.command) return false

if (build.command.includes(command)) {
return true
}

return Object.entries(scripts).some(
// Search for a npm script that is being called by the build command, and includes the searched-for command
([scriptName, scriptValue]) => build.command.includes(scriptName) && scriptValue.includes(command),
)
}

module.exports = usesBuildCommand
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { readdirSync } = require('fs')
const { readdirSync, existsSync } = require('fs')
const path = require('path')

const makeDir = require('make-dir')
Expand Down Expand Up @@ -71,6 +71,19 @@ module.exports = {
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
return
}
console.log('Detected Next.js site. Copying files...')

const { distDir } = await getNextConfig(failBuild, nextRoot)

const dist = path.resolve(nextRoot, distDir)
if (!existsSync(dist)) {
failBuild(`
Could not find "${distDir}" after building the site, which indicates that "next build" was not run.
Check that your build command includes "next build". If the site is a monorepo, see https://ntl.fyi/next-monorepo
for information on configuring the site. If this is not a Next.js site you should remove the Essential Next.js plugin.
See https://ntl.fyi/remove-plugin for instructions.
`)
}

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

Expand All @@ -87,7 +100,7 @@ module.exports = {
utils.status.show({
title: 'Essential Next.js Build Plugin did not run',
summary: netlifyConfig.build.command
? 'The site either uses static export, or manually runs next-on-netlify'
? 'The site either uses static export, manually runs next-on-netlify, or is not a Next.js site'
: 'The site config does not specify a build command',
})
return
Expand Down
32 changes: 32 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable max-lines */
/* eslint-disable max-lines-per-function */
const path = require('path')
const process = require('process')

Expand Down Expand Up @@ -156,6 +158,33 @@ describe('preBuild()', () => {
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
})

test('do nothing if build command includes "build-storybook"', async () => {
await plugin.onPreBuild({
netlifyConfig,
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'build-storybook' } },
utils,
})
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
})

test('do nothing if build command calls a script that includes "build-storybook"', async () => {
await plugin.onPreBuild({
netlifyConfig: { build: { command: 'npm run storybook' } },
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { storybook: 'build-storybook' } },
utils,
})
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
})

test('run plugin if app has build-storybook in an unused script', async () => {
await plugin.onPreBuild({
netlifyConfig,
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { storybook: 'build-storybook' } },
utils,
})
expect(process.env.NEXT_PRIVATE_TARGET).toBe('serverless')
})

test('fail build if the app has no package.json', async () => {
await expect(
plugin.onPreBuild({
Expand Down Expand Up @@ -289,3 +318,6 @@ describe('onPostBuild', () => {
expect(path.normalize(manifestPath.digests[0])).toBe(path.normalize('build/build-manifest.json'))
})
})

/* eslint-enable max-lines */
/* eslint-enable max-lines-per-function */