Skip to content

Commit 1bf9fcc

Browse files
authoredDec 17, 2016
Add build script for UMD and minified UMD builds (#47)
* add build script for umd, minified build * add release script
1 parent 543a2da commit 1bf9fcc

File tree

6 files changed

+162
-9
lines changed

6 files changed

+162
-9
lines changed
 

‎build/build.js

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

‎build/release.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -e
3+
read -p "Enter release version: " VERSION
4+
5+
read -p "Releasing $VERSION - are you sure? (y/N) " -n 1 -r
6+
echo # (optional) move to a new line
7+
if [[ $REPLY =~ ^[Yy]$ ]]
8+
then
9+
echo "Releasing $VERSION ..."
10+
npm test
11+
npm run clean
12+
VERSION=$VERSION npm run build
13+
14+
# commit
15+
git add -A
16+
git commit -m "[build] $VERSION"
17+
npm version $VERSION --message "[release] $VERSION"
18+
19+
# publish
20+
git push origin refs/tags/v$VERSION
21+
git push
22+
npm publish
23+
fi

‎package.json

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
"name": "vue-class-component",
33
"version": "4.3.1",
44
"description": "ES201X/TypeScript class decorator for Vue components",
5-
"main": "lib/index.js",
5+
"main": "dist/vue-class-component.common.js",
66
"typings": "lib/index.d.ts",
77
"files": [
8-
"lib",
9-
"tsconfig.json"
8+
"dist",
9+
"lib"
1010
],
1111
"scripts": {
12-
"build": "tsc -p .",
12+
"build": "npm run build:ts && npm run build:main",
13+
"build:ts": "tsc -p .",
14+
"build:main": "node build/build.js",
1315
"clean": "rimraf ./lib",
1416
"example": "npm run build && webpack --config example/webpack.config.js",
1517
"dev": "webpack --config example/webpack.config.js --watch",
16-
"test": "npm run build && webpack --config test/webpack.config.js && mocha test/test.build.js",
17-
"prepublish": "npm run clean && npm run build"
18+
"test": "npm run build && webpack --config test/webpack.config.js && mocha test/test.build.js"
1819
},
1920
"repository": {
2021
"type": "git",
@@ -45,8 +46,11 @@
4546
"mocha": "^3.1.2",
4647
"node-libs-browser": "^1.0.0",
4748
"rimraf": "^2.5.4",
49+
"rollup": "^0.37.0",
50+
"rollup-plugin-replace": "^1.1.1",
4851
"ts-loader": "^0.9.5",
4952
"typescript": "^2.0.6",
53+
"uglify-js": "^2.7.5",
5054
"vue": "^2.1.6",
5155
"webpack": "^2.1.0-beta.27"
5256
}

‎test/test-babel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Component, { createDecorator } from '../lib/index'
1+
import Component, { createDecorator } from '../'
22
import chai, { expect } from 'chai'
33
import spies from 'chai-spies'
44
import Vue from 'vue'

‎test/test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Component, { createDecorator } from '../lib/index'
1+
import Component, { createDecorator } from '../'
22
import { expect } from 'chai'
33
import * as Vue from 'vue'
44

‎tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"dom",
66
"es2015"
77
],
8-
"module": "commonjs",
8+
"module": "es2015",
99
"moduleResolution": "node",
1010
"outDir": "lib",
1111
"isolatedModules": false,

0 commit comments

Comments
 (0)
Please sign in to comment.