Skip to content

Commit

Permalink
chore: build CJS & UMD files manually;
Browse files Browse the repository at this point in the history
- mixed exports (named + default) broke old bundt... because it's discouraged
- closes #50
  • Loading branch information
lukeed committed Jul 5, 2022
1 parent 89407de commit 3712966
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
36 changes: 36 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -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 + '});');
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"node": ">=6"
},
"scripts": {
"build": "bundt",
"build": "node bin",
"test": "uvu -r esm test"
},
"files": [
Expand All @@ -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"
}
}

0 comments on commit 3712966

Please sign in to comment.