Skip to content
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

fix: treat firebase.json as root dir for Firebase resources #43

Merged
merged 5 commits into from
May 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand All @@ -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
Expand Down
14 changes: 5 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ 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}`);
utils.rimraf(publicDir);

utils.log.minor(`Prerendering static pages to: ${publicDir}`);
Expand Down Expand Up @@ -101,11 +100,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}`);
Expand Down Expand Up @@ -149,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 ${handlerSource} 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'),
Expand Down
5 changes: 3 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function isString(parameter) {
* }}
*/
function parseFirebaseConfiguration({hostingSite, sourceRewriteMatch, firebaseJson}) {
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.`);
}
Expand Down Expand Up @@ -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)
};
}

Expand Down
9 changes: 5 additions & 4 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down