From 7d80451eb33292a2bd87dc24ad062f350049f0b1 Mon Sep 17 00:00:00 2001 From: ktsn Date: Sat, 17 Dec 2016 14:49:18 +0900 Subject: [PATCH 1/2] add build script for umd, minified build --- build/build.js | 126 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 13 +++-- test/test-babel.js | 2 +- test/test.ts | 2 +- tsconfig.json | 2 +- 5 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 build/build.js diff --git a/build/build.js b/build/build.js new file mode 100644 index 0000000..69fd8d4 --- /dev/null +++ b/build/build.js @@ -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' +} \ No newline at end of file diff --git a/package.json b/package.json index 70255e3..7646d7f 100644 --- a/package.json +++ b/package.json @@ -2,14 +2,16 @@ "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", @@ -45,8 +47,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" } diff --git a/test/test-babel.js b/test/test-babel.js index 68bd7d8..04984c9 100644 --- a/test/test-babel.js +++ b/test/test-babel.js @@ -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' diff --git a/test/test.ts b/test/test.ts index 00e2e8c..0a6d80a 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1,4 +1,4 @@ -import Component, { createDecorator } from '../lib/index' +import Component, { createDecorator } from '../' import { expect } from 'chai' import * as Vue from 'vue' diff --git a/tsconfig.json b/tsconfig.json index bfcdbb9..1e57bea 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ "dom", "es2015" ], - "module": "commonjs", + "module": "es2015", "moduleResolution": "node", "outDir": "lib", "isolatedModules": false, From 29e858f68cd27faee75b3901e46b1ce6ffb3fb78 Mon Sep 17 00:00:00 2001 From: ktsn Date: Sat, 17 Dec 2016 15:03:40 +0900 Subject: [PATCH 2/2] add release script --- build/release.sh | 23 +++++++++++++++++++++++ package.json | 3 +-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100755 build/release.sh diff --git a/build/release.sh b/build/release.sh new file mode 100755 index 0000000..46f2690 --- /dev/null +++ b/build/release.sh @@ -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 \ No newline at end of file diff --git a/package.json b/package.json index 7646d7f..579dff6 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,7 @@ "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",