Skip to content

Add build script for UMD and minified UMD builds #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions build/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const fs = require('fs')
const path = require('path')
const zlib = require('zlib')
const uglify = require('uglify-js')
const rollup = require('rollup')
const replace = require('rollup-plugin-replace')
const version = process.env.VERSION || require('../package.json').version
const banner =
`/**
* vue-class-component v${version}
* (c) 2015-${new Date().getFullYear()} Evan You
* @license MIT
*/`

if (!fs.existsSync('dist')) {
fs.mkdirSync('dist')
}

const resolve = _path => path.resolve(__dirname, '../', _path)

build([
{
dest: resolve('dist/vue-class-component.js'),
format: 'umd',
env: 'development'
},
{
dest: resolve('dist/vue-class-component.min.js'),
format: 'umd',
env: 'production'
},
{
dest: resolve('dist/vue-class-component.common.js'),
format: 'cjs'
}
].map(genConfig))

function build (builds) {
let built = 0
const total = builds.length
const next = () => {
buildEntry(builds[built]).then(() => {
built++
if (built < total) {
next()
}
}).catch(logError)
}

next()
}

function genConfig (opts) {
const config = {
entry: resolve('lib/index.js'),
dest: opts.dest,
format: opts.format,
banner,
moduleName: 'VueClassComponent',
exports: 'named',
external: ['vue'],
globals: {
vue: 'Vue'
},
plugins: []
}

if (opts.env) {
config.plugins.unshift(replace({
'process.env.NODE_ENV': JSON.stringify(opts.env)
}))
}

return config
}

function buildEntry (config) {
const isProd = /min\.js$/.test(config.dest)
return rollup.rollup(config).then(bundle => {
const code = bundle.generate(config).code
if (isProd) {
var minified = (config.banner ? config.banner + '\n' : '') + uglify.minify(code, {
fromString: true,
output: {
screw_ie8: true
}
}).code
return write(config.dest, minified, true)
} else {
return write(config.dest, code)
}
})
}

function write (dest, code, zip) {
return new Promise((resolve, reject) => {
function report (extra) {
console.log(blue(path.relative(process.cwd(), dest)) + ' ' + getSize(code) + (extra || ''))
resolve()
}

fs.writeFile(dest, code, err => {
if (err) return reject(err)
if (zip) {
zlib.gzip(code, (err, zipped) => {
if (err) return reject(err)
report(' (gzipped: ' + getSize(zipped) + ')')
})
} else {
report()
}
})
})
}

function getSize (code) {
return (code.length / 1024).toFixed(2) + 'kb'
}

function logError (e) {
console.log(e)
}

function blue (str) {
return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
}
23 changes: 23 additions & 0 deletions build/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
set -e
read -p "Enter release version: " VERSION

read -p "Releasing $VERSION - are you sure? (y/N) " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Releasing $VERSION ..."
npm test
npm run clean
VERSION=$VERSION npm run build

# commit
git add -A
git commit -m "[build] $VERSION"
npm version $VERSION --message "[release] $VERSION"

# publish
git push origin refs/tags/v$VERSION
git push
npm publish
fi
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
"name": "vue-class-component",
"version": "4.3.1",
"description": "ES201X/TypeScript class decorator for Vue components",
"main": "lib/index.js",
"main": "dist/vue-class-component.common.js",
"typings": "lib/index.d.ts",
"files": [
"lib",
"tsconfig.json"
"dist",
"lib"
],
"scripts": {
"build": "tsc -p .",
"build": "npm run build:ts && npm run build:main",
"build:ts": "tsc -p .",
"build:main": "node build/build.js",
"clean": "rimraf ./lib",
"example": "npm run build && webpack --config example/webpack.config.js",
"dev": "webpack --config example/webpack.config.js --watch",
"test": "npm run build && webpack --config test/webpack.config.js && mocha test/test.build.js",
"prepublish": "npm run clean && npm run build"
"test": "npm run build && webpack --config test/webpack.config.js && mocha test/test.build.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -45,8 +46,11 @@
"mocha": "^3.1.2",
"node-libs-browser": "^1.0.0",
"rimraf": "^2.5.4",
"rollup": "^0.37.0",
"rollup-plugin-replace": "^1.1.1",
"ts-loader": "^0.9.5",
"typescript": "^2.0.6",
"uglify-js": "^2.7.5",
"vue": "^2.1.6",
"webpack": "^2.1.0-beta.27"
}
Expand Down
2 changes: 1 addition & 1 deletion test/test-babel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Component, { createDecorator } from '../lib/index'
import Component, { createDecorator } from '../'
import chai, { expect } from 'chai'
import spies from 'chai-spies'
import Vue from 'vue'
Expand Down
2 changes: 1 addition & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Component, { createDecorator } from '../lib/index'
import Component, { createDecorator } from '../'
import { expect } from 'chai'
import * as Vue from 'vue'

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"dom",
"es2015"
],
"module": "commonjs",
"module": "es2015",
"moduleResolution": "node",
"outDir": "lib",
"isolatedModules": false,
Expand Down