Skip to content

Commit

Permalink
fix: align adapter API with SvelteKit v1.0.0-next.54 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
jthegedus authored Mar 22, 2021
1 parent f910829 commit abe68f1
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 71 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
],
"version": "0.5.0",
"main": "dist/cli.js",
"type": "module",
"files": [
"dist"
],
Expand All @@ -37,12 +36,14 @@
"@rollup/plugin-node-resolve": "^11.2.0",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@sveltejs/kit": "^1.0.0-next.54",
"@types/node": "^14.14.35",
"ava": "^3.15.0",
"husky": "^5.0.8",
"rimraf": "^3.0.2",
"rollup": "^2.41.2",
"semantic-release": "^17.4.0",
"typescript": "^4.2.3",
"xo": "^0.38.2"
},
"scripts": {
Expand Down
88 changes: 88 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ const config = [
{
input: 'src/cli.js',
output: {
format: 'cjs',
exports: 'default',
file: 'dist/cli.js'
}
},
plugins: [nodeResolve(), commonjs()]
},
{
input: 'src/files/handler.js',
Expand Down
92 changes: 51 additions & 41 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import {readFileSync, renameSync, writeFileSync} from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';
import {copy} from '@sveltejs/app-utils/files'; // eslint-disable-line node/file-extension-in-import
import {copyFileIfExistsSync, parseFirebaseConfiguration} from './utils.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

function adapter() {
return {
/**
*
* @param {*} builder
* @param {{hostingSite: string|undefined,sourceRewriteMatch: string|undefined, firebaseJson: string|undefined, cloudRunBuildDir: string|undefined}|undefined} options
*/
async adapt(builder, options) {
const defaultOptions = {
firebaseJson: 'firebase.json',
hostingSite: undefined,
sourceRewriteMatch: '**',
cloudRunBuildDir: undefined
};
const config = {...defaultOptions, ...options};
const {functions, cloudRun, publicDir} = parseFirebaseConfiguration(config);
const {copyFileSync, readFileSync, renameSync, writeFileSync} = require('fs');
const path = require('path');
const {copyFileIfExistsSync, parseFirebaseConfiguration} = require('./utils.js');

/**
* @param {{
* hostingSite?: string;
* sourceRewriteMatch?: string,
* firebaseJson?: string,
* cloudRunBuildDir?: string
* }} options
*/
module.exports = function ({
firebaseJson = 'firebase.json',
hostingSite = undefined,
sourceRewriteMatch = '**',
cloudRunBuildDir = undefined
} = {}) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: 'svelte-adapter-firebase',
async adapt(builder) {
const {functions, cloudRun, publicDir} = parseFirebaseConfiguration({hostingSite, sourceRewriteMatch, firebaseJson});

if (functions !== false) {
adaptToCloudFunctions({builder, ...functions});
}

if (cloudRun !== false) {
adaptToCloudRun({builder, ...cloudRun, cloudRunBuildDir: config.cloudRunBuildDir});
adaptToCloudRun({builder, ...cloudRun, cloudRunBuildDir});
}

builder.log.info(`Writing client application to ${publicDir}`);
Expand All @@ -39,11 +38,17 @@ function adapter() {
await builder.prerender({dest: publicDir});
}
};
}

return adapter;
};

/**
*
* @param {{builder: any, name: string, source: string}} param0
* @param {{
* builder: any;
* name: string;
* source: string;
* }} param
*/
function adaptToCloudFunctions({builder, name, source}) {
const functionsPackageJson = JSON.parse(readFileSync(path.join(source, 'package.json'), 'utf-8'));
Expand All @@ -60,7 +65,9 @@ function adaptToCloudFunctions({builder, name, source}) {

// Prepare handler & entrypoint
renameSync(path.join(serverOutputDir, 'app.js'), path.join(serverOutputDir, 'app.mjs'));
copy(path.join(__dirname, 'files'), serverOutputDir);
copyFileSync(path.join(__dirname, 'files', 'index.js'), path.join(serverOutputDir, 'index.js'));
copyFileSync(path.join(__dirname, 'files', 'handler.mjs'), path.join(serverOutputDir, 'handler.mjs'));
copyFileSync(path.join(__dirname, 'files', 'handler.mjs.map'), path.join(serverOutputDir, 'handler.mjs.map'));

// Prepare Cloud Function
const functionsEntrypoint = path.join(source, functionsMain);
Expand All @@ -72,16 +79,14 @@ function adaptToCloudFunctions({builder, name, source}) {
`Add the following Cloud Function to ${functionsEntrypoint}
+--------------------------------------------------+
let ${ssrSvelteFunctionName};
exports.${name} = functions.https.onRequest(
async (request, response) => {
if (!${ssrSvelteFunctionName}) {
functions.logger.info("Initializing SvelteKit SSR Handler");
${ssrSvelteFunctionName} = require("./${ssrDirname}/index").default;
functions.logger.info("SvelteKit SSR Handler initialised!");
}
return await ${ssrSvelteFunctionName}(request, response);
exports.${name} = functions.https.onRequest(async (request, response) => {
if (!${ssrSvelteFunctionName}) {
functions.logger.info("Initializing SvelteKit SSR Handler");
${ssrSvelteFunctionName} = require("./${ssrDirname}/index").default;
functions.logger.info("SvelteKit SSR Handler initialised!");
}
);
return await ${ssrSvelteFunctionName}(request, response);
});
+--------------------------------------------------+`
);
}
Expand All @@ -92,7 +97,12 @@ exports.${name} = functions.https.onRequest(

/**
*
* @param {{builder: any, serviceId: string, region: string, cloudRunBuildDir: string|undefined}} param0
* @param {{
* builder: any;
* serviceId: string;
* region: string;
* cloudRunBuildDir: string|undefined
* }} param
*/
function adaptToCloudRun({builder, serviceId, region, cloudRunBuildDir}) {
const serverOutputDir = path.join(cloudRunBuildDir || `.${serviceId}`);
Expand All @@ -102,7 +112,9 @@ function adaptToCloudRun({builder, serviceId, region, cloudRunBuildDir}) {

// Prepare handler & entrypoint
renameSync(path.join(serverOutputDir, 'app.js'), path.join(serverOutputDir, 'app.mjs'));
copy(path.join(__dirname, 'files'), serverOutputDir);
copyFileSync(path.join(__dirname, 'files', 'index.js'), path.join(serverOutputDir, 'index.js'));
copyFileSync(path.join(__dirname, 'files', 'handler.mjs'), path.join(serverOutputDir, 'handler.mjs'));
copyFileSync(path.join(__dirname, 'files', 'handler.mjs.map'), path.join(serverOutputDir, 'handler.mjs.map'));

// Prepare Cloud Run package.json - read SvelteKit App 'package.json', modify the JSON, write to serverOutputDir
const pkgjson = JSON.parse(readFileSync('package.json', 'utf-8'));
Expand All @@ -127,5 +139,3 @@ gcloud beta run deploy ${serviceId} --platform managed --region ${region} --sour
+--------------------------------------------------+`
);
}

export default adapter;
Loading

0 comments on commit abe68f1

Please sign in to comment.