Skip to content

Commit

Permalink
refactor(runtime): use experiments.baseUrl for web exports instead …
Browse files Browse the repository at this point in the history
…of workaround
  • Loading branch information
byCedric committed Apr 26, 2024
1 parent ccce478 commit 52f492b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 72 deletions.
1 change: 1 addition & 0 deletions runtime/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.expo/
web-build/
dist/
16 changes: 16 additions & 0 deletions runtime/app.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { version: expoVersion } = require('expo/package.json');
const { major } = require('semver');

/**
* @param {object} params
* @param {import('expo/config').ExpoConfig} params.config
* @returns {import('expo/config').ExpoConfig}
*/
module.exports = ({ config }) => {
// Set web export prefix major Expo SDK version.
// This is used to properly host the web build on Snack's S3 hosting.
config.experiments ||= {};
config.experiments.baseUrl = `/v2/${major(expoVersion)}`;

return config;
};
20 changes: 0 additions & 20 deletions runtime/metro.config.js

This file was deleted.

53 changes: 1 addition & 52 deletions runtime/web/deploy-script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const spawnAsync = require('@expo/spawn-async');
const expoVersion = require('expo/package.json').version;
const fs = require('fs/promises');
const path = require('path');
const semver = require('semver');

Expand All @@ -13,8 +12,6 @@ async function run() {
const exportDir = './web-build';

await exportWeb({ workingDir, exportDir });
await patchBundleImportPath({ workingDir, exportDir });
await patchFaviconImportPath({ workingDir, exportDir });
await uploadWeb({ workingDir, exportDir });
}

Expand All @@ -32,61 +29,13 @@ async function exportWeb(options) {
{
cwd: options.workingDir,
stdio: process.env.CI ? 'inherit' : 'ignore',
env: {
...process.env,
SNACK_EXPORT_WEB: 'true',
},
},
);
console.log(
`✅ Exported the Snack Runtime to: ${path.join(options.workingDir, options.exportDir)}`,
);
}

/**
* Fix the bundle path import in the `dist/index.html` export.
* This is required because the Snack Runtime is hosted in S3 under subfolders.
* We need to convert `<script src="/_expo/static...">` to `<script src="_expo/static...">`.
*
* @param {object} options
* @param {string} options.workingDir
* @param {string} options.exportDir
*/
async function patchBundleImportPath(options) {
const indexPath = path.resolve(options.workingDir, options.exportDir, './index.html');
const indexFile = await fs.readFile(indexPath, 'utf8');
const patchedFile = indexFile.replace(`<script src="/_expo`, `<script src="_expo`);

if (patchedFile === indexFile) {
throw new Error('Could not patch the bundle import path in the index.html file');
}

await fs.writeFile(indexPath, patchedFile, 'utf8');
console.log(`✅ Patched bundle import path in: ${indexPath}`);
}

/**
* Fix the favicon import in the `dist/index.html` export.
* This is required because the Snack Runtime is hosted in S3 under subfolders.
* We need to convert `<link rel="shortcut icon" href="/favicon.ico" />` to `<link rel="shortcut icon" href="favicon.ico" />`.
*
* @param {object} options
* @param {string} options.workingDir
* @param {string} options.exportDir
*/
async function patchFaviconImportPath(options) {
const indexPath = path.resolve(options.workingDir, options.exportDir, './index.html');
const indexFile = await fs.readFile(indexPath, 'utf8');
const patchedFile = indexFile.replace(`href="/favicon.ico"`, `href="favicon.ico"`);

if (patchedFile === indexFile) {
throw new Error('Could not patch the favicon path in the index.html file');
}

await fs.writeFile(indexPath, patchedFile, 'utf8');
console.log(`✅ Patched favicon path in: ${indexPath}`);
}

/**
* Upload the exported web bundle to S3.
*
Expand Down Expand Up @@ -117,5 +66,5 @@ async function uploadWeb(options) {
},
);

console.log(`✅ Uploaded the Snack Runtime to S3: ${bucketName} (v2/${sdkVersion})`);
console.log(`✅ Uploaded the Snack Runtime to S3: ${bucketName} (/v2/${sdkVersion})`);
}
13 changes: 13 additions & 0 deletions runtime/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9159,6 +9159,14 @@ sucrase@3.34.0:
version "3.34.0"
resolved "https://registry.yarnpkg.com/sucrase/-/sucrase-3.34.0.tgz#1e0e2d8fcf07f8b9c3569067d92fbd8690fb576f"
integrity sha512-70/LQEZ07TEcxiU2dz51FKaE6hCTWC6vr7FOk3Gr0U60C3shtAN+H+BFr9XlYe5xqf3RA8nrc+VIwzCfnxuXJw==
dependencies:
"@jridgewell/gen-mapping" "^0.3.2"
commander "^4.0.0"
glob "7.1.6"
lines-and-columns "^1.1.6"
mz "^2.7.0"
pirates "^4.0.1"
ts-interface-checker "^0.1.9"

sudo-prompt@9.1.1:
version "9.1.1"
Expand Down Expand Up @@ -9412,6 +9420,11 @@ ts-api-utils@^1.0.1:
resolved "https://registry.yarnpkg.com/ts-api-utils/-/ts-api-utils-1.0.3.tgz#f12c1c781d04427313dbac808f453f050e54a331"
integrity sha512-wNMeqtMz5NtwpT/UZGY5alT+VoKdSsOOP/kqHFcUW1P/VRhH2wJ48+DN2WwUliNbQ976ETwDL0Ifd2VVvgonvg==

ts-interface-checker@^0.1.9:
version "0.1.13"
resolved "https://registry.yarnpkg.com/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz#784fd3d679722bc103b1b4b8030bcddb5db2a699"
integrity sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==

tsconfig-paths@^3.15.0:
version "3.15.0"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.15.0.tgz#5299ec605e55b1abb23ec939ef15edaf483070d4"
Expand Down

0 comments on commit 52f492b

Please sign in to comment.