-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.js
156 lines (137 loc) · 3.52 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
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
const fs = require('fs')
const path = require('path')
const rollup = require('rollup')
const rm = require('rimraf').sync
const babel = require('rollup-plugin-babel')
const cmd = require('rollup-plugin-commonjs')
const replace = require('rollup-plugin-replace')
const cleanup = require('rollup-plugin-cleanup')
const { terser } = require('rollup-plugin-terser')
const resolve = require('rollup-plugin-node-resolve')
const libName = 'mpstore'
const version = require('./package.json').version
const testLibPath = path.resolve(__dirname, './demo')
const devStoreDir = path.resolve(testLibPath, './store')
const entryPath = path.resolve(__dirname, './src/index.js')
const outputPath = filename => path.resolve(__dirname, './dist', filename)
const banner =
'/*!\n' +
` * Mpstore.js v${version}\n` +
` * (c) 2019-${new Date().getFullYear()} Imtaotao\n` +
' * Released under the MIT License.\n' +
' */'
const esm = {
input: entryPath,
output: {
banner,
format: 'es',
file: outputPath(`${libName}.esm.js`),
},
}
const es6m = {
input: entryPath,
output: {
banner,
format: 'es',
file: outputPath(`${libName}.es6m.js`),
},
}
const cjs = {
input: entryPath,
output: {
banner,
format: 'cjs',
file: outputPath(`${libName}.common.js`),
},
}
const uglifyCjs = {
input: entryPath,
output: {
banner,
format: 'cjs',
file: outputPath(`${libName}.min.js`),
},
}
// create env variable
const createReplacePlugin = () => {
return replace({
__VERSION__: `'${version}'`,
})
}
async function build (cfg, needUglify, sourcemap = false, needBabel = true) {
cfg.output.sourcemap = sourcemap
const buildCfg = {
input: cfg.input,
plugins: [
cleanup(),
resolve(),
cmd(),
createReplacePlugin(),
]
}
if (needBabel) {
buildCfg.plugins.splice(
2, 0,
babel({
babelrc: false,
exclude: 'node_modules/**',
presets: [['@babel/preset-env', { modules: false }]],
}),
)
}
if (needUglify) {
buildCfg.plugins.unshift(
terser({
sourcemap: false,
})
)
}
const bundle = await rollup.rollup(buildCfg)
await bundle.generate(cfg.output)
await bundle.write(cfg.output)
}
console.clear()
// delete old build files
rm('./dist')
rm(devStoreDir)
const transferfile = (from, desPath) => {
const readable = fs.createReadStream(from)
readable.on('open', () => readable.pipe(fs.createWriteStream(desPath)))
}
const buildVersion = sourcemap => {
const builds = [
build(esm, false, sourcemap),
build(cjs, false, sourcemap),
// build es6 version
build(es6m, false, sourcemap, false)
]
if (!sourcemap) {
builds.push(build(uglifyCjs, true, sourcemap))
}
Promise.all(builds).then(() => {
// transfer esm package to dev folder
if (fs.existsSync(testLibPath)) {
if (!fs.existsSync(devStoreDir)) {
fs.mkdirSync(devStoreDir)
}
const devStorePath = esm.output.file
const desPath = path.join(devStoreDir, `${libName}.esm.js`)
transferfile(devStorePath, desPath)
if (sourcemap) {
transferfile(devStorePath + '.map', desPath + '.map')
}
}
})
}
// watch, use in dev and test
if (process.argv.includes('-w')) {
let i = 0
fs.watch('./src', () => {
console.clear()
console.log('Rebuild: ' + ++i)
buildVersion(true)
})
buildVersion(true)
} else {
buildVersion()
}