diff --git a/bin/index.js b/bin/index.js new file mode 100644 index 0000000..6c9c079 --- /dev/null +++ b/bin/index.js @@ -0,0 +1,36 @@ +// @ts-check +const fs = require('fs'); +const zlib = require('zlib'); +const { minify } = require('terser'); +const pkg = require('../package.json'); + +/** + * @param {string} file + * @param {string} source + */ +function write(file, source) { + let isModule = !source.startsWith('!function'); + let result = minify(source, { + module: isModule, + compress: true, + }); + + fs.writeFileSync(file, result.code); + console.log('~> "%s" (%d b)', file, zlib.gzipSync(result.code).byteLength); +} + +let input = fs.readFileSync('src/index.js', 'utf8'); + +// copy for ESM +write(pkg.module, input); + +// transform ESM -> CJS exports +write(pkg.main, input.replace('export function', 'function').replace( + 'export default clsx;', + 'module.exports = clsx;\n' + + 'module.exports.clsx = clsx;' +)); + +// transform ESM -> UMD exports +input = input.replace('export function', 'function').replace('export default clsx;', 'return clsx.clsx=clsx, clsx;'); +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 + '});'); diff --git a/package.json b/package.json index 6721305..e75cfad 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "node": ">=6" }, "scripts": { - "build": "bundt", + "build": "node bin", "test": "uvu -r esm test" }, "files": [ @@ -30,8 +30,8 @@ "classnames" ], "devDependencies": { - "bundt": "1.0.1", "esm": "3.2.25", - "uvu": "0.5.4" + "terser": "4.8.0", + "uvu": "0.5.6" } }