Skip to content
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

feat(external_messaging): Adds external messaging resolver #961

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Setup pnpm
uses: pnpm/action-setup@v3
with:
version: "latest"
version: v9.0.6
run_install: false

- name: Get pnpm store directory
Expand Down
2 changes: 1 addition & 1 deletion cli/create-plasmo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-plasmo",
"version": "0.85.2",
"version": "0.86.0",
"description": "Create Plasmo Framework Browser Extension",
"main": "dist/index.js",
"bin": "bin/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion cli/plasmo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plasmo",
"version": "0.85.2",
"version": "0.86.0",
"description": "The Plasmo Framework CLI",
"publishConfig": {
"types": "dist/type.d.ts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const state = {
// TODO: cache these?
const createEntryCode = (
importSection: string,
switchCaseSection: string,
messageSection: string,
externalMessageSection: string,
portSection: string
) => `// @ts-nocheck
globalThis.__plasmoInternalPortMap = new Map()
Expand All @@ -30,7 +31,7 @@ ${importSection}

chrome.runtime.onMessageExternal.addListener((request, sender, sendResponse) => {
switch (request.name) {
${switchCaseSection}
${externalMessageSection}
default:
break
}
Expand All @@ -40,7 +41,7 @@ chrome.runtime.onMessageExternal.addListener((request, sender, sendResponse) =>

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.name) {
${switchCaseSection}
${messageSection}
default:
break
}
Expand All @@ -63,7 +64,7 @@ chrome.runtime.onConnect.addListener(function(port) {

const getHandlerList = async (
plasmoManifest: PlasmoManifest,
dirName: "messages" | "ports"
dirName: "messages" | "messages/external" | "ports"
) => {
const handlerDir = join(
plasmoManifest.projectPath.backgroundDirectory,
Expand Down Expand Up @@ -114,14 +115,18 @@ const getPortCode = (name: string, importName: string) => `case "${name}":

export const createBgswMessaging = async (plasmoManifest: PlasmoManifest) => {
try {
const [messageHandlerList, portHandlerList] = await Promise.all([
const handlerLists = await Promise.all([
getHandlerList(plasmoManifest, "messages"),
getHandlerList(plasmoManifest, "messages/external"),
getHandlerList(plasmoManifest, "ports")
])

vLog({ messageHandlerList, portHandlerList })
const [messageHandlerList, externalMessageHandlerList, portHandlerList] =
handlerLists

if (messageHandlerList.length === 0 && portHandlerList.length === 0) {
vLog({ messageHandlerList, externalMessageHandlerList, portHandlerList })

if (handlerLists.every((list) => list.length === 0)) {
return false
}

Expand All @@ -145,12 +150,15 @@ export const createBgswMessaging = async (plasmoManifest: PlasmoManifest) => {
state.md5Hash = declarationMd5Hash

const entryCode = createEntryCode(
[...messageHandlerList, ...portHandlerList]
[...messageHandlerList, ...externalMessageHandlerList, ...portHandlerList]
.map((code) => code.importCode)
.join("\n"),
messageHandlerList
.map((code) => getMessageCode(code.name, code.importName))
.join("\n"),
externalMessageHandlerList
.map((code) => getMessageCode(code.name, code.importName))
.join("\n"),
portHandlerList
.map((code) => getPortCode(code.name, code.importName))
.join("\n")
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@
"npm": ">=9.5.0",
"node": ">=18.0.0"
},
"packageManager": "pnpm@8.15.0"
"packageManager": "pnpm@9.0.6"
}
Loading