Skip to content

Commit 2a95601

Browse files
committed
update build script
1 parent 6931f06 commit 2a95601

File tree

3 files changed

+136
-25
lines changed

3 files changed

+136
-25
lines changed

build/build.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const zlib = require('zlib')
4+
const uglify = require('uglify-js')
5+
const rollup = require('rollup')
6+
const buble = require('rollup-plugin-buble')
7+
const flow = require('rollup-plugin-flow-no-whitespace')
8+
const cjs = require('rollup-plugin-commonjs')
9+
const node = require('rollup-plugin-node-resolve')
10+
const replace = require('rollup-plugin-replace')
11+
const version = process.env.VERSION || require('../package.json').version
12+
const banner =
13+
`/**
14+
* vue-router v${version}
15+
* (c) ${new Date().getFullYear()} Evan You
16+
* @license MIT
17+
*/`
18+
19+
if (!fs.existsSync('dist')) {
20+
fs.mkdirSync('dist')
21+
}
22+
23+
const resolve = _path => path.resolve(__dirname, '../', _path)
24+
25+
build([
26+
// browser dev
27+
{
28+
dest: resolve('dist/vue-router.js'),
29+
format: 'umd',
30+
env: 'development'
31+
},
32+
{
33+
dest: resolve('dist/vue-router.min.js'),
34+
format: 'umd',
35+
env: 'production'
36+
},
37+
{
38+
dest: resolve('dist/vue-router.common.js'),
39+
format: 'cjs'
40+
}
41+
].map(genConfig))
42+
43+
function build (builds) {
44+
let built = 0
45+
const total = builds.length
46+
const next = () => {
47+
buildEntry(builds[built]).then(() => {
48+
built++
49+
if (built < total) {
50+
next()
51+
}
52+
}).catch(logError)
53+
}
54+
55+
next()
56+
}
57+
58+
function genConfig (opts) {
59+
const config = {
60+
entry: resolve('src/index.js'),
61+
dest: opts.dest,
62+
format: opts.format,
63+
banner,
64+
moduleName: 'VueRouter',
65+
plugins: [
66+
flow(),
67+
node(),
68+
cjs(),
69+
buble()
70+
]
71+
}
72+
73+
if (opts.env) {
74+
config.plugins.unshift(replace({
75+
'process.env.NODE_ENV': JSON.stringify(opts.env)
76+
}))
77+
}
78+
79+
return config
80+
}
81+
82+
function buildEntry (config) {
83+
const isProd = /min\.js$/.test(config.dest)
84+
return rollup.rollup(config).then(bundle => {
85+
const code = bundle.generate(config).code
86+
if (isProd) {
87+
var minified = (config.banner ? config.banner + '\n' : '') + uglify.minify(code, {
88+
fromString: true,
89+
output: {
90+
screw_ie8: true,
91+
ascii_only: true
92+
},
93+
compress: {
94+
pure_funcs: ['makeMap']
95+
}
96+
}).code
97+
return write(config.dest, minified, true)
98+
} else {
99+
return write(config.dest, code)
100+
}
101+
})
102+
}
103+
104+
function write (dest, code, zip) {
105+
return new Promise((resolve, reject) => {
106+
function report (extra) {
107+
console.log(blue(path.relative(process.cwd(), dest)) + ' ' + getSize(code) + (extra || ''))
108+
resolve()
109+
}
110+
111+
fs.writeFile(dest, code, err => {
112+
if (err) return reject(err)
113+
if (zip) {
114+
zlib.gzip(code, (err, zipped) => {
115+
if (err) return reject(err)
116+
report(' (gzipped: ' + getSize(zipped) + ')')
117+
})
118+
} else {
119+
report()
120+
}
121+
})
122+
})
123+
}
124+
125+
function getSize (code) {
126+
return (code.length / 1024).toFixed(2) + 'kb'
127+
}
128+
129+
function logError (e) {
130+
console.log(e)
131+
}
132+
133+
function blue (str) {
134+
return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
135+
}

build/rollup.config.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"scripts": {
2323
"dev": "node examples/server.js",
2424
"dev:dist": "rollup -wm -c build/rollup.config.js",
25-
"build": "rollup --environment NODE_ENV:production -c build/rollup.config.js && uglifyjs dist/vue-router.js -cm --comments -o dist/vue-router.min.js",
25+
"build": "node build/build.js",
2626
"lint": "eslint src examples",
2727
"test": "npm run lint && flow check && npm run test:unit && npm run test:e2e && npm run test:types",
2828
"test:unit": "jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",

0 commit comments

Comments
 (0)