Skip to content
This repository has been archived by the owner on Feb 8, 2019. It is now read-only.

Commit

Permalink
💥 breaking(build): change dist files bulding
Browse files Browse the repository at this point in the history
NOTE:
  using rollup plugin: babel -> buble
  • Loading branch information
kazupon committed Sep 12, 2016
1 parent 37dc1a0 commit 397d93a
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 87 deletions.
92 changes: 10 additions & 82 deletions template/config/build.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
const zlib = require('zlib')
const rollup = require('rollup')
const uglify = require('uglify-js')
const babel = require('rollup-plugin-babel')
const replace = require('rollup-plugin-replace')
const pack = require('../package.json')
const banner = require('./banner')
const fs = require('fs')
const readFile = fs.readFileSync
const writeFile = fs.writeFileSync
const exist = fs.existsSync
const mkdir = fs.mkdirSync
const pack = require('../package.json')
const getAllEntries = require('./entry').getAllEntries
const build = require('./bundle')

if (!exist('dist')) {
mkdir('dist')
Expand All @@ -20,82 +16,14 @@ const main = readFile('src/index.js', 'utf-8')
.replace(/plugin\.version = '[\d\.]+'/, `plugin.version = '${pack.version}'`)
writeFile('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()
]
}).then(bundle => {
return write(
`dist/${pack.name}.common.js`,
bundle.generate({ format: 'cjs', banner }).code
)
}).then(() => { // Standalone Dev Build
return rollup.rollup({
entry: 'src/index.js',
plugins: [
replace({ 'process.env.NODE_ENV': "'development'" }),
babel()
]
}).then(bundle => {
return write(
`dist/${pack.name}.js`,
bundle.generate({
format: 'umd', banner, moduleName: classify(pack.name)
}).code
)
})
}).then(() => { // Standalone Production Build
return rollup.rollup({
entry: 'src/index.js',
plugins: [
replace({ 'process.env.NODE_ENV': "'production'" }),
babel()
]
}).then(bundle => {
const code = bundle.generate({
format: 'umd',
moduleName: classify(pack.name)
}).code
const 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) }
let entries = getAllEntries()

function write (dest, code) {
return new Promise((resolve, reject) => {
fs.writeFile(dest, code, err => {
if (err) { return reject(err) }
console.log(blue(dest) + ' ' + getSize(code))
resolve()
})
// filter entries via command line arg
if (process.argv[2]) {
const filters = process.argv[2].split(',')
entries = entries.filter(b => {
return filters.some(f => b.dest.indexOf(f) > -1)
})
}

function zip () {
return new Promise((resolve, reject) => {
fs.readFile(`dist/${pack.name}.min.js`, (err, buf) => {
if (err) { return reject(err) }
zlib.gzip(buf, (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' }
build(entries)
81 changes: 81 additions & 0 deletions template/config/bundle.js
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'
}
65 changes: 65 additions & 0 deletions template/config/entry.js
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]))
15 changes: 15 additions & 0 deletions template/config/rollup-plugin-flow.js
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)
}
}
}
}
10 changes: 5 additions & 5 deletions template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"babel-plugin-coverage": "^1.0.0",
"babel-plugin-espower": "^2.0.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-es2015-rollup": "^1.0.0",
"babel-preset-es2015-rollup-vue": "^1.1.0",
"buble": "^0.13.1",
{{#flow}}
"babel-preset-flow-vue": "^1.0.0",
"eslint-plugin-flowtype": "^2.16.0",
"flow-bin": "^0.32.0",
"flow-remove-types": "github:yyx990803/flow-remove-types",
{{/flow}}
{{#conventional}}
"conventional-changelog-cli": "^1.1.1",
Expand Down Expand Up @@ -58,8 +58,8 @@
"karma-webpack": "^1.7.0",
"phantomjs-prebuilt": "^2.1.1",
"power-assert": "^1.2.0",
"rollup": "^0.26.3",
"rollup-plugin-babel": "^2.4.0",
"rollup": "^0.34.10",
"rollup-plugin-buble": "^0.13.0",
"rollup-plugin-replace": "^1.1.0",
"uglify-js": "^2.6.1",
{{#e2e}}
Expand Down Expand Up @@ -101,7 +101,7 @@
},
{{/unless_eq}}
"scripts": {
"build": "NODE_ENV=production node config/build.js{{#gitbook}} && npm run docs:build{{/gitbook}}",
"build": "node config/build.js{{#gitbook}} && npm run docs:build{{/gitbook}}",
{{#conventional}}
"changelog": "conventional-changelog -i CHANGELOG.md -s {{#unless_eq conventionalConfig "none"}}-p {{ conventionalConfig }}{{/unless_eq}}{{#if_eq conventionalConfig "none"}}-n ./config/convention.js{{/if_eq}}",
"release": "conventional-github-releaser {{#unless_eq conventionalConfig "none"}}-p {{ conventionalConfig }}{{/unless_eq}}{{#if_eq conventionalConfig "none"}}-n ./config/convention.js{{/if_eq}}",
Expand Down

0 comments on commit 397d93a

Please sign in to comment.