|
| 1 | +// @ts-check |
| 2 | +const fs = require('fs'); |
| 3 | +const zlib = require('zlib'); |
| 4 | +const { minify } = require('terser'); |
| 5 | +const pkg = require('../package.json'); |
| 6 | + |
| 7 | +/** |
| 8 | + * @param {string} file |
| 9 | + * @param {string} source |
| 10 | + */ |
| 11 | +function write(file, source) { |
| 12 | + let isModule = !source.startsWith('!function'); |
| 13 | + let result = minify(source, { |
| 14 | + module: isModule, |
| 15 | + compress: true, |
| 16 | + }); |
| 17 | + |
| 18 | + fs.writeFileSync(file, result.code); |
| 19 | + console.log('~> "%s" (%d b)', file, zlib.gzipSync(result.code).byteLength); |
| 20 | +} |
| 21 | + |
| 22 | +let input = fs.readFileSync('src/index.js', 'utf8'); |
| 23 | + |
| 24 | +// copy for ESM |
| 25 | +write(pkg.module, input); |
| 26 | + |
| 27 | +// transform ESM -> CJS exports |
| 28 | +write(pkg.main, input.replace('export function', 'function').replace( |
| 29 | + 'export default clsx;', |
| 30 | + 'module.exports = clsx;\n' |
| 31 | + + 'module.exports.clsx = clsx;' |
| 32 | +)); |
| 33 | + |
| 34 | +// transform ESM -> UMD exports |
| 35 | +input = input.replace('export function', 'function').replace('export default clsx;', 'return clsx.clsx=clsx, clsx;'); |
| 36 | +write(pkg.unpkg, '!function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):global.clsx=factory()}(this,function(){' + input + '});'); |
0 commit comments