<commit_before>const fs = require('fs')
const path = require('path')
const rollup = require('rollup')
const version = require('../package.json').version
const zlib = require('zlib')
const typescript = require('rollup-plugin-typescript2')
const uglify = require('rollup-plugin-uglify').uglify
const program = require('commander')

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

function resolve(p) {
  return path.resolve(__dirname, '../', p)
}

function trans2CamelCase(str){
  const re=/-(\w)/g;
  const newStr = str.replace(re, function ($0,$1){
      return $1.toUpperCase();
  })
  return newStr.charAt(0).toUpperCase() + newStr.slice(1);
}

const banner =
  '/*!\n' +
  ' * better-normal-scroll v' + version + '\n' +
  ' * (c) 2016-' + new Date().getFullYear() + ' ustbhuangyi\n' +
  ' * Released under the MIT License.\n' +
  ' */'
const bsConfig = {
  output: 'dist',
  entry: 'src/index.ts',
  libName: 'BScroll',
  fileName: 'bscroll'
}
const bsPluginsConfig = {
  rootPath: 'src/plugins',
  output: 'dist/lib',
  plugins: {
    'observe-dom': 'observe-dom/index.ts',
    'pull-down': 'pull-down/pull-down.ts',
    'pull-up': 'pull-up/pull-up.ts',
    'scroll-bar': 'scroll-bar/scroll-bar.ts',
    'slide': 'slide/index.ts',
    'wheel': 'wheel/index.ts',
    'zoom': 'zoom/index.ts'
  }
}
const buildType = [
  {
    format: 'umd',
    extend: 'js'
  },
  {
    format: 'es',
    extend: 'esm.js'
  },
  {
    format: 'umd',
    extend: 'min.js'
  }
]

function genRollupPlugins(isMin) {
  const tsConfig = {
    useTsconfigDeclarationDir: true
  }
  const plugins = []
    if (isMin) {
      plugins.push(uglify())
    }
  plugins.push(typescript(tsConfig))
  return plugins
}
function genBSBuildConfig(bsConfig, buildType) {
  const result = []
  buildType.forEach(type => {
    result.push({
      input: resolve(bsConfig.entry),
      output: {
        file: resolve(`${bsConfig.output}/${bsConfig.fileName.replace('.ts', '')}.${type.extend}`),
        name: bsConfig.libName,
        format: type.format,
        banner
      },
      plugins: genRollupPlugins(type.extend.indexOf('min')>-1)
    })
  })
  return result
}
function genPluginsBuildConfig(bsPluginsConfig, buildType, specialName) {
  const result = []
  buildType.forEach(type => {
    bsPluginsName = Object.keys(bsPluginsConfig.plugins)
    bsPluginsName.forEach(pluginName => {
      if (specialName && specialName !== pluginName) {
        return
      }
      const entryPath = `${bsPluginsConfig.rootPath}/${bsPluginsConfig.plugins[pluginName]}`
      const camelcaseName = trans2CamelCase(pluginName)
      result.push({
        input: resolve(entryPath),
        output: {
          file: resolve(`${bsPluginsConfig.output}/${pluginName.replace('.ts', '')}.${type.extend}`),
          name: camelcaseName,
          format: type.format,
          banner
        },
        plugins: genRollupPlugins(type.extend.indexOf('min')>-1)
      })
    })
  })
  return result
}

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(config) {
  const isProd = /min\.js$/.test(config.output.file)
  return rollup.rollup(config).then((bundle) => {
    return new Promise((resolve, reject) => {
      bundle.write(config.output).then((output) => {
        const code = output.code
        function report(extra) {
          console.log(blue(path.relative(process.cwd(), config.output.file)) + ' ' + getSize(code) + (extra || ''))
          resolve()
        }
        if (isProd) {
          zlib.gzip(code, (err, zipped) => {
            if (err) return reject(err)
            report(' (gzipped: ' + getSize(zipped) + ')')
          })
        } else {
          report()
        }
      })
    })
  }).catch(logError)
}

function getSize(code) {
  return (code.length / 1024).toFixed(2) + 'kb'
}

function blue(str) {
  return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
}

function logError(e) {
  console.log(e)
}

