Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Commit

Permalink
feat(bundle): more compact the vue-i18n distribution file
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Dec 9, 2015
1 parent 41097ea commit 2f32ecc
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 39 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ When used in CommonJS, you must explicitly install the router via Vue.use():

```javascript
var Vue = require('vue')
var i18n = require('vue-validator')
var VueI18n = require('vue-i18n')

Vue.use(i18n, { ... })
Vue.use(VueI18n, { ... })
```

# Usage
Expand Down
9 changes: 5 additions & 4 deletions config/banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ var pack = require('../package.json')
var version = process.env.VERSION || pack.version

module.exports =
pack.name + ' v' + version + '\n' +
'(c) ' + new Date().getFullYear() +
' ' + pack.author.name + '\n' +
'Released under the ' + pack.license + ' License.'
'/*!\n' +
' * ' + pack.name + ' v' + version + '\n' +
' * (c) ' + new Date().getFullYear() + ' ' + pack.author.name + '\n' +
' * Released under the ' + pack.license + ' License.\n' +
' */'
122 changes: 122 additions & 0 deletions config/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
var fs = require('fs')
var zlib = require('zlib')
var rollup = require('rollup')
var uglify = require('uglify-js')
var babel = require('rollup-plugin-babel')
var replace = require('rollup-plugin-replace')
var pack = require('../package.json')
var banner = require('./banner')

// update main file
var main = fs
.readFileSync('src/index.js', 'utf-8')
.replace(/plugin\.version = '[\d\.]+'/, "plugin.version = '" + pack.version + "'")
fs.writeFileSync('src/index.js', main)

// CommonJS build.
// this is used as the "main" field in package.json
// and used by bundlers like Webpack and Browserify.
rollup.rollup({
entry: 'src/index.js',
plugins: [
babel({
presets: ['es2015-rollup']
})
]
})
.then(function (bundle) {
return write('dist/' + pack.name + '.common.js', bundle.generate({
format: 'cjs',
banner: banner
}).code)
})
// Standalone Dev Build
.then(function () {
return rollup.rollup({
entry: 'src/index.js',
plugins: [
replace({
'process.env.NODE_ENV': "'development'"
}),
babel({
presets: ['es2015-rollup']
})
]
})
.then(function (bundle) {
return write('dist/' + pack.name + '.js', bundle.generate({
format: 'umd',
banner: banner,
moduleName: classify(pack.name)
}).code)
})
})
.then(function () {
// Standalone Production Build
return rollup.rollup({
entry: 'src/index.js',
plugins: [
replace({
'process.env.NODE_ENV': "'production'"
}),
babel({
presets: ['es2015-rollup']
})
]
})
.then(function (bundle) {
var code = bundle.generate({
format: 'umd',
moduleName: classify(pack.name)
}).code
var minified = banner + '\n' + uglify.minify(code, {
fromString: true
}).code
return write('dist/' + pack.name + '.min.js', minified)
})
.then(zip)
})
.catch(logError)

function toUpper (_, c) {
return c ? c.toUpperCase() : ''
}

const classifyRE = /(?:^|[-_\/])(\w)/g
function classify (str) {
return str.replace(classifyRE, toUpper)
}

function write (dest, code) {
return new Promise(function (resolve, reject) {
fs.writeFile(dest, code, function (err) {
if (err) return reject(err)
console.log(blue(dest) + ' ' + getSize(code))
resolve()
})
})
}

function zip () {
return new Promise(function (resolve, reject) {
fs.readFile('dist/' + pack.name + '.min.js', function (err, buf) {
if (err) return reject(err)
zlib.gzip(buf, function (err, buf) {
if (err) return reject(err)
write('dist/' + pack.name + '.min.js.gz', buf).then(resolve)
})
})
})
}

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'
}
22 changes: 0 additions & 22 deletions config/webpack.min.conf.js

This file was deleted.

34 changes: 24 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@
"bugs": {
"url": "https://github.com/kazupon/vue-i18n/issues"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"devDependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-plugin-espower": "^2.0.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.3.13",
"babel-preset-es2015": "^6.1.18",
"babel-preset-es2015-rollup": "^1.0.0",
"cz-conventional-changelog": "^1.1.4",
"eslint": "^1.10.2",
"eslint-loader": "^1.1.1",
"espower-babel": "^3.3.0",
"espower-babel": "^4.0.0",
"istanbul-instrumenter-loader": "^0.1.3",
"json-loader": "^0.5.4",
"karma": "^0.13.9",
Expand All @@ -37,11 +44,22 @@
"nightmare": "^2.0.8",
"phantomjs": "^1.9.19",
"power-assert": "^1.2.0",
"rollup": "^0.21.1",
"rollup-plugin-babel": "^2.2.0",
"rollup-plugin-replace": "^1.1.0",
"uglify-js": "^2.6.1",
"vue": "^0.12.0",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"files": [
"dist/vue-i18n.js",
"dist/vue-i18n.min.js",
"dist/vue-i18n.common.js",
"src"
],
"homepage": "https://github.com/kazupon/vue-i18n",
"jsnext:main": "src/index.js",
"keywords": [
"i18n",
"internationalization",
Expand All @@ -50,30 +68,26 @@
"vue.js"
],
"license": "MIT",
"main": "dist/vue-i18n.js",
"main": "dist/vue-i18n.common.js",
"repository": {
"type": "git",
"url": "git+https://github.com/kazupon/vue-i18n.git"
},
"scripts": {
"browser": "VUE_I18N_TYPE=browser karma start config/karma.conf.js",
"build": "node config/build.js",
"ci": "npm run lint && npm run coverage && npm run coveralls && npm run sauce",
"clean": "rm -rf lib && rm -rf coverage && rm -rf dist",
"clean": "rm -rf coverage && rm -rf dist/*.*",
"coolkids": "VUE_I18N_TYPE=sauce SAUCE=batch1 karma start config/karma.conf.js",
"coverage": "VUE_I18N_TYPE=coverage karma start config/karma.conf.js",
"dev": "webpack-dev-server --quiet --config config/webpack.test.conf.js --host 0.0.0.0",
"coveralls": "VUE_I18N_TYPE=coveralls karma start config/karma.conf.js",
"e2e": "webpack-dev-server --quiet -h --config config/webpack.e2e.conf.js --content-base test/e2e & mocha -t 20000 --compilers js:espower-babel/guess test/e2e/test.js && kill $! || (kill $! && exit 1)",
"dev": "webpack-dev-server --quiet --config config/webpack.test.conf.js",
"e2e": "webpack-dev-server --quiet --config config/webpack.e2e.conf.js & mocha -t 20000 --compilers js:espower-babel/guess test/e2e/test.js && kill $! || (kill $! && exit 1)",
"ie": "VUE_I18N_TYPE=sauce SAUCE=batch2 karma start config/karma.conf.js",
"lint": "eslint src test",
"mobile": "VUE_I18N_TYPE=sauce SAUCE=batch3 karma start config/karma.conf.js",
"sauce": "npm run coolkids && npm run ie && npm run mobile",
"test": "npm run ci",
"unit": "npm run lint && karma start config/karma.conf.js"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import extend from './extend'
* @param {Object} opts
*/

export default function (Vue, opts = { lang: 'en', locales: {} }) {
function plugin (Vue, opts = { lang: 'en', locales: {} }) {
defineConfig(Vue.config, opts.lang)
extend(Vue, opts.locales)
}
Expand All @@ -30,3 +30,7 @@ function defineConfig (config, lang) {
set: (val) => { lang = val }
})
}

plugin.version = '2.3.1'

export default plugin

0 comments on commit 2f32ecc

Please sign in to comment.