This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
240 lines (228 loc) · 6.86 KB
/
rollup.config.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// rollup.config.js
import { defineConfig } from 'rollup'
import userScriptHeader from 'rollup-plugin-tampermonkey-header'
import path from 'path'
import fs from 'fs'
import {resolve} from "@babel/core/lib/vendor/import-meta-resolve";
const headerDev = {
basic: {
"@name": "EWT Killer Box Beta",
"@version": "r2332",
},
extra: {
"@description": "Beta software.Only test.",
}
}
const headerBuild = {
basic: {
"@name": "E网通辅助工具",
"@version": "3.0",
},
extra: {
"@description": "一个专门用于EWT的辅助插件.",
}
}
const userDefinedOptions = {
metaPath: path.resolve(__dirname, 'src', 'meta.json')
}
const isObject = (arg) => {
return Object.prototype.toString.call(arg) === '[object Object]'
}
const mergeRollupConfigs = function (object, ...sources) {
sources.forEach((source) => {
if (!isObject(source)) {
return
}
Object.entries(source).forEach(([name, value]) => {
if (name in object) {
if (value === null || value === undefined) {
return
}
const objectValue = object[name]
if (Array.isArray(objectValue)) {
if (name === 'plugins') {
const pluginNames = objectValue.map((plugin) => plugin.name)
value.forEach((plugin) => {
const index = pluginNames.indexOf(plugin.name)
if (index > -1) {
Object.assign(objectValue[index], plugin)
} else {
objectValue.push(plugin)
}
})
} else {
object[name] = Array.from(
new Set([
...objectValue,
...(typeof value === 'object' ? Object.values(value) : [value])
])
)
}
} else if (isObject(objectValue)) {
Object.assign(object[name], value)
} else {
object[name] = value
}
} else {
object[name] = value
}
})
})
return object
}
const commonConfigs = require('./rollup_configs/default.js')
const rollupConfigsPath = require('path').join(__dirname, 'rollup_configs')
try {
const files = fs.readdirSync(rollupConfigsPath)
files.forEach(function (file) {
if (file === 'default.js') return
const configs = require('./rollup_configs/' + file).default
mergeRollupConfigs(commonConfigs, configs)
})
} catch (err) {
console.log(err)
}
function devConfigs() {
let userScriptHeaderContent = []
const outputFileName = 'main.dev.user'
const outputFile = `${outputFileName}.js`
return defineConfig({
input: { [outputFileName]: 'src/main.ts' },
output: {
dir: 'dist',
format: 'iife',
sourcemap: 'inline',
globals: {
"jquery": "$"
}
},
watch: {
exclude: 'dist'
},
plugins: [
...commonConfigs.plugins,
userScriptHeader({
metaPath: userDefinedOptions.metaPath,
transformHeaderContent(items) {
const newItems = items
.filter(([name]) => !['@supportURL', '@updateURL', '@downloadURL'].includes(name))
.map(([name, value]) => {
if(name in headerDev.basic) {
return [name,headerDev.basic[name]]
} else {
return [name,value]
}
})
let gm_info = []
for(let i of newItems) {
if(i[0] == "@grant")
gm_info.push(["@grant",i[1].replace("_",".")]) //支持UserScript
}
for(let i of gm_info)
newItems.push(i)
for(let i in headerDev.extra) {
newItems.push([i,headerDev.extra[i]])
}
userScriptHeaderContent = [...newItems]
return newItems
},
outputFile
}),
devEntryPlugin(outputFile)
],
external: [
"jquery"
]
})
function devEntryPlugin(outputFileName) {
let headerPluginApi
let devFileContentCache = ''
return {
name: 'generate-dev-entry',
buildStart(options) {
const { plugins } = options
const pluginName = 'tampermonkey-header'
const headerPlugin = plugins.find((plugin) => plugin.name === 'tampermonkey-header')
if (!headerPlugin) {
// or handle this silently if it is optional
throw new Error(`This plugin depends on the "${pluginName}" plugin.`)
}
// now you can access the API methods in subsequent hooks
headerPluginApi = headerPlugin.api
},
generateBundle(options) {
const { dir } = options
const filePath = path.resolve(__dirname, dir, outputFileName)
userScriptHeaderContent.push(['@require', filePath])
const devFileName = 'dev.user.js'
const devFilePath = path.resolve(__dirname, dir, devFileName)
const devFileContent =
headerPluginApi?.generateUserScriptHeader(userScriptHeaderContent) ?? ''
if (devFileContentCache !== devFileContent) {
this.emitFile({
type: 'asset',
fileName: devFileName,
source: devFileContent
})
if (!devFileContentCache) {
console.log(
'\n✅Dev plugin is created. Please paste the path to browser and install in Tampermonkey: \n\x1b[1m\x1b[4m\x1b[36m%s\x1b[0m\n',
devFilePath
)
} else {
console.log(
'\n🔥Dev plugin need re-install. Please paste the path to browser and reinstall in Tampermonkey: \n\x1b[1m\x1b[4m\x1b[36m%s\x1b[0m\n',
devFilePath
)
}
devFileContentCache = devFileContent
}
}
}
}
}
function prodConfigs() {
const outputFile = 'main.user.js'
return defineConfig({
input: 'src/main.ts',
output: {
file: outputFile,
format: 'iife',
globals: {
"jquery": "$"
}
},
plugins: [
...commonConfigs.plugins,
userScriptHeader({
metaPath: userDefinedOptions.metaPath,
transformHeaderContent(items) {
console.log(items)
const newItems = items
.filter(([name]) => !['@supportURL', '@updateURL', '@downloadURL'].includes(name))
.map(([name, value]) => {
if(name in headerBuild.basic) {
return [name,headerBuild.basic[name]]
} else {
return [name,value]
}
})
let gm_info = []
for(let i of newItems) {
if(i[0] == "@grant")
gm_info.push(["@grant",i[1].replace("_",".")]) //支持UserScript
}
for(let i of gm_info)
newItems.push(i)
for(let i in headerBuild.extra) {
newItems.push([i,headerBuild.extra[i]])
}
return newItems
},
outputFile
})
]
})
}
const isDev = process.env.BUILD === 'development'
export default isDev ? devConfigs() : prodConfigs()