This repository has been archived by the owner on Dec 31, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 860
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bundle): more compact the vue-i18n distribution file
- Loading branch information
Showing
6 changed files
with
158 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters