Skip to content
This repository has been archived by the owner on Mar 21, 2023. It is now read-only.

Commit

Permalink
Replace lodash with locale functions to save bitebytes
Browse files Browse the repository at this point in the history
  • Loading branch information
wangpin34 committed Nov 4, 2019
1 parent 6ca7a22 commit ba6e2c6
Show file tree
Hide file tree
Showing 13 changed files with 3,848 additions and 6,648 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
91 changes: 49 additions & 42 deletions build/build.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
const fs = require('fs')
const path = require('path')
const zlib = require('zlib')
const uglify = require('uglify-js')
const rollup = require('rollup')
const configs = require('./configs')
import fs from 'fs'
import path from 'path'
import zlib from 'zlib'
import uglify from 'uglify-js'
import * as rollup from 'rollup'
import configs from './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)
async function build (builds) {
try {
for (let i = 0; i < builds.length; i ++) {
await buildEntry(builds[i])
}
} catch (err) {
console.error(`err occurs: ${err}`)
}

next()
}

function buildEntry ({ input, output }) {
const isProd = /min\.js$/.test(output.file)
return rollup.rollup(input)
.then(bundle => bundle.generate(output))
.then(({ code }) => {
if (isProd) {
var minified = uglify.minify(code, {
output: {
preamble: output.banner,
/* eslint-disable camelcase */
ascii_only: true
/* eslint-enable camelcase */
}
}).code
return write(output.file, minified, true)
} else {
return write(output.file, code)
async function buildEntry ({ input: inputOptions, output: outputOptions }) {
try {
const isProd = /min\.js$/.test(outputOptions.file)
const bundle = await rollup.rollup(inputOptions)
const { output } = await bundle.generate(outputOptions)
for (const chunkOrAsset of output) {
if (chunkOrAsset.type === 'chunk') {
const code = chunkOrAsset.code
if (isProd) {
var minified = uglify.minify(code, {
output: {
preamble: outputOptions.banner,
/* eslint-disable camelcase */
ascii_only: true
/* eslint-enable camelcase */
}
}).code
return write(outputOptions.file, minified, true)
} else {
return write(outputOptions.file, code)
}
}
})
}
} catch (err) {
throw err
}
}

function write (dest, code, zip) {
Expand Down Expand Up @@ -80,4 +76,15 @@ function blue (str) {
return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
}

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

(async () => {
try {
await build(Object.keys(configs).map(key => configs[key]))
} catch (err) {
console.error(err)
}
})()

40 changes: 20 additions & 20 deletions build/configs.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
const path = require('path')
const buble = require('rollup-plugin-buble')
const flow = require('rollup-plugin-flow-no-whitespace')
const cjs = require('rollup-plugin-commonjs')
const node = require('rollup-plugin-node-resolve')
const replace = require('rollup-plugin-replace')
const strip = require('rollup-plugin-strip')
const version = process.env.VERSION || require('../package.json').version
import fs from 'fs'
import path from 'path'
import buble from 'rollup-plugin-buble'
import sizes from 'rollup-plugin-sizes'
import flow from 'rollup-plugin-flow-no-whitespace'
import cjs from 'rollup-plugin-commonjs'
import node from 'rollup-plugin-node-resolve'
import replace from 'rollup-plugin-replace'
import cleanup from 'rollup-plugin-cleanup'
import strip from 'rollup-plugin-strip'

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

const pkg = fs.readFileSync(resolve('package.json'))

const version = process.env.VERSION || pkg.version
const banner =
`/**
* vue-scroll v${version}
* (c) ${new Date().getFullYear()} Wang Pin
* @license MIT
*/`

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


module.exports = [
// browser dev
Expand Down Expand Up @@ -42,13 +50,15 @@ function genConfig (opts) {
input: {
input: resolve('src/index.js'),
plugins: [
flow(),
flow(), // Remove flow type
node(),
cjs(),
replace({
__VERSION__: version
}),
buble(),
sizes(),
cleanup(),
strip({
// set this to `false` if you don't want to
// remove debugger statements
Expand All @@ -75,16 +85,6 @@ function genConfig (opts) {
config.input.plugins.unshift(replace({
'process.env.NODE_ENV': JSON.stringify(opts.env)
}))
} else {
config.input.external = [
'lodash/isObject',
'lodash/isFunction',
'lodash/isInteger',
'lodash/isFinite',
'lodash/debounce',
'lodash/throttle',
'es6-map/implement'
]
}

return config
Expand Down
11 changes: 1 addition & 10 deletions build/rollup.dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,5 @@ export default {
output: {
file: 'dist/vue-scroll.dev.js',
format: 'cjs'
},
external: [
'lodash/isObject',
'lodash/isFunction',
'lodash/isInteger',
'lodash/isFinite',
'lodash/debounce',
'lodash/throttle',
'es6-map/implement'
]
}
}
Loading

0 comments on commit ba6e2c6

Please sign in to comment.