-
Notifications
You must be signed in to change notification settings - Fork 78
/
manifest.ts
55 lines (48 loc) 路 1.46 KB
/
manifest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { readFileSync } from "fs";
import { flatMapDeep } from "lodash";
import { Entry, Output } from "webpack";
import {
bgScriptEntryErrorMsg,
bgScriptManifestRequiredMsg,
} from "../messages/errors";
export function extractEntries(
webpackEntry: Entry,
webpackOutput: Output = {},
manifestPath: string,
): IEntriesOption {
const manifestJson = JSON.parse(
readFileSync(manifestPath).toString(),
) as IExtensionManifest;
const { background, content_scripts } = manifestJson;
const { filename } = webpackOutput;
if (!filename) {
throw new Error();
}
if (!background?.scripts) {
throw new TypeError(bgScriptManifestRequiredMsg.get());
}
const bgScriptFileNames = background.scripts;
const toRemove = (filename as string).replace("[name]", "");
const bgWebpackEntry = Object.keys(webpackEntry).find(entryName =>
bgScriptFileNames.some(
bgManifest => bgManifest.replace(toRemove, "") === entryName,
),
);
if (!bgWebpackEntry) {
throw new TypeError(bgScriptEntryErrorMsg.get());
}
const contentEntries: unknown = content_scripts
? flatMapDeep(Object.keys(webpackEntry), entryName =>
content_scripts.map(({ js }) =>
js
.map(contentItem => contentItem.replace(toRemove, ""))
.filter(contentItem => contentItem === entryName),
),
)
: null;
return {
background: bgWebpackEntry,
contentScript: contentEntries as string[],
extensionPage: null,
};
}