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))
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;
}
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);
}
async function measure(module: string) {
const path = dest('bundles', `${module}.umd.js`);
const file = await readFile(path);
const gzip = prettySize(gzipSync(file), true);
const size = prettySize(file.byteLength, true);
return { size, gzip };
}
.map(asset => {
var fileContents = fs.readFileSync(paths.appBuild + '/' + asset.name);
var size = gzipSize(fileContents);
var previousSize = previousSizeMap[removeFileNameHash(asset.name)];
var difference = getDifferenceLabel(size, previousSize);
return {
folder: path.join('build', path.dirname(asset.name)),
name: path.basename(asset.name),
size: size,
sizeLabel: filesize(size) + (difference ? ' (' + difference + ')' : '')
};
});
assets.sort((a, b) => b.size - a.size);
ongenerate(bundle, obj) {
const size = Buffer.byteLength(obj.code)
const gzipSize = gzip.sync(obj.code)
options.getSize(size, gzipSize)
}
}
.reduce((memo, fileName) => {
var contents = fs.readFileSync(fileName);
var key = removeFileNameHash(fileName);
memo[key] = gzipSize(contents);
return memo;
}, {});