Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Web Extension manifest v3 (#7050)
- Loading branch information
Showing
29 changed files
with
943 additions
and
387 deletions.
There are no files selected for viewing
This file contains 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 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
3 changes: 3 additions & 0 deletions
3
packages/core/integration-tests/test/integration/webextension-mv3/.parcelrc
This file contains 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,3 @@ | ||
{ | ||
"extends": ["@parcel/config-webextension"] | ||
} |
Empty file.
1 change: 1 addition & 0 deletions
1
packages/core/integration-tests/test/integration/webextension-mv3/content-script.js
This file contains 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 @@ | ||
alert('File test alert'); |
3 changes: 3 additions & 0 deletions
3
packages/core/integration-tests/test/integration/webextension-mv3/injected.css
This file contains 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,3 @@ | ||
h1 { | ||
font-family: "Comic Sans MS"; | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/core/integration-tests/test/integration/webextension-mv3/manifest.json
This file contains 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,20 @@ | ||
{ | ||
"name": "MV3 Migration - content script example", | ||
"description": "Source: https://github.com/GoogleChrome/chrome-extensions-samples", | ||
"version": "0.1", | ||
"manifest_version": 3, | ||
"background": { | ||
"service_worker": "background.js" | ||
}, | ||
"permissions": [ | ||
"scripting", | ||
"activeTab" | ||
], | ||
"content_scripts": [{ | ||
"matches": ["https://*.google.com/*"], | ||
"js": ["other-content-script.js"] | ||
}], | ||
"action": { | ||
"default_popup": "popup.html" | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/core/integration-tests/test/integration/webextension-mv3/other-content-script.js
This file contains 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 @@ | ||
import './injected.css'; |
19 changes: 19 additions & 0 deletions
19
packages/core/integration-tests/test/integration/webextension-mv3/popup.css
This file contains 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,19 @@ | ||
* { | ||
box-sizing: border-box; | ||
} | ||
html, | ||
body, | ||
main { | ||
height: 100%; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
body { | ||
min-width: 20em; | ||
min-height: 10em; | ||
} | ||
main { | ||
padding: 1em .5em; | ||
display: grid; | ||
place-items: center; | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/core/integration-tests/test/integration/webextension-mv3/popup.html
This file contains 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,21 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
<link rel="stylesheet" href="popup.css"> | ||
<script src="popup.js" type="module"></script> | ||
</head> | ||
<body> | ||
<main> | ||
<div> | ||
<button id="inject-file">Inject file</button> | ||
</div> | ||
<div> | ||
<button id="inject-function">Inject function</button> | ||
</div> | ||
</main> | ||
</body> | ||
</html> |
33 changes: 33 additions & 0 deletions
33
packages/core/integration-tests/test/integration/webextension-mv3/popup.js
This file contains 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,33 @@ | ||
import contentScript from 'url:./content-script.js'; | ||
let injectFile = document.getElementById('inject-file'); | ||
let injectFunction = document.getElementById('inject-function'); | ||
|
||
async function getCurrentTab() { | ||
let queryOptions = { active: true, currentWindow: true }; | ||
let [tab] = await chrome.tabs.query(queryOptions); | ||
return tab; | ||
} | ||
|
||
injectFile.addEventListener('click', async () => { | ||
let tab = await getCurrentTab(); | ||
|
||
chrome.scripting.executeScript({ | ||
target: {tabId: tab.id}, | ||
files: [contentScript] | ||
}); | ||
}); | ||
|
||
function showAlert(givenName) { | ||
alert(`Hello, ${givenName}`); | ||
} | ||
|
||
injectFunction.addEventListener('click', async () => { | ||
let tab = await getCurrentTab(); | ||
|
||
let name = 'World'; | ||
chrome.scripting.executeScript({ | ||
target: {tabId: tab.id}, | ||
func: showAlert, | ||
args: [name] | ||
}); | ||
}); |
Empty file.
1 change: 1 addition & 0 deletions
1
packages/core/integration-tests/test/integration/webextension/_locales/.unwanted-file
This file contains 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 @@ | ||
This is to cover files like .DS_Store |
This file contains 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 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 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,27 @@ | ||
{ | ||
"name": "@parcel/packager-webextension", | ||
"version": "2.4.1", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"funding": { | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/parcel" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/parcel-bundler/parcel.git" | ||
}, | ||
"main": "lib/WebExtensionPackager.js", | ||
"source": "src/WebExtensionPackager.js", | ||
"engines": { | ||
"node": ">=12.0.0", | ||
"parcel": "^2.4.0" | ||
}, | ||
"dependencies": { | ||
"@parcel/plugin": "2.4.1", | ||
"@parcel/utils": "2.4.1", | ||
"nullthrows": "^1.1.1" | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
packages/packagers/webextension/src/WebExtensionPackager.js
This file contains 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,76 @@ | ||
// @flow strict-local | ||
|
||
import assert from 'assert'; | ||
import nullthrows from 'nullthrows'; | ||
import {Packager} from '@parcel/plugin'; | ||
import {replaceURLReferences, relativeBundlePath} from '@parcel/utils'; | ||
|
||
export default (new Packager({ | ||
async package({bundle, bundleGraph}) { | ||
let assets = []; | ||
bundle.traverseAssets(asset => { | ||
assets.push(asset); | ||
}); | ||
|
||
assert.equal( | ||
assets.length, | ||
1, | ||
'Web extension manifest bundles must only contain one asset', | ||
); | ||
const asset = assets[0]; | ||
assert(asset.meta.webextEntry === true); | ||
|
||
const relPath = b => | ||
relativeBundlePath(bundle, b, {leadingDotSlash: false}); | ||
|
||
const manifest = JSON.parse(await asset.getCode()); | ||
const deps = asset.getDependencies(); | ||
const war = []; | ||
for (const contentScript of manifest.content_scripts || []) { | ||
const jsBundles = deps | ||
.filter(d => contentScript.js?.includes(d.id)) | ||
.map(d => nullthrows(bundleGraph.getReferencedBundle(d, bundle))); | ||
|
||
contentScript.css = [ | ||
...new Set( | ||
(contentScript.css || []).concat( | ||
jsBundles | ||
.flatMap(b => bundleGraph.getReferencedBundles(b)) | ||
.filter(b => b.type == 'css') | ||
.map(relPath), | ||
), | ||
), | ||
]; | ||
|
||
war.push({ | ||
matches: contentScript.matches, | ||
extension_ids: [], | ||
resources: jsBundles | ||
.flatMap(b => { | ||
const children = []; | ||
const siblings = bundleGraph.getReferencedBundles(b); | ||
bundleGraph.traverseBundles(child => { | ||
if (b !== child && !siblings.includes(child)) { | ||
children.push(child); | ||
} | ||
}, b); | ||
return children; | ||
}) | ||
.map(relPath), | ||
}); | ||
} | ||
manifest.web_accessible_resources = ( | ||
manifest.web_accessible_resources || [] | ||
).concat( | ||
manifest.manifest_version == 2 | ||
? [...new Set(war.flatMap(entry => entry.resources))] | ||
: war, | ||
); | ||
let {contents} = replaceURLReferences({ | ||
bundle, | ||
bundleGraph, | ||
contents: JSON.stringify(manifest), | ||
}); | ||
return {contents}; | ||
}, | ||
}): Packager); |
Oops, something went wrong.