Repository navigation
feat(vite): single-process HMR dev and debug cleartext for Android #6209
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
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f1724bc
feat(vite): run HMR sessions on vite serve alone when the package sup…
edusperoni 3b34307
feat(vite): allow cleartext HTTP to the dev server in Android debug H…
edusperoni 4fbe536
fix(vite): skip the dev cleartext change check for host projects
edusperoni 7997eea
fix(vite): skip the in-app refresh before restarting on Android HMR s…
edusperoni b21f069
fix(android): handle self-closing and single-quoted debug manifests
edusperoni 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
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 |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { IProjectData } from "../../definitions/project"; | ||
|
|
||
| /** | ||
| * Identifies a debug manifest whose cleartext permission the CLI wrote, so a | ||
| * later prepare that doesn't need it can take it out again. | ||
| */ | ||
| export const DEV_CLEARTEXT_MARKER = | ||
| "nativescript-cli: cleartext HTTP to the Vite dev server (debug HMR runs only)"; | ||
|
|
||
| const TOOLS_NS = 'xmlns:tools="http://schemas.android.com/tools"'; | ||
| const CLEARTEXT_ATTRIBUTE = "android:usesCleartextTraffic"; | ||
|
|
||
| /** | ||
| * A Vite HMR app loads its code from the dev server over plain HTTP, which | ||
| * Android blocks by default from API 28. | ||
| */ | ||
| export function needsDevCleartext( | ||
| projectData: Pick<IProjectData, "bundler">, | ||
| prepareData: { hmr?: boolean; release?: boolean }, | ||
| ): boolean { | ||
| return ( | ||
| projectData.bundler === "vite" && !!prepareData.hmr && !prepareData.release | ||
| ); | ||
| } | ||
|
|
||
| export function hasDevCleartext(manifest: string | null): boolean { | ||
| return !!manifest && manifest.includes(DEV_CLEARTEXT_MARKER); | ||
| } | ||
|
|
||
| /** The debug source set's manifest when the app ships none of its own. */ | ||
| export function devCleartextManifest(): string { | ||
| return [ | ||
| '<?xml version="1.0" encoding="utf-8"?>', | ||
| `<!-- ${DEV_CLEARTEXT_MARKER} -->`, | ||
| `<manifest xmlns:android="http://schemas.android.com/apk/res/android" ${TOOLS_NS}>`, | ||
| `\t<application ${CLEARTEXT_ATTRIBUTE}="true" tools:replace="${CLEARTEXT_ATTRIBUTE}" />`, | ||
| "</manifest>", | ||
| "", | ||
| ].join("\n"); | ||
| } | ||
|
|
||
| /** | ||
| * The app's own debug manifest with the permission added. `tools:replace` | ||
| * lets it win over a main manifest that sets the attribute itself. | ||
| */ | ||
| export function withDevCleartext(manifest: string): string { | ||
| let result = manifest; | ||
| const manifestTag = /<manifest\b[^>]*>/.exec(result); | ||
| if (!manifestTag) { | ||
| return result; | ||
| } | ||
| if (!manifestTag[0].includes("xmlns:tools=")) { | ||
| const withTools = manifestTag[0].replace( | ||
| /^<manifest\b/, | ||
| `<manifest ${TOOLS_NS}`, | ||
| ); | ||
| result = result.replace(manifestTag[0], withTools); | ||
| } | ||
|
|
||
| const applicationTag = /<application\b[^>]*?(\/?)>/.exec(result); | ||
| if (!applicationTag) { | ||
| const application = `\t<application ${CLEARTEXT_ATTRIBUTE}="true" tools:replace="${CLEARTEXT_ATTRIBUTE}" />`; | ||
| const root = /<manifest\b[^>]*>/.exec(result)[0]; | ||
| if (root.endsWith("/>")) { | ||
| result = result.replace( | ||
| root, | ||
| `${root.slice(0, -2).trimEnd()}>\n${application}\n</manifest>`, | ||
| ); | ||
| } else if (/<\/manifest>/.test(result)) { | ||
| result = result.replace(/<\/manifest>/, `${application}\n</manifest>`); | ||
| } else { | ||
| // Without the marker, the next prepare tries again. | ||
| return manifest; | ||
| } | ||
| } else { | ||
| let tag = applicationTag[0]; | ||
| const value = /android:usesCleartextTraffic\s*=\s*(?:"[^"]*"|'[^']*')/; | ||
| tag = value.test(tag) | ||
| ? tag.replace(value, `${CLEARTEXT_ATTRIBUTE}="true"`) | ||
| : tag.replace( | ||
| /^<application\b/, | ||
| `<application ${CLEARTEXT_ATTRIBUTE}="true"`, | ||
| ); | ||
| const replace = /tools:replace\s*=\s*(["'])(.*?)\1/.exec(tag); | ||
| if (!replace) { | ||
| tag = tag.replace( | ||
| /^<application\b/, | ||
| `<application tools:replace="${CLEARTEXT_ATTRIBUTE}"`, | ||
| ); | ||
| } else if ( | ||
| !replace[2] | ||
| .split(",") | ||
| .map((name) => name.trim()) | ||
| .includes(CLEARTEXT_ATTRIBUTE) | ||
| ) { | ||
| const [, quote, names] = replace; | ||
| tag = tag.replace( | ||
| replace[0], | ||
| `tools:replace=${quote}${names},${CLEARTEXT_ATTRIBUTE}${quote}`, | ||
| ); | ||
| } | ||
| result = result.replace(applicationTag[0], tag); | ||
| } | ||
|
|
||
| return result.replace( | ||
| /(<manifest\b)/, | ||
| `<!-- ${DEV_CLEARTEXT_MARKER} -->\n$1`, | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Preserve multiline
tools:replacevalues.If an existing value spans lines,
.*?does not match it. The fallback then inserts a secondtools:replaceattribute. XML permits newlines in attribute values, but prohibits duplicate attributes, so the prepared debug manifest becomes invalid. (w3.org)Use
[\s\S]*?instead of.*?. Keep the quote backreference. Add a regression case with a multiline replacement list.Based on learnings,
.without dotAll does not match newlines.Source: Learnings