From 72c36decd8468a1c4ec6fcfbaeff70534c3b51e1 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 8 Nov 2023 11:26:11 +0100 Subject: [PATCH 1/3] fix: respect build plugins changing `publish` dir --- src/utils/run-build.mjs | 3 ++- .../plugin-changing-publish-dir/dist/index.html | 1 + .../plugin-changing-publish-dir/netlify.toml | 2 ++ .../plugin-changing-publish-dir/plugin/index.js | 5 +++++ .../plugin-changing-publish-dir/plugin/manifest.yml | 1 + .../plugin-changing-publish-dir/plugin/package.json | 1 + tests/integration/commands/dev/serve.test.ts | 10 ++++++++++ tests/integration/utils/fixture.ts | 3 ++- 8 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/integration/__fixtures__/plugin-changing-publish-dir/dist/index.html create mode 100644 tests/integration/__fixtures__/plugin-changing-publish-dir/netlify.toml create mode 100644 tests/integration/__fixtures__/plugin-changing-publish-dir/plugin/index.js create mode 100644 tests/integration/__fixtures__/plugin-changing-publish-dir/plugin/manifest.yml create mode 100644 tests/integration/__fixtures__/plugin-changing-publish-dir/plugin/package.json create mode 100644 tests/integration/commands/dev/serve.test.ts diff --git a/src/utils/run-build.mjs b/src/utils/run-build.mjs index da80f829105..a4e6677ef06 100644 --- a/src/utils/run-build.mjs +++ b/src/utils/run-build.mjs @@ -110,7 +110,7 @@ export const runNetlifyBuild = async ({ command, env = {}, options, settings, ti } // Run Netlify Build using the main entry point. - const { success } = await buildSite(buildSiteOptions) + const { netlifyConfig, success } = await buildSite(buildSiteOptions) if (!success) { error('Could not start local server due to a build error') @@ -123,6 +123,7 @@ export const runNetlifyBuild = async ({ command, env = {}, options, settings, ti await devCommand({ command: undefined, useStaticServer: true, + dist: netlifyConfig?.build?.publish, }) return { configPath: tempConfigPath } diff --git a/tests/integration/__fixtures__/plugin-changing-publish-dir/dist/index.html b/tests/integration/__fixtures__/plugin-changing-publish-dir/dist/index.html new file mode 100644 index 00000000000..c57eff55ebc --- /dev/null +++ b/tests/integration/__fixtures__/plugin-changing-publish-dir/dist/index.html @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/tests/integration/__fixtures__/plugin-changing-publish-dir/netlify.toml b/tests/integration/__fixtures__/plugin-changing-publish-dir/netlify.toml new file mode 100644 index 00000000000..7caee501c35 --- /dev/null +++ b/tests/integration/__fixtures__/plugin-changing-publish-dir/netlify.toml @@ -0,0 +1,2 @@ +[[plugins]] + package="/plugin" \ No newline at end of file diff --git a/tests/integration/__fixtures__/plugin-changing-publish-dir/plugin/index.js b/tests/integration/__fixtures__/plugin-changing-publish-dir/plugin/index.js new file mode 100644 index 00000000000..b70100e91f5 --- /dev/null +++ b/tests/integration/__fixtures__/plugin-changing-publish-dir/plugin/index.js @@ -0,0 +1,5 @@ +export default { + onBuild({netlifyConfig}){ + netlifyConfig.build.publish = "dist" + } +} \ No newline at end of file diff --git a/tests/integration/__fixtures__/plugin-changing-publish-dir/plugin/manifest.yml b/tests/integration/__fixtures__/plugin-changing-publish-dir/plugin/manifest.yml new file mode 100644 index 00000000000..49a980b9bb5 --- /dev/null +++ b/tests/integration/__fixtures__/plugin-changing-publish-dir/plugin/manifest.yml @@ -0,0 +1 @@ +name: change-publish-dir \ No newline at end of file diff --git a/tests/integration/__fixtures__/plugin-changing-publish-dir/plugin/package.json b/tests/integration/__fixtures__/plugin-changing-publish-dir/plugin/package.json new file mode 100644 index 00000000000..bb34440a365 --- /dev/null +++ b/tests/integration/__fixtures__/plugin-changing-publish-dir/plugin/package.json @@ -0,0 +1 @@ +{ "type": "module" } \ No newline at end of file diff --git a/tests/integration/commands/dev/serve.test.ts b/tests/integration/commands/dev/serve.test.ts new file mode 100644 index 00000000000..da64388b276 --- /dev/null +++ b/tests/integration/commands/dev/serve.test.ts @@ -0,0 +1,10 @@ +import { expect, test } from 'vitest' + +import { FixtureTestContext, setupFixtureTests } from '../../utils/fixture.js' + +setupFixtureTests('plugin-changing-publish-dir', { devServer: { serve: true } }, () => { + test('ntl serve should respect plugins changing publish dir', async ({ devServer }) => { + const response = await fetch(`http://localhost:${devServer.port}/`) + expect(response.status).toBe(200) + }) +}) diff --git a/tests/integration/utils/fixture.ts b/tests/integration/utils/fixture.ts index 74e1e836b1c..1a5a01e9b71 100644 --- a/tests/integration/utils/fixture.ts +++ b/tests/integration/utils/fixture.ts @@ -27,7 +27,7 @@ export interface FixtureTestContext { type LifecycleHook = (context: FixtureTestContext) => Promise | void export interface FixtureOptions { - devServer?: boolean + devServer?: boolean | { serve?: boolean } mockApi?: MockApiOptions /** * Executed after fixture setup, but before tests run @@ -148,6 +148,7 @@ export async function setupFixtureTests( if (options.devServer) { devServer = await startDevServer({ + serve: typeof options.devServer === 'object' && options.devServer.serve, cwd: fixture.directory, offline: !mockApi, args: ['--country', 'DE'], From 2d56ce23a1ba6a0d6795d0203eb16708c469d620 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 8 Nov 2023 11:27:42 +0100 Subject: [PATCH 2/3] fix: prefer `--dir` --- src/utils/run-build.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/run-build.mjs b/src/utils/run-build.mjs index a4e6677ef06..8cf2489d1fa 100644 --- a/src/utils/run-build.mjs +++ b/src/utils/run-build.mjs @@ -123,7 +123,7 @@ export const runNetlifyBuild = async ({ command, env = {}, options, settings, ti await devCommand({ command: undefined, useStaticServer: true, - dist: netlifyConfig?.build?.publish, + dist: options.dir ? undefined : netlifyConfig?.build?.publish, }) return { configPath: tempConfigPath } From 1a4f1bb51aa2d93ce539bff4537890d60d9422cf Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 8 Nov 2023 11:56:17 +0100 Subject: [PATCH 3/3] fix: only override dist when its wanted --- src/utils/run-build.mjs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/utils/run-build.mjs b/src/utils/run-build.mjs index 8cf2489d1fa..e1f6f8ce6bd 100644 --- a/src/utils/run-build.mjs +++ b/src/utils/run-build.mjs @@ -120,11 +120,14 @@ export const runNetlifyBuild = async ({ command, env = {}, options, settings, ti // Start the dev server, forcing the usage of a static server as opposed to // the framework server. - await devCommand({ + const settingsOverrides = { command: undefined, useStaticServer: true, - dist: options.dir ? undefined : netlifyConfig?.build?.publish, - }) + } + if (!options.dir && netlifyConfig?.build?.publish) { + settingsOverrides.dist = netlifyConfig.build.publish + } + await devCommand(settingsOverrides) return { configPath: tempConfigPath } }