-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add non-standard cross-browser fields support
- Loading branch information
1 parent
b0391db
commit dc1bda6
Showing
2 changed files
with
91 additions
and
0 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
74 changes: 74 additions & 0 deletions
74
packages/manifest-compat-plugin/plugins/HandleBrowserSpecificFields.ts
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,74 @@ | ||
import {type Compiler, sources} from 'webpack' | ||
import {ManifestBase} from '../manifest-types' | ||
|
||
interface Options { | ||
manifestPath: string | ||
browser: string | ||
exclude?: string[] | ||
} | ||
|
||
export default class HandleBrowserSpecificFields { | ||
private readonly manifestPath: string | ||
private browser: string | ||
private chromiumBasedBrowsers = ['chrome', 'edge', 'opera', 'brave'] | ||
|
||
constructor(options: Options) { | ||
this.manifestPath = options.manifestPath | ||
this.browser = options.browser | ||
} | ||
|
||
private processManifest(manifest: any): any { | ||
const result: any = {} | ||
|
||
for (const key in manifest) { | ||
if (manifest.hasOwnProperty(key)) { | ||
if ( | ||
typeof manifest[key] === 'object' && | ||
!Array.isArray(manifest[key]) | ||
) { | ||
result[key] = this.processManifest(manifest[key]) | ||
} else if ( | ||
key.startsWith(`${this.browser}:`) || | ||
(key.startsWith('chromium:') && | ||
this.chromiumBasedBrowsers.includes(this.browser)) | ||
) { | ||
result[key.replace(`${this.browser}:`, '').replace('chromium:', '')] = | ||
manifest[key] | ||
} else if (!key.includes(':')) { | ||
result[key] = manifest[key] | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
apply(compiler: Compiler) { | ||
compiler.hooks.emit.tapAsync( | ||
'HandleBrowserSpecificFields', | ||
(compilation, done) => { | ||
if (compilation.errors.length > 0) return | ||
|
||
const manifestSource = compilation | ||
.getAsset('manifest.json') | ||
?.source.source() | ||
const manifest: ManifestBase = JSON.parse( | ||
(manifestSource || '').toString() | ||
) | ||
const overrides = this.processManifest(manifest) | ||
|
||
const patchedManifest: ManifestBase = { | ||
...manifest, | ||
...JSON.parse(overrides) | ||
} | ||
|
||
const source = JSON.stringify(patchedManifest, null, 2) | ||
const rawSource = new sources.RawSource(source) | ||
|
||
compilation.updateAsset('manifest.json', rawSource) | ||
|
||
done() | ||
} | ||
) | ||
} | ||
} |