Skip to content

Commit 1ce2126

Browse files
Support building for additional remote origins (#122)
## Motivation for the change, related issues Since we are running a Playground instance for A8C, we want to be able to build `@wp-playground/client` to support that remote origin. This PR: - Supports specifying additional supported remote Playground origins during build - Updates the A8C package-for-self-hosting workflow to build `@wp-playground/client` to support the origin `https://wordpress-playground.atomicsites.blog` ## Implementation details This PR: - Adds an `additional-remote-origins.ts` module with a default empty origin list. - Adds a build-time to the Vite config to replace this module with a custom list of origins specified via the ADDITIONAL_REMOTE_ORIGINS environment variable. - Defines the ADDITIONAL_REMOTE_ORIGINS environment variable in the package-for-self-hosting workflow. ## Testing Instructions (or ideally a Blueprint) - CI - Test locally - Run `ADDITIONAL_REMOTE_ORIGINS=https://test-this-pr npx nx build playground-client` - Grep `dist/packages/playground/client` for "https://test-this-pr,http://another-remote-origin" - Note that the origins are added to the built script as separate strings in an array - Run `ADDITIONAL_REMOTE_ORIGINS=this-origin-is-invalid nx build playground-client` - Note that there is a build error due to invalid origin
1 parent c3c4672 commit 1ce2126

File tree

7 files changed

+67
-3
lines changed

7 files changed

+67
-3
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@ jobs:
138138
with:
139139
submodules: true
140140
- uses: ./.github/actions/prepare-playground
141-
- run: npx nx affected --target=build --parallel=3 --verbose
141+
- run: |
142+
if ADDITIONAL_REMOTE_ORIGINS=invalid-origin npx nx build playground-client; then
143+
echo "The Playground client build should have failed due to an invalid additional origin."
144+
echo "The build does not appear to be applying ADDITIONAL_REMOTE_ORIGINS."
145+
exit 1
146+
fi
147+
npx nx affected --target=build --parallel=3 --verbose
142148
143149
# Deploy documentation job
144150
deploy_docs:

.github/workflows/publish-self-hosted-package-release.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ jobs:
6767
git fetch --depth 1 origin tag "$LATEST_PACKAGE_VERSION_TAG" --no-tags
6868
git checkout "$LATEST_PACKAGE_VERSION_TAG"
6969
- name: Build JS packages for self-hosting
70+
env:
71+
ADDITIONAL_REMOTE_ORIGINS: 'https://wordpress-playground.atomicsites.blog'
7072
shell: bash
7173
run: npx nx run-many --all --target=package-for-self-hosting -- --hostingBaseUrl="$HOSTING_BASE_URL"
7274
- name: Build source tarball
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This is a placeholder during development
2+
// and may be replaced during build by a vite plugin.
3+
export const additionalRemoteOrigins = [];

packages/playground/client/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { consumeAPI } from '@php-wasm/web';
3131
import { ProgressTracker } from '@php-wasm/progress';
3232
import type { MountDescriptor, PlaygroundClient } from '@wp-playground/remote';
3333
import { collectPhpLogs, logger } from '@php-wasm/logger';
34+
import { additionalRemoteOrigins } from './additional-remote-origins';
3435

3536
export interface StartPlaygroundOptions {
3637
iframe: HTMLIFrameElement;
@@ -196,6 +197,7 @@ const validRemoteOrigins = [
196197
'https://localhost',
197198
'http://127.0.0.1',
198199
'https://127.0.0.1',
200+
...additionalRemoteOrigins,
199201
];
200202
/**
201203
* Assert that the remote origin is likely compatible with this client library.

packages/playground/client/tsconfig.lib.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,12 @@
1010
"../blueprints/src/lib/steps/activate-plugin.ts",
1111
"../blueprints/src/lib/compile.ts"
1212
],
13-
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
13+
"exclude": [
14+
"src/**/*.spec.ts",
15+
"src/**/*.test.ts",
16+
// Avoid generating types for this file during the build.
17+
// The module is not included in bundled build output,
18+
// so its type definition should be excluded as well.
19+
"src/additional-remote-origins.ts"
20+
]
1421
}

packages/playground/client/vite.config.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ import { viteTsConfigPaths } from '../../vite-extensions/vite-ts-config-paths';
99
// eslint-disable-next-line @nx/enforce-module-boundaries
1010
import { buildVersionPlugin } from '../../vite-extensions/vite-build-version';
1111

12+
function validateOrigin(origin: string) {
13+
try {
14+
const url = new URL(origin);
15+
if (url.href === `${origin}/`) {
16+
return true;
17+
}
18+
} catch {
19+
// Let exceptions fall through to the error below
20+
}
21+
22+
throw new Error(`Invalid origin: '${origin}'`);
23+
}
24+
25+
const additionalRemoteOriginsModulePath = join(
26+
__dirname,
27+
'src/additional-remote-origins.ts'
28+
);
29+
1230
export default defineConfig({
1331
cacheDir: '../../../node_modules/.vite/playground-client',
1432
plugins: [
@@ -28,6 +46,32 @@ export default defineConfig({
2846
// involve the remote-config virtual module, the bundler still needs to know
2947
// what to do when it sees `import from "virtual:remote-config"`.
3048
buildVersionPlugin('remote-config'),
49+
50+
// This plugin allows us to add additional remote origins during build.
51+
// We could use a virtual module instead, but if we do, we'll need to
52+
// add it to the vite config of every package that imports it, in order
53+
// to load those packages in the vite dev server.
54+
// By using this build transform, we only need to update this vite config.
55+
{
56+
name: 'replace-additional-remote-origins-during-build',
57+
transform(code: string, id: string) {
58+
if (id !== additionalRemoteOriginsModulePath) {
59+
return code;
60+
}
61+
if (!process.env['ADDITIONAL_REMOTE_ORIGINS']) {
62+
return code;
63+
}
64+
65+
const additionalRemoteOrigins = process.env[
66+
'ADDITIONAL_REMOTE_ORIGINS'
67+
]
68+
.split(',')
69+
.filter(validateOrigin);
70+
return `export const additionalRemoteOrigins = ${JSON.stringify(
71+
additionalRemoteOrigins
72+
)};`;
73+
},
74+
},
3175
],
3276

3377
// Configuration for building your library.

packages/vite-extensions/vite-list-assets-required-for-offline-mode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const patternsToNotCache = [
1212
'/README.md',
1313
'/.DS_Store',
1414
'/index.cjs',
15-
'/index.d.ts',
15+
/\/.*\.d\.ts$/, // No type declarations are needed at runtime.
1616
/\/lib\/.*/, // Remote lib files
1717
/\/test-fixtures\/.*/, // Test fixtures
1818
'/index.js',

0 commit comments

Comments
 (0)