-
Notifications
You must be signed in to change notification settings - Fork 401
Serve client on 127.0.0.1:5400/client/index.js when running npm run dev
#3302
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
Open
ashfame
wants to merge
4
commits into
trunk
Choose a base branch
from
devex_improvement_using_dynamic_client
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a2ce582
serve client on `127.0.0.1:5400/client/index.js` when running `npm ru…
ashfame 294951f
be a bit better in setting Content-Type header
ashfame 4ee4eae
Harden serve-client-library Vite plugin per PR review feedback
ashfame 79e8c2b
Use path.relative() for cross-platform path traversal check
ashfame File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,8 +18,15 @@ import { | |||||||||||||||||||||||
| // eslint-disable-next-line @nx/enforce-module-boundaries | ||||||||||||||||||||||||
| import { oAuthMiddleware } from './vite.oauth'; | ||||||||||||||||||||||||
| import { fileURLToPath } from 'node:url'; | ||||||||||||||||||||||||
| import { copyFileSync, existsSync } from 'node:fs'; | ||||||||||||||||||||||||
| import { join } from 'node:path'; | ||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||
| copyFileSync, | ||||||||||||||||||||||||
| existsSync, | ||||||||||||||||||||||||
| readFileSync, | ||||||||||||||||||||||||
| readdirSync, | ||||||||||||||||||||||||
| statSync, | ||||||||||||||||||||||||
| } from 'node:fs'; | ||||||||||||||||||||||||
| import { join, resolve, relative, isAbsolute } from 'node:path'; | ||||||||||||||||||||||||
| import { exec } from 'node:child_process'; | ||||||||||||||||||||||||
| // eslint-disable-next-line @nx/enforce-module-boundaries | ||||||||||||||||||||||||
| import { buildVersionPlugin } from '../../vite-extensions/vite-build-version'; | ||||||||||||||||||||||||
| // eslint-disable-next-line @nx/enforce-module-boundaries | ||||||||||||||||||||||||
|
|
@@ -123,6 +130,144 @@ export default defineConfig(({ command, mode }) => { | |||||||||||||||||||||||
| server.middlewares.use(oAuthMiddleware); | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| // Serve the built @wp-playground/client library at /client/ | ||||||||||||||||||||||||
| // to match production where playground.wordpress.net/client/index.js | ||||||||||||||||||||||||
| // is available. Auto-builds if missing, warns if stale. | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| name: 'serve-client-library', | ||||||||||||||||||||||||
| configureServer(server: ViteDevServer) { | ||||||||||||||||||||||||
| const repoRoot = join(__dirname, '../../../'); | ||||||||||||||||||||||||
| const clientDistDir = join( | ||||||||||||||||||||||||
| repoRoot, | ||||||||||||||||||||||||
| 'dist/packages/playground/client' | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| const clientSrcDir = join(__dirname, '../client/src'); | ||||||||||||||||||||||||
| let buildInProgress = false; | ||||||||||||||||||||||||
| let stalenessWarned = false; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| function newestMtimeIn(dir: string): number { | ||||||||||||||||||||||||
| let newest = 0; | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| for (const entry of readdirSync(dir, { | ||||||||||||||||||||||||
| withFileTypes: true, | ||||||||||||||||||||||||
| })) { | ||||||||||||||||||||||||
| const full = join(dir, entry.name); | ||||||||||||||||||||||||
| if (entry.isDirectory()) { | ||||||||||||||||||||||||
| newest = Math.max( | ||||||||||||||||||||||||
| newest, | ||||||||||||||||||||||||
| newestMtimeIn(full) | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| } else if (entry.isFile()) { | ||||||||||||||||||||||||
| newest = Math.max( | ||||||||||||||||||||||||
| newest, | ||||||||||||||||||||||||
| statSync(full).mtimeMs | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||
| // Directory may not exist yet | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| return newest; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| function triggerClientBuild() { | ||||||||||||||||||||||||
| if (buildInProgress) { | ||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| buildInProgress = true; | ||||||||||||||||||||||||
| server.config.logger.warn( | ||||||||||||||||||||||||
| '\n Building @wp-playground/client… Refresh when done.\n' | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| exec( | ||||||||||||||||||||||||
| 'npx nx build playground-client', | ||||||||||||||||||||||||
| { cwd: repoRoot }, | ||||||||||||||||||||||||
| (error, stdout, stderr) => { | ||||||||||||||||||||||||
| buildInProgress = false; | ||||||||||||||||||||||||
| stalenessWarned = false; | ||||||||||||||||||||||||
| if (error) { | ||||||||||||||||||||||||
| server.config.logger.error( | ||||||||||||||||||||||||
| ' @wp-playground/client build failed. ' + | ||||||||||||||||||||||||
| 'Run manually: npx nx build playground-client\n' | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| if (stderr) { | ||||||||||||||||||||||||
| server.config.logger.error(stderr); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| server.config.logger.info( | ||||||||||||||||||||||||
| ' @wp-playground/client built. Refresh to load.\n' | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
ashfame marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| server.middlewares.use((req, res, next) => { | ||||||||||||||||||||||||
| if (!req.url?.startsWith('/client/')) { | ||||||||||||||||||||||||
| return next(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const distIndexPath = join(clientDistDir, 'index.js'); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (!existsSync(distIndexPath)) { | ||||||||||||||||||||||||
| triggerClientBuild(); | ||||||||||||||||||||||||
| res.setHeader('Access-Control-Allow-Origin', '*'); | ||||||||||||||||||||||||
| res.setHeader( | ||||||||||||||||||||||||
| 'Content-Type', | ||||||||||||||||||||||||
| 'application/javascript' | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| res.statusCode = 503; | ||||||||||||||||||||||||
| res.end( | ||||||||||||||||||||||||
| 'throw new Error(' + | ||||||||||||||||||||||||
| '"@wp-playground/client is not built yet. ' + | ||||||||||||||||||||||||
| 'A build was triggered automatically — refresh in a few seconds.\\n' + | ||||||||||||||||||||||||
| 'Or build manually: npx nx build playground-client"' + | ||||||||||||||||||||||||
| ');' | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (!stalenessWarned && !buildInProgress) { | ||||||||||||||||||||||||
| const distMtime = statSync(distIndexPath).mtimeMs; | ||||||||||||||||||||||||
| const srcMtime = newestMtimeIn(clientSrcDir); | ||||||||||||||||||||||||
| if (srcMtime > distMtime) { | ||||||||||||||||||||||||
| stalenessWarned = true; | ||||||||||||||||||||||||
| triggerClientBuild(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
ashfame marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const urlPath = new URL(req.url, 'http://localhost') | ||||||||||||||||||||||||
| .pathname; | ||||||||||||||||||||||||
|
Comment on lines
+238
to
+239
|
||||||||||||||||||||||||
| const urlPath = new URL(req.url, 'http://localhost') | |
| .pathname; | |
| let urlPath: string; | |
| try { | |
| urlPath = new URL(req.url, 'http://localhost') | |
| .pathname; | |
| } catch { | |
| res.statusCode = 400; | |
| res.end('Invalid request URL'); | |
| return; | |
| } |
ashfame marked this conversation as resolved.
Show resolved
Hide resolved
ashfame marked this conversation as resolved.
Show resolved
Hide resolved
ashfame marked this conversation as resolved.
Show resolved
Hide resolved
ashfame marked this conversation as resolved.
Show resolved
Hide resolved
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function name
newestMtimeInuses abbreviation 'mtime' which may not be immediately clear to all developers. Consider renaming togetNewestModificationTimeInorgetLatestFileModificationTimefor better clarity.