Skip to content

Commit

Permalink
fix: static output dir differes from static source dirs (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
jthegedus authored May 13, 2021
1 parent 3d7e1e0 commit 72c7670
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {readFileSync, writeFileSync} from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';
import {copyFileIfExistsSync, parseFirebaseConfiguration} from './utils.js';
import {copyFileIfExistsSync, ensureStaticResourceDirsDiffer, parseFirebaseConfiguration} from './utils.js';
import esbuild from 'esbuild';

/**
Expand All @@ -24,6 +24,11 @@ const entrypoint = function ({
async adapt(utils) {
const {firebaseJsonDir, functions, cloudRun, publicDir} = parseFirebaseConfiguration({hostingSite, sourceRewriteMatch, firebaseJson});

// Temporary solution until - https://github.com/sveltejs/kit/issues/1435 - is resolved
const svelteConfig = await import(path.join(process.cwd(), 'svelte.config.js'));
const svelteStaticDir = path.join(process.cwd(), svelteConfig?.kit?.files?.assets || 'static');
ensureStaticResourceDirsDiffer({source: svelteStaticDir, dest: publicDir});

if (functions !== false) {
await adaptToCloudFunctions({utils, ...functions});
}
Expand All @@ -32,6 +37,7 @@ const entrypoint = function ({
await adaptToCloudRun({utils, ...cloudRun, firebaseJsonDir, cloudRunBuildDir});
}

utils.log.warn(`Erasing ${publicDir} before processing static assets`);
utils.rimraf(publicDir);

utils.log.minor(`Prerendering static pages to: ${publicDir}`);
Expand Down
16 changes: 15 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,23 @@ function copyFileIfExistsSync(filename, destDir) {
}
}

/**
* Ensure provided static asset output dir (firebase.json:hosting.public) is not the same as the source dir
* @param {{
* dest:string
* source:string
* }} param source and destination directory for static assets
*/
function ensureStaticResourceDirsDiffer({source, dest}) {
if (source === dest) {
throw new Error('firebase.json:hosting.public must be a different directory to svelte.config.js:kit.files.assets');
}
}

export {
parseFirebaseConfiguration,
validCloudRunServiceId,
validCloudFunctionName,
copyFileIfExistsSync
copyFileIfExistsSync,
ensureStaticResourceDirsDiffer
};
16 changes: 15 additions & 1 deletion tests/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava';
import {fileURLToPath} from 'url';
import path from 'path';
import {parseFirebaseConfiguration, validCloudFunctionName, validCloudRunServiceId} from '../src/utils.js';
import {ensureStaticResourceDirsDiffer, parseFirebaseConfiguration, validCloudFunctionName, validCloudRunServiceId} from '../src/utils.js';

// ParseFirebaseConfiguration: Valid configs
test(
Expand Down Expand Up @@ -230,3 +230,17 @@ test('Cloud Function name with invalid length', t => {
const result = validCloudFunctionName('aCloudFunctionsFunctionNameThatIsSeventyFiveCharactersLongWhichIsMoreThan63');
t.is(result, false);
});

//
test('Static asset source and dest different dirs', t => {
const error = t.notThrows(() => ensureStaticResourceDirsDiffer({source: 'a', dest: 'b'}));
t.is(error, undefined);
});

test(
'Static asset source and dest the same dir',
t => {
const error = t.throws(() => ensureStaticResourceDirsDiffer({source: 'a', dest: 'a'}));
t.is(error.message, 'firebase.json:hosting.public must be a different directory to svelte.config.js:kit.files.assets');
}
);

0 comments on commit 72c7670

Please sign in to comment.