Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
mkdir('dist').then(() => {
// Copy as is for ESM
fs.writeFileSync(pkg.module, data);
// Minify & print gzip-size
let { code } = minify(data, { toplevel:true });
console.log(`> gzip size: ${pretty(sizer.sync(code))}`);
let keys = [];
// Mutate exports for CJS
data = data.replace(/export function\s?(.+?)(?=\()/gi, (_, x) => {
return keys.push(x) && `function ${x}`;
});
keys.sort().forEach(key => {
data += `\nexports.${key} = ${key};`;
});
fs.writeFileSync(pkg.main, data + '\n');
let name = pkg['umd:name'] || pkg.name;
// Write UMD bundle
let UMD = minify(`
(function (global, factory) {
ongenerate(bundle, details) {
const gzipSize = gzip.sync(details.code);
const { bundleSizeThreshold } = packageJson;
console.log('Total size (gzipped):', gzipSize); // eslint-disable-line no-console
if (gzipSize > bundleSizeThreshold) {
throw new RangeError(
`Exceeded size threshold of ${bundleSizeThreshold} bytes (gzipped)!`
);
}
},
},
}).then((results) => {
if (Array.isArray(results)) {
results.forEach((result) => {
this.scripts.push(result.body)
})
}
// join all JS string
parsedData.jsString = this.scripts.join('')
parsedData.size = Buffer.byteLength(parsedData.jsString, 'utf8')
parsedData.gzippedSize = gzipSize.sync(parsedData.jsString)
return resolve(parsedData)
})
.catch(reason => reject(reason))
test('Distribution File Sizes', function (t) {
let filepath = require('path').resolve('./test/lib/ngn.min.js')
let stats = require('fs').statSync(filepath)
let uncompressedSize = (stats.size / 1000).toFixed(2)
t.ok(uncompressedSize < 100, 'Uncompressed distributable file is under 100kb (' + (stats.size / 1000) + 'kb).')
let compressedSize = (gzip.fileSync(filepath, { level: 9 }) / 1000).toFixed(2)
t.ok(compressedSize < 30, 'Compressed (minified) distributable is under 30kb (' + compressedSize + 'kb)')
t.end()
})
const [input] = cli.input;
if (!input && process.stdin.isTTY) {
console.error('Specify a file path');
process.exit(1);
}
const source = input ? fs.createReadStream(input) : process.stdin;
const options = {};
if (cli.flags.level) {
options.level = Number(cli.flags.level);
}
source.pipe(gzipSize.stream(options)).on('gzip-size', size => {
console.log(cli.flags.raw ? size : prettyBytes(size));
});
function updateBundleSizes(readmeContents) {
let newReadmeContents = readmeContents;
const bundles = getBundleNames();
for (const bundleFile of bundles) {
console.info(`Inspecting dist/${bundleFile}`);
const bundleContents = readFileSync(
`${rootPath}/dist/${bundleFile}`,
'utf-8',
);
const gzipSizeBytes = gzipSize.sync(bundleContents);
const gzipSizeKb = bytesToKilobytes(gzipSizeBytes);
const bundleSize =
gzipSizeBytes > 1000 ? `${gzipSizeKb}kb` : `${gzipSizeBytes}b`;
newReadmeContents = newReadmeContents.replace(
`{{bundleSize['${bundleFile}']}}`,
bundleSize,
);
}
return newReadmeContents;
}
readDirs(path.resolve('./lib-out/'), async (filepath, filename) => {
try {
console.log(chalk.greenBright(`> ${filename}: ${bytes(gzipSize.fileSync(filepath))}`))
} catch (err) {
console.error(err)
}
}, (err) => {
console.error(err)
fs.readFile(minPath, (minErr, minBundle) => {
throwOnError(minErr)
gzipSize(bundle, (gzipErr, gzipedSize) => {
throwOnError(gzipErr)
const output = [
'UMD bundle size:',
'────────────────',
`Minified: ${size(bundle.length)}`,
`Minified + gzip: ${size(gzipedSize)}`
].join('\n')
console.log(
boxen(output, {
padding: 1,
borderColor: 'yellow',
align: 'right'
})
)
function getStat(root, module) {
var modulePath = path.resolve(root, module);
try {
var stat = fs.statSync(modulePath);
return {
size: stat.size,
gzipped: gzipSize.sync(fs.readFileSync(modulePath).toString())
};
} catch (e) {
return {
size: 0,
gzipped: 0
};
}
}
function replaceFootprint (needle, relative, done) {
var file = path.resolve(relative);
var data = fs.readFileSync(file);
var size = gzipSize.sync(data);
var sizeHuman = prettyBytes(size).replace(/\s+/g, '');
var readmeFile = path.resolve('./README.md');
var readme = fs.readFileSync(readmeFile, { encoding: 'utf8' });
var output = readme.replace(needle, '$1' + sizeHuman + '$3');
fs.writeFile(readmeFile, output, { encoding: 'utf8'}, done);
}