From 0c9e066e9164dee341c8e6949c453eb49a0cded6 Mon Sep 17 00:00:00 2001 From: jthegedus Date: Thu, 6 May 2021 15:37:21 +1000 Subject: [PATCH 1/5] docs: clarify some language in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ffbf6e2..3f6c7d9 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ In your standard SvelteKit project: - `npm run build` - Follow further instructions output by the adapter to prepare for deployment. -This adapter reads your `firebase.json` to determine if the Firebase Hosting site is using Cloud Functions or Cloud Run and outputs the server pieces accordingly. Static assets are output to the `public` directory for the Hosting site config. +This adapter reads `firebase.json` to determine if the Firebase Hosting site is using Cloud Functions or Cloud Run and outputs the server pieces accordingly. Static assets are output to the directory for the Hosting site field `public` configured in `firebase.json`. Please read the docs carefully! From 003a8520ddf52d3d57a10e2a0cb21c80f457cac2 Mon Sep 17 00:00:00 2001 From: jthegedus Date: Thu, 6 May 2021 19:08:27 +1000 Subject: [PATCH 2/5] docs: cloudRunBuildDir is relative to firebaseJson loc --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3f6c7d9..bf9b4e7 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ Adapter options: - path to your `firebase.json`, relative from where `svelte build` is called - default: `./firebase.json` - `cloudRunBuildDir` - - output dir of Cloud Run service, relative from your svelte app + - output dir of Cloud Run service, relative from the `firebaseJson` location - default: `./.${run.serviceId}` where `run.serviceId` is pulled from the `firebase.json` rewrite rule ## Details From 50ca085c60a87d7fb0e659eac5872af906bc5412 Mon Sep 17 00:00:00 2001 From: jthegedus Date: Thu, 6 May 2021 19:15:33 +1000 Subject: [PATCH 3/5] fix: output firebase build relative to firebase.json --- src/index.js | 11 ++++++----- src/utils.js | 5 +++-- tests/index.test.js | 9 +++++---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index 0a205cc..f38d279 100644 --- a/src/index.js +++ b/src/index.js @@ -22,14 +22,14 @@ const entrypoint = function ({ const adapter = { name: 'svelte-adapter-firebase', async adapt(utils) { - const {functions, cloudRun, publicDir} = parseFirebaseConfiguration({hostingSite, sourceRewriteMatch, firebaseJson}); + const {firebaseJsonDir, functions, cloudRun, publicDir} = parseFirebaseConfiguration({hostingSite, sourceRewriteMatch, firebaseJson}); if (functions !== false) { await adaptToCloudFunctions({utils, ...functions}); } if (cloudRun !== false) { - await adaptToCloudRun({utils, ...cloudRun, cloudRunBuildDir}); + await adaptToCloudRun({utils, ...cloudRun, firebaseJsonDir, cloudRunBuildDir}); } utils.log.info(`Clearing dirs for new build: ${publicDir}`); @@ -101,11 +101,12 @@ exports.${name} = functions.https.onRequest(async (request, response) => { * utils: import('@sveltejs/kit').AdapterUtils, * serviceId: string; * region: string; + * firebaseJsonDir: string * cloudRunBuildDir: string|undefined * }} param */ -async function adaptToCloudRun({utils, serviceId, region, cloudRunBuildDir}) { - const serverOutputDir = path.join(cloudRunBuildDir || `.${serviceId}`); +async function adaptToCloudRun({utils, serviceId, region, firebaseJsonDir, cloudRunBuildDir}) { + const serverOutputDir = path.join(firebaseJsonDir, cloudRunBuildDir || `.${serviceId}`); await prepareEntrypoint({utils, serverOutputDir}); utils.log.info(`Writing Cloud Run service to ./${serverOutputDir}`); @@ -156,7 +157,7 @@ async function prepareEntrypoint({utils, serverOutputDir}) { const handlerSource = path.join(fileURLToPath(new URL('./files', import.meta.url)), 'handler.js'); const handlerDest = path.join(temporaryDir, 'handler.js'); - utils.log.info(`Copying adapter ${handlerSource} to ${handlerDest}`); + utils.log.info(`Copying adapter handler to ${handlerDest}`); utils.copy(handlerSource, handlerDest); utils.log.info('Compiling handler.js with SvelteKit server via esbuild'); diff --git a/src/utils.js b/src/utils.js index 8965175..085fc75 100644 --- a/src/utils.js +++ b/src/utils.js @@ -24,6 +24,7 @@ function isString(parameter) { * }} */ function parseFirebaseConfiguration({hostingSite, sourceRewriteMatch, firebaseJson}) { + firebaseJson = path.resolve(firebaseJson); // Should this be fileURLToPath for portability??? if (!existsSync(firebaseJson)) { throw new Error(`File ${firebaseJson} does not exist. The provided file should exist and be a Firebase JSON config.`); } @@ -99,13 +100,13 @@ function parseFirebaseConfiguration({hostingSite, sourceRewriteMatch, firebaseJs return { functions: rewriteConfig?.function ? { name: rewriteConfig.function, - source: firebaseConfig.functions.source + source: path.join(path.dirname(firebaseJson), firebaseConfig.functions.source) } : false, cloudRun: rewriteConfig?.run ? { serviceId: rewriteConfig.run.serviceId, region: rewriteConfig.run?.region || 'us-central1' } : false, - publicDir: hostingConfig.public + publicDir: path.join(path.dirname(firebaseJson), hostingConfig.public) }; } diff --git a/tests/index.test.js b/tests/index.test.js index b138b43..9afdb2b 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -1,5 +1,6 @@ import test from 'ava'; import {fileURLToPath} from 'url'; +import path from 'path'; import {parseFirebaseConfiguration, validCloudFunctionName, validCloudRunServiceId} from '../src/utils.js'; // ParseFirebaseConfiguration: Valid configs @@ -8,7 +9,7 @@ test( t => { const config = {hostingSite: undefined, sourceRewriteMatch: '**', firebaseJson: fileURLToPath(new URL('./fixtures/successes/cf_site.json', import.meta.url))}; const result = parseFirebaseConfiguration(config); - const expectedResult = {functions: {name: 'some_func', source: 'functions'}, cloudRun: false, publicDir: 'app'}; + const expectedResult = {functions: {name: 'some_func', source: path.join(path.dirname(config.firebaseJson), 'functions')}, cloudRun: false, publicDir: path.join(path.dirname(config.firebaseJson), 'app')}; t.deepEqual(result, expectedResult); } @@ -19,7 +20,7 @@ test( t => { const config = {hostingSite: 'app', sourceRewriteMatch: '**', firebaseJson: fileURLToPath(new URL('./fixtures/successes/cf_sites.json', import.meta.url))}; const result = parseFirebaseConfiguration(config); - const expectedResult = {functions: {name: 'some_func', source: 'functions'}, cloudRun: false, publicDir: 'app'}; + const expectedResult = {functions: {name: 'some_func', source: path.join(path.dirname(config.firebaseJson), 'functions')}, cloudRun: false, publicDir: path.join(path.dirname(config.firebaseJson), 'app')}; t.deepEqual(result, expectedResult); } @@ -30,7 +31,7 @@ test( t => { const config = {hostingSite: undefined, sourceRewriteMatch: '**', firebaseJson: fileURLToPath(new URL('./fixtures/successes/cr_site.json', import.meta.url))}; const result = parseFirebaseConfiguration(config); - const expectedResult = {functions: false, cloudRun: {serviceId: 'some-service', region: 'us-central1'}, publicDir: 'app'}; + const expectedResult = {functions: false, cloudRun: {serviceId: 'some-service', region: 'us-central1'}, publicDir: path.join(path.dirname(config.firebaseJson), 'app')}; t.deepEqual(result, expectedResult); } @@ -41,7 +42,7 @@ test( t => { const config = {hostingSite: 'app', sourceRewriteMatch: '**', firebaseJson: fileURLToPath(new URL('./fixtures/successes/cr_sites.json', import.meta.url))}; const result = parseFirebaseConfiguration(config); - const expectedResult = {functions: false, cloudRun: {serviceId: 'some-service', region: 'us-central1'}, publicDir: 'app'}; + const expectedResult = {functions: false, cloudRun: {serviceId: 'some-service', region: 'us-central1'}, publicDir: path.join(path.dirname(config.firebaseJson), 'app')}; t.deepEqual(result, expectedResult); } From b0a45a6fb6aec27f17cc3a0bca28a1e64764d9b6 Mon Sep 17 00:00:00 2001 From: jthegedus Date: Thu, 6 May 2021 19:21:27 +1000 Subject: [PATCH 4/5] chore: clean up logging --- src/index.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/index.js b/src/index.js index f38d279..690a3e3 100644 --- a/src/index.js +++ b/src/index.js @@ -32,7 +32,6 @@ const entrypoint = function ({ await adaptToCloudRun({utils, ...cloudRun, firebaseJsonDir, cloudRunBuildDir}); } - utils.log.info(`Clearing dirs for new build: ${publicDir}`); utils.rimraf(publicDir); utils.log.minor(`Prerendering static pages to: ${publicDir}`); @@ -150,17 +149,13 @@ async function prepareEntrypoint({utils, serverOutputDir}) { // TODO: SvelteKit may add utils.tmpdir() which would replace this hardcoded path const temporaryDir = path.join('.svelte-kit', 'firebase'); - utils.log.info(`Clearing dirs for new build: ${temporaryDir}`); utils.rimraf(temporaryDir); - utils.log.info(`Clearing dirs for new build: ${serverOutputDir}`); utils.rimraf(serverOutputDir); const handlerSource = path.join(fileURLToPath(new URL('./files', import.meta.url)), 'handler.js'); const handlerDest = path.join(temporaryDir, 'handler.js'); - utils.log.info(`Copying adapter handler to ${handlerDest}`); utils.copy(handlerSource, handlerDest); - utils.log.info('Compiling handler.js with SvelteKit server via esbuild'); await esbuild.build({ entryPoints: [path.join(temporaryDir, 'handler.js')], outfile: path.join(serverOutputDir, 'index.js'), From 037cfc33dd607c8b30cabe8995f5bf2b2422888f Mon Sep 17 00:00:00 2001 From: jthegedus Date: Fri, 7 May 2021 18:20:31 +1000 Subject: [PATCH 5/5] chore: rm comment --- src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 085fc75..cbe70fb 100644 --- a/src/utils.js +++ b/src/utils.js @@ -24,7 +24,7 @@ function isString(parameter) { * }} */ function parseFirebaseConfiguration({hostingSite, sourceRewriteMatch, firebaseJson}) { - firebaseJson = path.resolve(firebaseJson); // Should this be fileURLToPath for portability??? + firebaseJson = path.resolve(firebaseJson); if (!existsSync(firebaseJson)) { throw new Error(`File ${firebaseJson} does not exist. The provided file should exist and be a Firebase JSON config.`); }