This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.js
130 lines (124 loc) · 3.75 KB
/
build.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
const rollup = require('rollup')
const fs = require('fs-extra')
const path = require('path')
const { info, success } = require('./lib/log')
const { getBabelOutputPlugin } = require('@rollup/plugin-babel')
const json = require('@rollup/plugin-json')
const { doSomething } = require('./lib/doSomething')
const { uglify } = require('rollup-plugin-uglify')
const minCSS = require('./lib/minCss')
const version = fs.readJSONSync('package.json').version
const disableWebsites = []
function loadWebsites () {
const websiteDir = 'src/scripts/website/'
info('Remove old file:', websiteDir + 'main.js')
fs.removeSync(path.join(websiteDir, 'main.js'))
info('Generate new file:', websiteDir + 'main.js')
let template = fs.readFileSync(path.join(websiteDir, 'main.template'), 'utf-8')
const websiteDirFiles = fs.readdirSync(websiteDir)
const websites = []
let importLine = ''
for (const e of websiteDirFiles) {
if (/\.js$/.test(e)) {
const functionName = e.replace(/\.js$/, '')
if (!disableWebsites.includes(functionName)) {
websites.push(functionName)
importLine += 'import { ' + functionName + ' } from \'./' + functionName + '\'\n'
}
}
}
template = template.replace('__IMPORT__', importLine).replace('__WEBSITE__', websites.join(', '))
fs.writeFileSync(path.join(websiteDir, 'main.js'), template)
}
async function buildUserJs () {
info('Empty directory: public')
fs.emptyDirSync('./public')
success('Empty directory: public')
await loadWebsites()
info('Package file to:', 'auto-task.user.js')
const bundle = await rollup.rollup({
input: 'src/scripts/main.js',
plugins: [
json({ namedExports: false })
]
})
await bundle.write({
file: 'auto-task.user.js',
format: 'cjs',
plugins: [
doSomething({
addContainer: true
}),
getBabelOutputPlugin({
presets: ['@babel/preset-env']
}),
doSomething({
addHeader: true,
replace: {
__TEST__: '',
__VERSION__: version,
__FILENAME__: 'auto-task.user.js',
__SITEURL__: 'auto-task.hclonely.com'
}
})
]
})
success('Write file to: auto-task.user.js')
await generateHelper()
await minCSS()
success('Build user js completed!')
info('Format file: auto-task.user.js')
}
async function buildI18nJs () {
info('Generate file: from "src/requirejs/i18n.js" to "src/requirejs/i18n.min.js"')
const bundle = await rollup.rollup({
input: 'src/requirejs/i18n.js',
plugins: [
json({ namedExports: false })
]
})
await bundle.write({
file: 'src/requirejs/i18n.min.js',
format: 'cjs',
plugins: [
getBabelOutputPlugin({
presets: ['@babel/preset-env']
}),
uglify()
]
})
success('Generate i18n.min.js completed!')
}
async function buildPageJs (file) {
const bundle = await rollup.rollup({
input: 'src/page/js/' + file,
plugins: [
json({ namedExports: false })
]
})
await bundle.write({
file: './public/js/' + file,
format: 'cjs',
plugins: [
getBabelOutputPlugin({
presets: ['@babel/preset-env']
}),
doSomething({
addHeader: true,
replace: {
__FILENAME__: 'auto-task.user.js'
}
}),
uglify()
]
})
}
function generateHelper () {
info('Generate file: auto-task-helper.user.js')
const text = fs.readFileSync('./src/helper/auto-task-helper.user.js').toString().replace(/__VERSION__/g, version).replace(/__SITEURL__/g, 'auto-task.hclonely.com').replace(/__FILENAME__/g, 'auto-task-helper.user.js')
fs.writeFileSync('./auto-task-helper.user.js', text)
success('Write file to: auto-task-helper.user.js')
}
exports.buildUserJs = buildUserJs
exports.buildPageJs = buildPageJs
exports.buildI18nJs = buildI18nJs