Skip to content

Commit

Permalink
add rollup as build tools and make dist file
Browse files Browse the repository at this point in the history
  • Loading branch information
sky-admin committed May 24, 2019
1 parent 879d0e6 commit e35d0a5
Show file tree
Hide file tree
Showing 7 changed files with 445 additions and 13 deletions.
5 changes: 5 additions & 0 deletions packages/webview-bridge/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
["@babel/env", {"modules": false}]
]
}
1 change: 1 addition & 0 deletions packages/webview-bridge/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist
83 changes: 83 additions & 0 deletions packages/webview-bridge/build/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const fs = require('fs')
const path = require('path')
const zlib = require('zlib')
const terser = require('terser')
const rollup = require('rollup')
const configs = require('./configs')

if (!fs.existsSync('dist')) {
fs.mkdirSync('dist')
}

build(Object.keys(configs).map(key => configs[key]))

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 buildEntry ({ input, output }) {
const { file, banner } = output
const isProd = /min\.js$/.test(file)
return rollup.rollup(input)
.then(bundle => bundle.generate(output))
.then(({ output: [{ code }] }) => {
if (isProd) {
const minified = (banner ? banner + '\n' : '') + terser.minify(code, {
toplevel: true,
output: {
ascii_only: true
},
compress: {
pure_funcs: ['makeMap']
}
}).code
return write(file, minified, true)
} else {
return write(file, 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'
}
87 changes: 87 additions & 0 deletions packages/webview-bridge/build/configs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const path = require('path')
const babel = require('rollup-plugin-babel')
const replace = require('rollup-plugin-replace')
const version = process.env.VERSION || require('../package.json').version
const banner =
`/**
* mpxjs webview bridge v${version}
* (c) ${new Date().getFullYear()} @mpxjs team
* @license Apache
*/`

const resolve = _path => path.resolve(__dirname, '../', _path)

const configs = {
umdDev: {
input: resolve('src/index.js'),
file: resolve('dist/webviewbridge.js'),
format: 'umd',
env: 'development'
},
umdProd: {
input: resolve('src/index.js'),
file: resolve('dist/webviewbridge.min.js'),
format: 'umd',
env: 'production'
},
esm: {
input: resolve('src/index.esm.js'),
file: resolve('dist/webviewbridge.esm.js'),
format: 'es'
},
'esm-browser-dev': {
input: resolve('src/index.esm.js'),
file: resolve('dist/webviewbridge.esm.browser.js'),
format: 'es',
env: 'development',
transpile: false
},
'esm-browser-prod': {
input: resolve('src/index.esm.js'),
file: resolve('dist/webviewbridge.esm.browser.min.js'),
format: 'es',
env: 'production',
transpile: false
}
}

function genConfig (opts) {
const config = {
input: {
input: opts.input,
plugins: [
replace({
__VERSION__: version
})
]
},
output: {
banner,
file: opts.file,
format: opts.format,
name: 'mpx'
}
}

if (opts.env) {
config.input.plugins.unshift(replace({
'process.env.NODE_ENV': JSON.stringify(opts.env)
}))
}

if (opts.transpile !== false) {
config.input.plugins.push(babel())
}

return config
}

function mapValues (obj, fn) {
const res = {}
Object.keys(obj).forEach(key => {
res[key] = fn(obj[key], key)
})
return res
}

module.exports = mapValues(configs, genConfig)
14 changes: 12 additions & 2 deletions packages/webview-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@
"url": "https://github.com/didi/mpx/issues"
},
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
"test": "echo \"Error: run tests from root\" && exit 1",
"build": "node build/build.js"
},
"sideEffects": true
"sideEffects": true,
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"rollup": "^1.12.3",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-node-resolve": "^5.0.0",
"rollup-plugin-replace": "^2.2.0",
"terser": "^4.0.0"
}
}
Loading

0 comments on commit e35d0a5

Please sign in to comment.