const buildHandler = {
  all: function () {
    const builds = genBSBuildConfig(bsConfig, buildType).concat(genPluginsBuildConfig(bsPluginsConfig, buildType))<commit_msg>chore(fix memory leaks when bulid): fix memory leaks when bulid<commit_after>const fs = require('fs')
const path = require('path')
const rollup = require('rollup')
const version = require('../package.json').version
const zlib = require('zlib')
const typescript = require('rollup-plugin-typescript2')
const uglify = require('rollup-plugin-uglify').uglify
const program = require('commander')

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

function resolve(p) {
  return path.resolve(__dirname, '../', p)
}

function trans2CamelCase(str){
  const re=/-(\w)/g;
  const newStr = str.replace(re, function ($0,$1){
      return $1.toUpperCase();
  })
  return newStr.charAt(0).toUpperCase() + newStr.slice(1);
}

const banner =
  '/*!\n' +
  ' * better-normal-scroll v' + version + '\n' +
  ' * (c) 2016-' + new Date().getFullYear() + ' ustbhuangyi\n' +
  ' * Released under the MIT License.\n' +
  ' */'
const bsConfig = {
  output: 'dist',
  entry: 'src/index.ts',
  libName: 'BScroll',
  fileName: 'bscroll'
}
const bsPluginsConfig = {
  rootPath: 'src/plugins',
  output: 'dist/lib',
  plugins: {
    'observe-dom': 'observe-dom/index.ts',
    'pull-down': 'pull-down/pull-down.ts',
    'pull-up': 'pull-up/pull-up.ts',
    'scroll-bar': 'scroll-bar/scroll-bar.ts',
    'slide': 'slide/index.ts',
    'wheel': 'wheel/index.ts',
    'zoom': 'zoom/index.ts'
  }
}
const buildType = [
  {
    format: 'umd',
    extend: 'js'
  },
  {
    format: 'es',
    extend: 'esm.js'
  },
  {
    format: 'umd',
    extend: 'min.js'
  }
]

function genRollupPlugins(isMin) {
  const tsConfig = {
    useTsconfigDeclarationDir: true
  }
  const plugins = []
    if (isMin) {
      plugins.push(uglify())
    }
  plugins.push(typescript(tsConfig))
  return plugins
}
function genBSBuildConfig(bsConfig, buildType) {
  const result = []
  buildType.forEach(type => {
    result.push({
      input: resolve(bsConfig.entry),
      output: {
        file: resolve(`${bsConfig.output}/${bsConfig.fileName.replace('.ts', '')}.${type.extend}`),
        name: bsConfig.libName,
        format: type.format,
        banner
      },
      plugins: genRollupPlugins(type.extend.indexOf('min')>-1)
    })
  })
  return result
}
function genPluginsBuildConfig(bsPluginsConfig, buildType, specialName) {
  const result = []
  buildType.forEach(type => {
    bsPluginsName = Object.keys(bsPluginsConfig.plugins)
    bsPluginsName.forEach(pluginName => {
      if (specialName && specialName !== pluginName) {
        return
      }
      const entryPath = `${bsPluginsConfig.rootPath}/${bsPluginsConfig.plugins[pluginName]}`
      const camelcaseName = trans2CamelCase(pluginName)
      result.push({
        input: resolve(entryPath),
        output: {
          file: resolve(`${bsPluginsConfig.output}/${pluginName.replace('.ts', '')}.${type.extend}`),
          name: camelcaseName,
          format: type.format,
          banner
        },
        plugins: genRollupPlugins(type.extend.indexOf('min')>-1)
      })
    })
  })
  return result
}

function build(builds) {
  let built = 0
  const total = builds.length
  const next = () => {
    buildEntry(builds[built], () => {
      builds[built-1] = null
      built++
      if (built < total) {
        next()
      }
    })
  }
  next()
}

function buildEntry(config, next) {
  const isProd = /min\.js$/.test(config.output.file)
  rollup.rollup(config).then((bundle) => {
    bundle.write(config.output).then((output) => {
      const code = output.code
      function report(extra) {
        console.log(blue(path.relative(process.cwd(), config.output.file)) + ' ' + getSize(code) + (extra || ''))
        next()
      }
      if (isProd) {
        zlib.gzip(code, (err, zipped) => {
          if (err) return reject(err)
          report(' (gzipped: ' + getSize(zipped) + ')')
        })
      } else {
        report()
      }
    })
  }).catch(logError)
}

function getSize(code) {
  return (code.length / 1024).toFixed(2) + 'kb'
}

function blue(str) {
  return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
}

function logError(e) {
  console.log(e)
}

const buildHandler = {
  all: function () {
    const builds = genBSBuildConfig(bsConfig, buildType).concat(genPluginsBuildConfig(bsPluginsConfig, buildType))
{'fraction_non_alphanumeric': 0.17108938547486033, 'fraction_numerical': 0.007332402234636872, 'mean_word_length': 2.900590104403087, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2587190', 'n_tokens_mistral': 3025, 'n_tokens_neox': 2982, 'n_words': 529}