UNPKG

11.2 kBSource Map (JSON)View Raw
1{"version":3,"file":"index.js","sources":["../src/index.js"],"sourcesContent":["import { readFile as origReadFile } from \"fs\";\nimport { promisify } from \"util\";\nimport { dirname, resolve as pathResolve, join } from \"path\";\n\nimport fileSize from \"filesize\";\nimport gzip from \"gzip-size\";\nimport terser from \"terser\";\nimport brotli from \"brotli-size\";\nimport pacote from \"pacote\";\n\nconst readFile = promisify(origReadFile);\n\nconst isWindows = process.platform === \"win32\";\n\nfunction fixWindowsPath(path) {\n\treturn path.slice(\n\t\t// istanbul ignore next\n\t\tisWindows ? 1 : 0\n\t);\n}\n\n// Node should be ok with this, but transpiling\n// to `require` doesn't work, so detect Windows\n// to remove slash instead\n// \"file://\" +\nconst thisDirectory = fixWindowsPath(\n\tdirname(\n\t\tnew URL(\n\t\t\t// `import.meta.url` is giving backslashes in Windows currently\n\t\t\t// (at least in how it is compiled to CJS) which makes for an\n\t\t\t// invalid URL\n\t\t\timport.meta.url.replace(/\\\\/g, \"/\")\n\t\t).pathname\n\t)\n);\n\nexport default function filesize(options = {}, env) {\n\tlet {\n\t\trender,\n\t\tformat = {},\n\t\ttheme = \"dark\",\n\t\tshowBeforeSizes = \"none\",\n\t\tshowGzippedSize = true,\n\t\tshowBrotliSize = false,\n\t\tshowMinifiedSize = true,\n\t} = options;\n\n\tconst getLoggingData = async function (outputOptions, bundle) {\n\t\tconst { code, fileName } = bundle;\n\t\tconst info = {};\n\n\t\tlet codeBefore;\n\t\tif (showBeforeSizes !== \"none\") {\n\t\t\tlet file = outputOptions.file || outputOptions.dest;\n\t\t\tif (showBeforeSizes !== \"build\") {\n\t\t\t\tconst { name } = await import(join(process.cwd(), \"./package.json\"));\n\t\t\t\ttry {\n\t\t\t\t\tconst output = join(thisDirectory, \"../.cache\");\n\n\t\t\t\t\tconst { resolved } = await pacote.extract(`${name}@latest`, output);\n\t\t\t\t\tconst idx = resolved.lastIndexOf(name);\n\t\t\t\t\tconst lastVersion = resolved.slice(\n\t\t\t\t\t\tidx + name.length + 1,\n\t\t\t\t\t\t-\".tgz\".length\n\t\t\t\t\t);\n\t\t\t\t\tinfo.lastVersion = lastVersion;\n\n\t\t\t\t\tfile = pathResolve(output, file);\n\t\t\t\t} catch (err) {\n\t\t\t\t\t// Package might not exist\n\t\t\t\t\tconsole.log(`Package, \"${name}\", was not found.`);\n\t\t\t\t\tfile = null;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (file) {\n\t\t\t\ttry {\n\t\t\t\t\tcodeBefore = await readFile(file, \"utf8\");\n\t\t\t\t} catch (err) {\n\t\t\t\t\tconsole.log(`File, \"${file}\", was not found.`);\n\t\t\t\t\t// File might not exist\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tinfo.fileName = fileName;\n\n\t\tinfo.bundleSize = fileSize(Buffer.byteLength(code), format);\n\n\t\tinfo.brotliSize = showBrotliSize\n\t\t\t? fileSize(await brotli(code), format)\n\t\t\t: \"\";\n\n\t\tif (showMinifiedSize || showGzippedSize) {\n\t\t\tconst minifiedCode = (await terser.minify(code)).code;\n\t\t\tinfo.minSize = showMinifiedSize\n\t\t\t\t? fileSize(minifiedCode.length, format)\n\t\t\t\t: \"\";\n\t\t\tinfo.gzipSize = showGzippedSize\n\t\t\t\t? fileSize(gzip.sync(minifiedCode), format)\n\t\t\t\t: \"\";\n\t\t}\n\n\t\tif (codeBefore) {\n\t\t\tinfo.bundleSizeBefore = fileSize(Buffer.byteLength(codeBefore), format);\n\t\t\tinfo.brotliSizeBefore = showBrotliSize\n\t\t\t\t? fileSize(await brotli(codeBefore), format)\n\t\t\t\t: \"\";\n\t\t\tif (showMinifiedSize || showGzippedSize) {\n\t\t\t\tconst minifiedCode = (await terser.minify(codeBefore)).code;\n\t\t\t\tinfo.minSizeBefore = showMinifiedSize\n\t\t\t\t\t? fileSize(minifiedCode.length, format)\n\t\t\t\t\t: \"\";\n\t\t\t\tinfo.gzipSizeBefore = showGzippedSize\n\t\t\t\t\t? fileSize(gzip.sync(minifiedCode), format)\n\t\t\t\t\t: \"\";\n\t\t\t}\n\t\t}\n\n\t\tconst opts = {\n\t\t\tformat,\n\t\t\ttheme,\n\t\t\trender,\n\t\t\tshowBeforeSizes,\n\t\t\tshowGzippedSize,\n\t\t\tshowBrotliSize,\n\t\t\tshowMinifiedSize,\n\t\t};\n\n\t\tif (render) {\n\t\t\tconsole.warn(\n\t\t\t\t\"`render` is now deprecated. Please use `reporter` instead.\"\n\t\t\t);\n\t\t\treturn opts.render(opts, outputOptions, info);\n\t\t}\n\n\t\tconst reporters = options.reporter\n\t\t\t? Array.isArray(options.reporter)\n\t\t\t\t? options.reporter\n\t\t\t\t: [options.reporter]\n\t\t\t: [\"boxen\"];\n\n\t\treturn (\n\t\t\tawait Promise.all(\n\t\t\t\treporters.map(async (reporter) => {\n\t\t\t\t\tif (typeof reporter === \"string\") {\n\t\t\t\t\t\tlet p;\n\t\t\t\t\t\tif (reporter === \"boxen\") {\n\t\t\t\t\t\t\tp = import(join(thisDirectory, \"/reporters/boxen.js\"));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tp = import(pathResolve(process.cwd(), reporter));\n\t\t\t\t\t\t}\n\t\t\t\t\t\treporter = (await p).default;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn reporter(opts, outputOptions, info);\n\t\t\t\t})\n\t\t\t)\n\t\t).join(\"\");\n\t};\n\n\tif (env === \"test\") {\n\t\treturn getLoggingData;\n\t}\n\n\treturn {\n\t\tname: \"filesize\",\n\t\tasync generateBundle(outputOptions, bundle /* , isWrite */) {\n\t\t\tconst dataStrs = await Promise.all(\n\t\t\t\tObject.keys(bundle)\n\t\t\t\t\t.map((fileName) => bundle[fileName])\n\t\t\t\t\t.filter((currentBundle) => {\n\t\t\t\t\t\tif ({}.hasOwnProperty.call(currentBundle, \"type\")) {\n\t\t\t\t\t\t\treturn currentBundle.type !== \"asset\";\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn !currentBundle.isAsset;\n\t\t\t\t\t})\n\t\t\t\t\t.map((currentBundle) => {\n\t\t\t\t\t\treturn getLoggingData(outputOptions, currentBundle);\n\t\t\t\t\t})\n\t\t\t);\n\t\t\tdataStrs.forEach((str) => {\n\t\t\t\tif (str) {\n\t\t\t\t\tconsole.log(str);\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n}\n"],"names":["readFile","promisify","origReadFile","isWindows","process","platform","fixWindowsPath","path","slice","thisDirectory","dirname","URL","import","replace","pathname","filesize","options","env","render","format","theme","showBeforeSizes","showGzippedSize","showBrotliSize","showMinifiedSize","getLoggingData","outputOptions","bundle","code","fileName","info","codeBefore","file","dest","name","join","cwd","output","resolved","pacote","extract","idx","lastIndexOf","lastVersion","length","pathResolve","err","console","log","bundleSize","fileSize","Buffer","byteLength","brotliSize","brotli","minifiedCode","terser","minify","minSize","gzipSize","gzip","sync","bundleSizeBefore","brotliSizeBefore","minSizeBefore","gzipSizeBefore","opts","warn","reporters","reporter","Array","isArray","Promise","all","map","p","default","generateBundle","dataStrs","Object","keys","filter","currentBundle","hasOwnProperty","call","type","isAsset","forEach","str"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAUA,MAAMA,QAAQ,GAAGC,cAAS,CAACC,WAAD,CAA1B;AAEA,MAAMC,SAAS,GAAGC,OAAO,CAACC,QAAR,KAAqB,OAAvC;;AAEA,SAASC,cAAT,CAAwBC,IAAxB,EAA8B;AAC7B,SAAOA,IAAI,CAACC,KAAL;AAENL,EAAAA,SAAS,GAAG,CAAH,GAAO,CAFV,CAAP;AAIA;AAGD;AACA;AACA;;;AACA,MAAMM,aAAa,GAAGH,cAAc,CACnCI,YAAO,CACN,IAAIC,GAAJ;AAEC;AACA;AACAC,mMAAA,CAAgBC,OAAhB,CAAwB,KAAxB,EAA+B,GAA/B,CAJD,EAKEC,QANI,CAD4B,CAApC;AAWe,SAASC,QAAT,CAAkBC,OAAO,GAAG,EAA5B,EAAgCC,GAAhC,EAAqC;AACnD,MAAI;AACHC,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