Skip to content

Commit 683a6ca

Browse files
ktsnyyx990803
authored andcommitted
replace env vars during compile time
1 parent bb8ba9e commit 683a6ca

File tree

5 files changed

+158
-26
lines changed

5 files changed

+158
-26
lines changed

build/build.main.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 configs = require('./configs')
7+
8+
if (!fs.existsSync('dist')) {
9+
fs.mkdirSync('dist')
10+
}
11+
12+
build(Object.keys(configs).map(key => configs[key]))
13+
14+
function build (builds) {
15+
let built = 0
16+
const total = builds.length
17+
const next = () => {
18+
buildEntry(builds[built]).then(() => {
19+
built++
20+
if (built < total) {
21+
next()
22+
}
23+
}).catch(logError)
24+
}
25+
26+
next()
27+
}
28+
29+
function buildEntry (config) {
30+
const isProd = /min\.js$/.test(config.dest)
31+
return rollup.rollup(config).then(bundle => {
32+
const code = bundle.generate(config).code
33+
if (isProd) {
34+
var minified = (config.banner ? config.banner + '\n' : '') + uglify.minify(code, {
35+
fromString: true,
36+
output: {
37+
/* eslint-disable camelcase */
38+
screw_ie8: true,
39+
ascii_only: true
40+
/* eslint-enable camelcase */
41+
}
42+
}).code
43+
return write(config.dest, minified, true)
44+
} else {
45+
return write(config.dest, code)
46+
}
47+
})
48+
}
49+
50+
function write (dest, code, zip) {
51+
return new Promise((resolve, reject) => {
52+
function report (extra) {
53+
console.log(blue(path.relative(process.cwd(), dest)) + ' ' + getSize(code) + (extra || ''))
54+
resolve()
55+
}
56+
57+
fs.writeFile(dest, code, err => {
58+
if (err) return reject(err)
59+
if (zip) {
60+
zlib.gzip(code, (err, zipped) => {
61+
if (err) return reject(err)
62+
report(' (gzipped: ' + getSize(zipped) + ')')
63+
})
64+
} else {
65+
report()
66+
}
67+
})
68+
})
69+
}
70+
71+
function getSize (code) {
72+
return (code.length / 1024).toFixed(2) + 'kb'
73+
}
74+
75+
function logError (e) {
76+
console.log(e)
77+
}
78+
79+
function blue (str) {
80+
return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
81+
}

build/configs.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const path = require('path')
2+
const buble = require('rollup-plugin-buble')
3+
const replace = require('rollup-plugin-replace')
4+
const version = process.env.VERSION || require('../package.json').version
5+
const banner =
6+
`/**
7+
* vuex v${version}
8+
* (c) ${new Date().getFullYear()} Evan You
9+
* @license MIT
10+
*/`
11+
12+
const resolve = _path => path.resolve(__dirname, '../', _path)
13+
14+
const configs = {
15+
browserDev: {
16+
entry: resolve('src/index.js'),
17+
dest: resolve('dist/vuex.js'),
18+
format: 'umd',
19+
env: 'development'
20+
},
21+
browserProd: {
22+
entry: resolve('src/index.js'),
23+
dest: resolve('dist/vuex.min.js'),
24+
format: 'umd',
25+
env: 'production'
26+
},
27+
commonjs: {
28+
entry: resolve('src/index.js'),
29+
dest: resolve('dist/vuex.common.js'),
30+
format: 'cjs'
31+
},
32+
esm: {
33+
entry: resolve('src/index.esm.js'),
34+
dest: resolve('dist/vuex.esm.js'),
35+
format: 'es'
36+
}
37+
}
38+
39+
function genConfig (opts) {
40+
const config = {
41+
entry: opts.entry,
42+
dest: opts.dest,
43+
format: opts.format,
44+
banner,
45+
moduleName: 'Vuex',
46+
plugins: [
47+
replace({
48+
__VERSION__: version
49+
}),
50+
buble()
51+
]
52+
}
53+
54+
if (opts.env) {
55+
config.plugins.unshift(replace({
56+
'process.env.NODE_ENV': JSON.stringify(opts.env)
57+
}))
58+
}
59+
60+
return config
61+
}
62+
63+
function mapValues (obj, fn) {
64+
const res = {}
65+
Object.keys(obj).forEach(key => {
66+
res[key] = fn(obj[key], key)
67+
})
68+
return res
69+
}
70+
71+
module.exports = mapValues(configs, genConfig)

build/rollup.config.js

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

build/rollup.dev.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./configs').commonjs

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vuex",
33
"version": "2.3.1",
44
"description": "state management for Vue.js",
5-
"main": "dist/vuex.js",
5+
"main": "dist/vuex.common.js",
66
"module": "dist/vuex.esm.js",
77
"unpkg": "dist/vuex.js",
88
"typings": "types/index.d.ts",
@@ -15,14 +15,13 @@
1515
],
1616
"scripts": {
1717
"dev": "node examples/server.js",
18-
"dev:dist": "rollup -wm -c build/rollup.config.js",
19-
"build": "npm run build:main && npm run build:esm && npm run build:logger",
20-
"build:main": "rollup -c build/rollup.config.js && uglifyjs dist/vuex.js -cm --comments -o dist/vuex.min.js",
21-
"build:esm": "rollup -c build/rollup.config.js --environment ESM",
18+
"dev:dist": "rollup -wm -c build/rollup.dev.config.js",
19+
"build": "npm run build:main && npm run build:logger",
20+
"build:main": "node build/build.main.js",
2221
"build:logger": "rollup -c build/rollup.logger.config.js",
2322
"lint": "eslint src test",
2423
"test": "npm run lint && npm run test:types && npm run test:unit && npm run test:e2e",
25-
"test:unit": "rollup -c build/rollup.config.js && jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
24+
"test:unit": "rollup -c build/rollup.dev.config.js && jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
2625
"test:e2e": "node test/e2e/runner.js",
2726
"test:types": "tsc -p types/test",
2827
"release": "bash build/release.sh",

0 commit comments

Comments
 (0)