Skip to content

fix: skip plugin if build command is empty #471

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 3 commits into from
Jul 1, 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
9 changes: 9 additions & 0 deletions helpers/doesNotNeedPlugin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
// Checks all the cases for which the plugin should do nothing
const { redBright } = require('chalk')

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

const doesNotNeedPlugin = ({ netlifyConfig, packageJson }) => {
const { build } = netlifyConfig
const { scripts = {} } = packageJson

if (!build.command) {
console.log(
redBright`⚠️ Warning: No build command specified in the site's Netlify config, so plugin will not run. This deploy will fail unless you have already exported the site. ⚠️`,
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lindsaylevine How about this? It also shows up in the build status now.

return true
}

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

Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ module.exports = {
if (doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
utils.status.show({
title: 'Essential Next.js Build Plugin did not run',
summary: 'The site either uses static export, or manually runs next-on-netlify',
summary: netlifyConfig.build.command
? 'The site either uses static export, or manually runs next-on-netlify'
: 'The site config does not specify a build command',
})
return
}
Expand Down
15 changes: 13 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,20 @@ afterEach(async () => {
})

const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' }
const netlifyConfig = { build: {} }
const netlifyConfig = { build: { command: 'next build' } }

describe('preBuild()', () => {
test('do nothing if the app has no build command', async () => {
await plugin.onPreBuild({
netlifyConfig: { build: { command: '' } },
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'next build' } },
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
})

expect(await pathExists('next.config.js')).toBeFalsy()
})

test('do nothing if the app has static html export in npm script', async () => {
await plugin.onPreBuild({
netlifyConfig: { build: { command: 'npm run build' } },
Expand Down Expand Up @@ -205,7 +216,7 @@ describe('onBuild()', () => {
await moveNextDist()
const PUBLISH_DIR = 'publish'
await plugin.onBuild({
netlifyConfig: { build: { publish: path.resolve(PUBLISH_DIR) } },
netlifyConfig: { build: { publish: path.resolve(PUBLISH_DIR), command: 'next build' } },
packageJson: DUMMY_PACKAGE_JSON,
constants: {
PUBLISH_DIR,
Expand Down