index.js
async function getSize (files, opts) {
  if (typeof files === 'string') files = [files]
  if (!opts) opts = { }

  if (opts.webpack === false) {
    let sizes = await Promise.all(files.map(async file => {
      let bytes = await readFile(file, 'utf8')
      let result = { parsed: bytes.length }
      if (opts.running !== false) result.running = await getRunningTime(file)
      if (opts.gzip === false) {
        result.loading = getLoadingTime(result.parsed)
      } else {
        result.gzip = await gzipSize(bytes)
        result.loading = getLoadingTime(result.gzip)
      }
      return result
    }))
    return sizes.reduce(sumSize)
  } else {
    let config = getConfig(files, opts)
    let output = path.join(
      config.output.path || process.cwd(), config.output.filename)
    let size, running
    try {
      let stats = await runWebpack(config)
      if (opts.running !== false) running = await getRunningTime(output)

      if (stats.hasErrors()) {
        throw new Error(stats.toString('errors-only'))
      }

      if (opts.config && stats.stats) {
        size = stats.stats
          .map(stat => extractSize(stat.toJson(), opts))
          .reduce(sumSize)
      } else {
        size = extractSize(stats.toJson(), opts)
      }
    } finally {
      if (config.output.path && !opts.output) {
        await del(config.output.path, { force: true })
      }
    }

    let result = { parsed: size.parsed - WEBPACK_EMPTY_PROJECT_PARSED }
    if (opts.running !== false) result.running = running
    if (opts.config || opts.gzip === false) {
      result.loading = getLoadingTime(result.parsed)
    } else {
      result.gzip = size.gzip - WEBPACK_EMPTY_PROJECT_GZIP
      result.loading = getLoadingTime(result.gzip)
    }
    return result
  }
}
async function getSize (files, opts) {
  if (typeof files === 'string') files = [files]
  if (!opts) opts = { }

  if (opts.webpack === false) {
    let sizes = await Promise.all(files.map(async file => {
      let bytes = await readFile(file, 'utf8')
      let result = { parsed: bytes.length }
      if (opts.running !== false) result.running = await getRunningTime(file)
      if (opts.gzip === false) {
        result.loading = getLoadingTime(result.parsed)
      } else {
        result.gzip = await gzipSize(bytes)
        result.loading = getLoadingTime(result.gzip)
      }
      return result
    }))
    return sizes.reduce(sumSize)
  } else {
    let config = getConfig(files, opts)
    let output = path.join(
      config.output.path || process.cwd(), config.output.filename)
    let size, running
    try {
      let stats = await runWebpack(config)
      if (opts.running !== false) running = await getRunningTime(output)

      if (stats.hasErrors()) {
        throw new Error(stats.toString('errors-only'))
      }

      if (opts.config && stats.stats) {
        size = stats.stats
          .map(stat => extractSize(stat.toJson(), opts))
          .reduce(sumSize)
      } else {
        size = extractSize(stats.toJson(), opts)
      }
    } finally {
      if (config.output.path && !opts.output) {
        await del(config.output.path, { force: true })
      }
    }

    let result = { parsed: size.parsed - WEBPACK_EMPTY_PROJECT_PARSED }
    if (opts.running !== false) result.running = running
    if (opts.config || opts.gzip === false) {
      result.loading = getLoadingTime(result.parsed)
    } else {
      result.gzip = size.gzip - WEBPACK_EMPTY_PROJECT_GZIP
      result.loading = getLoadingTime(result.gzip)
    }
    return result
  }
}
async function getSize ( files , opts ) { if ( typeof files === 'string' ) files = [ files ] if ( ! opts ) opts = { } if ( opts . webpack === false ) { let sizes = await Promise . all ( files . map ( async file => { let bytes = await readFile ( file , 'utf8' ) let result = { parsed : bytes . length } if ( opts . running !== false ) result . running = await getRunningTime ( file ) if ( opts . gzip === false ) { result . loading = getLoadingTime ( result . parsed ) } else { result . gzip = await gzipSize ( bytes ) result . loading = getLoadingTime ( result . gzip ) } return result } ) ) return sizes . reduce ( sumSize ) } else { let config = getConfig ( files , opts ) let output = path . join ( config . output . path || process . cwd ( ) , config . output . filename ) let size , running try { let stats = await runWebpack ( config ) if ( opts . running !== false ) running = await getRunningTime ( output ) if ( stats . hasErrors ( ) ) { throw new Error ( stats . toString ( 'errors-only' ) ) } if ( opts . config && stats . stats ) { size = stats . stats . map ( stat => extractSize ( stat . toJson ( ) , opts ) ) . reduce ( sumSize ) } else { size = extractSize ( stats . toJson ( ) , opts ) } } finally { if ( config . output . path && ! opts . output ) { await del ( config . output . path , { force : true } ) } } let result = { parsed : size . parsed - WEBPACK_EMPTY_PROJECT_PARSED } if ( opts . running !== false ) result . running = running if ( opts . config || opts . gzip === false ) { result . loading = getLoadingTime ( result . parsed ) } else { result . gzip = size . gzip - WEBPACK_EMPTY_PROJECT_GZIP result . loading = getLoadingTime ( result . gzip ) } return result } }
Return size of project files with all dependencies and after UglifyJS
and gzip.

@param {string|string[]} files Files to get size.
@param {object} [opts] Extra options.
@param {"server"|"static"|false} [opts.analyzer=false] Show package
content in browser.
@param {boolean} [opts.webpack=true] Pack files by webpack.
@param {boolean} [opts.running=true] Calculate running time.
@param {boolean} [opts.gzip=true] Compress files by gzip.
@param {string} [opts.config] A path to custom webpack config.
@param {string} [opts.bundle] Bundle name for Analyzer mode.
@param {string} [opts.output] A path for output bundle.
@param {string[]} [opts.ignore] Dependencies to be ignored.
@param {string[]} [opts.entry] Webpack entry whose size will be checked.

@return {Promise} Promise with parsed and gzip size of files

@example
const getSize = require('size-limit')

const index = path.join(__dirname, 'index.js')
const extra = path.join(__dirname, 'extra.js')

getSize([index, extra]).then(size => {
if (size.gzip > 1 * 1024 * 1024) {
console.error('Project become bigger than 1MB')
}
})
Return size of project files with all dependencies and after UglifyJS and gzip .