Skip to content

Commit

Permalink
clean up slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
louisgv committed Jun 8, 2024
1 parent 082f22d commit f4bc50d
Show file tree
Hide file tree
Showing 4 changed files with 568 additions and 467 deletions.
13 changes: 6 additions & 7 deletions cli/plasmo/src/features/manifest-factory/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
import { assertTruthy } from "@plasmo/utils/assert"
import { injectEnv } from "@plasmo/utils/env"
import { isDirectory, isReadable } from "@plasmo/utils/fs"
import { vLog } from "@plasmo/utils/logging"
import { vLog, wLog } from "@plasmo/utils/logging"
import { getSubExt, toPosix } from "@plasmo/utils/path"

import { loadEnvConfig, type EnvConfig } from "~features/env/env-config"
Expand Down Expand Up @@ -225,13 +225,13 @@ export abstract class PlasmoManifest<T extends ExtensionManifest = any> {
})
)

if (!process.env.POST_BUILD_SCRIPT)
if (!process.env.POST_BUILD_SCRIPT) {
return

}
const postBuildPath = resolve(process.env.POST_BUILD_SCRIPT)

if (!existsSync(postBuildPath)) {
console.error("Post-build file does not exist or is not readable:", postBuildPath)
wLog("Post-build script is unavailable:", postBuildPath)
return
}

Expand Down Expand Up @@ -491,9 +491,8 @@ export abstract class PlasmoManifest<T extends ExtensionManifest = any> {

const parsedModulePath = parse(scriptPath)

const { wereFilesWritten } = await this.scaffolder.createPageMount(
parsedModulePath
)
const { wereFilesWritten } =
await this.scaffolder.createPageMount(parsedModulePath)

// if enabled, and the template file was written, invalidate hash!
if (wereFilesWritten) {
Expand Down
1 change: 1 addition & 0 deletions core/parcel-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"tsup": "7.2.0"
},
"dependencies": {
"@types/trusted-types": "2.0.7",
"@parcel/core": "2.9.3",
"@parcel/plugin": "2.9.3",
"react-refresh": "0.14.0"
Expand Down
59 changes: 37 additions & 22 deletions core/parcel-runtime/src/utils/loading-indicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,37 @@ const LOADING_ID = "__plasmo-loading__"

// Function to update the CSP to allow the new trusted type policy
function updateCSP() {
const cspMetaTag = document.querySelector('meta[http-equiv="Content-Security-Policy"]')

const cspMetaTag = document.querySelector(
'meta[http-equiv="Content-Security-Policy"]'
)

if (!cspMetaTag) {
return true
return
}
const currentCSP = cspMetaTag.getAttribute('content')

const currentCSP = cspMetaTag.getAttribute("content")
const newPolicy = ` trusted-html-${LOADING_ID}`

if (!currentCSP.includes(newPolicy)) {
const updatedCSP = currentCSP + newPolicy
cspMetaTag.setAttribute('content', updatedCSP)
if (currentCSP.includes(newPolicy)) {
return
}

return true
const updatedCSP = currentCSP + newPolicy
cspMetaTag.setAttribute("content", updatedCSP)
}

function createTrustedPolicy() {
const trustedTypes = globalThis.window.trustedTypes
if (!trustedTypes) {
return undefined
}
updateCSP()
return trustedTypes.createPolicy(`trusted-html-${LOADING_ID}`, {
createHTML: (str) => str
})
}

const trustedPolicy = typeof trustedTypes !== "undefined"
? updateCSP() && trustedTypes
.createPolicy(`trusted-html-${LOADING_ID}`, { createHTML: str => str })
: undefined
const trustedPolicy = createTrustedPolicy()

function getLoader() {
return document.getElementById(LOADING_ID)
Expand Down Expand Up @@ -96,7 +106,9 @@ function createLoader() {
<span class="hidden">Context Invalidated, Press to Reload</span>
`

loadingEl.innerHTML = trustedPolicy ? trustedPolicy.createHTML(htmlText) : htmlText
loadingEl.innerHTML = trustedPolicy
? (trustedPolicy.createHTML(htmlText) as any)
: htmlText

loadingEl.style.pointerEvents = "none"

Expand Down Expand Up @@ -153,16 +165,19 @@ export const createLoadingIndicator = () => {
await injectPromise
const loadingEl = getLoader()
loadingEl.style.opacity = "1"
if (reloadButton) {
loadingEl.onclick = (e) => {
e.stopPropagation()
globalThis.location.reload()
}
loadingEl.querySelector("span").classList.remove("hidden")

loadingEl.style.cursor = "pointer"
loadingEl.style.pointerEvents = "all"
if (!reloadButton) {
return
}

loadingEl.onclick = (e) => {
e.stopPropagation()
globalThis.location.reload()
}
loadingEl.querySelector("span").classList.remove("hidden")

loadingEl.style.cursor = "pointer"
loadingEl.style.pointerEvents = "all"
},
hide: async () => {
await injectPromise
Expand Down
Loading

0 comments on commit f4bc50d

Please sign in to comment.