This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💥 breaking(build): change dist files bulding
NOTE: using rollup plugin: babel -> buble
- Loading branch information
Showing
5 changed files
with
176 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
const fs = require('fs') | ||
const readFile = fs.readFile | ||
const writeFile = fs.writeFile | ||
const relative = require('path').relative | ||
const gzip = require('zlib').gzip | ||
const rollup = require('rollup') | ||
const uglify = require('uglify-js') | ||
|
||
module.exports = build | ||
|
||
function build (entries) { | ||
let built = 0 | ||
const total = entries.length | ||
const next = () => { | ||
buildEntry(entries[built]).then(() => { | ||
built++ | ||
if (built < total) { | ||
next() | ||
} | ||
}).catch(logError) | ||
} | ||
next() | ||
} | ||
|
||
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, | ||
ascii_only: true | ||
}, | ||
compress: { | ||
pure_funcs: ['makeMap'] | ||
} | ||
}).code | ||
return write(config.dest, minified).then(zip(config.dest)) | ||
} else { | ||
return write(config.dest, code) | ||
} | ||
}) | ||
} | ||
|
||
function write (dest, code) { | ||
return new Promise(function (resolve, reject) { | ||
writeFile(dest, code, function (err) { | ||
if (err) { return reject(err) } | ||
console.log(blue(relative(process.cwd(), dest)) + ' ' + getSize(code)) | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
function zip (file) { | ||
return function () { | ||
return new Promise(function (resolve, reject) { | ||
readFile(file, function (err, buf) { | ||
if (err) { return reject(err) } | ||
gzip(buf, function (err, buf) { | ||
if (err) { return reject(err) } | ||
write(file + '.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 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,65 @@ | ||
const replace = require('rollup-plugin-replace'){{#flow}} | ||
const flow = require('./rollup-plugin-flow'){{/flow}} | ||
const buble = require('rollup-plugin-buble') | ||
const banner = require('./banner') | ||
const pack = require('../package.json') | ||
|
||
function toUpper (_, c) { | ||
return c ? c.toUpperCase() : '' | ||
} | ||
|
||
const classifyRE = /(?:^|[-_\/])(\w)/g | ||
function classify (str) { | ||
return str.replace(classifyRE, toUpper) | ||
} | ||
const moduleName = classify(pack.name) | ||
|
||
const entries = { | ||
commonjs: { | ||
entry: 'src/index.js', | ||
dest: `dist/${pack.name}.common.js`, | ||
format: 'cjs', | ||
banner | ||
}, | ||
production: { | ||
entry: 'src/index.js', | ||
dest: `dist/${pack.name}.min.js`, | ||
format: 'umd', | ||
env: 'production', | ||
moduleName, | ||
banner | ||
}, | ||
development: { | ||
entry: 'src/index.js', | ||
dest: `dist/${pack.name}.js`, | ||
format: 'umd', | ||
env: 'development', | ||
moduleName, | ||
banner | ||
} | ||
} | ||
|
||
function genConfig (opts) { | ||
const config = { | ||
entry: opts.entry, | ||
dest: opts.dest, | ||
format: opts.format, | ||
banner: opts.banner, | ||
moduleName, | ||
plugins: [{{#flow}} | ||
flow(),{{/flow}} | ||
buble() | ||
] | ||
} | ||
|
||
if (opts.env) { | ||
config.plugins.push(replace({ | ||
'process.env.NODE_ENV': JSON.stringify(opts.env) | ||
})) | ||
} | ||
|
||
return config | ||
} | ||
|
||
exports.getEntry = name => genConfig(entries[name]) | ||
exports.getAllEntries = () => Object.keys(entries).map(name => genConfig(entries[name])) |
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,15 @@ | ||
const flowRemoveTypes = require('flow-remove-types') | ||
const createFilter = require('rollup-pluginutils').createFilter | ||
|
||
module.exports = options => { | ||
options = options || {} | ||
const filter = createFilter(options.include, options.exclude) | ||
return { | ||
name: 'flow-remove-types', | ||
transform: (code, id) => { | ||
if (filter(id)) { | ||
return flowRemoveTypes(code) | ||
} | ||
} | ||
} | ||
} |
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