-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathindex.ts
executable file
·115 lines (95 loc) · 3.28 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import htmlInputs from './html-inputs'
import manifestInput from './manifest-input'
import { browserPolyfill as b } from './browser-polyfill'
import { validateNames as v } from './validate-names'
import { readJSONSync } from 'fs-extra'
import { join } from 'path'
import { ChromeExtensionOptions, ChromeExtensionPlugin } from './plugin-options'
import { mixedFormat as m } from './mixed-format'
export { simpleReloader } from './plugin-reloader-simple'
export type { ManifestV2, ManifestV3 } from './manifest-types'
export const chromeExtension = (
options = {} as ChromeExtensionOptions,
): ChromeExtensionPlugin => {
/* --------------- LOAD PACKAGE.JSON --------------- */
try {
const packageJsonPath = join(process.cwd(), 'package.json')
options.pkg = options.pkg || readJSONSync(packageJsonPath)
// eslint-disable-next-line no-empty
} catch (error) {}
/* ----------------- SETUP PLUGINS ----------------- */
const manifest = manifestInput(options)
const html = htmlInputs(manifest)
const validate = v()
const browser = b(manifest)
const mixedFormat = m(manifest)
/* ----------------- RETURN PLUGIN ----------------- */
return {
name: 'chrome-extension',
// For testing
_plugins: { manifest, html, validate },
config: () => {
console.warn(
'Please run `npm i rollup-plugin-chrome-extension@beta` to use with Vite.',
)
throw new Error(
'[chrome-extension] Vite support is for RPCE v4 and above. This is RPCE v3.6.7.',
)
},
async options(options) {
try {
// return [manifest, html].reduce((opts, plugin) => {
// const result = plugin.options.call(this, opts)
// return result || options
// }, options)
let result = options
for (const plugin of [manifest, html]) {
const r = await plugin.options.call(this, result)
result = r ?? result
}
return result
} catch (error) {
const manifestError =
'The manifest must have at least one script or HTML file.'
const htmlError =
'At least one HTML file must have at least one script.'
if (
error instanceof Error &&
(error.message === manifestError || error.message === htmlError)
) {
throw new Error(
'A Chrome extension must have at least one script or HTML file.',
)
} else {
throw error
}
}
},
async buildStart(options) {
await Promise.all([
manifest.buildStart.call(this, options),
html.buildStart.call(this, options),
])
},
async resolveId(...args) {
return manifest.resolveId.call(this, ...args)
},
async load(id) {
return manifest.load.call(this, id)
},
transform(source, id) {
return manifest.transform.call(this, source, id)
},
watchChange(...args) {
manifest.watchChange.call(this, ...args)
html.watchChange.call(this, ...args)
},
async generateBundle(...args) {
await manifest.generateBundle.call(this, ...args)
await validate.generateBundle.call(this, ...args)
await browser.generateBundle.call(this, ...args)
// TODO: should skip this if not needed
await mixedFormat.generateBundle.call(this, ...args)
},
}
}