Skip to content

Commit 3712966

Browse files
committedJul 5, 2022
chore: build CJS & UMD files manually;
- mixed exports (named + default) broke old bundt... because it's discouraged - closes #50
1 parent 89407de commit 3712966

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed
 

‎bin/index.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 + '});');

‎package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"node": ">=6"
1818
},
1919
"scripts": {
20-
"build": "bundt",
20+
"build": "node bin",
2121
"test": "uvu -r esm test"
2222
},
2323
"files": [
@@ -30,8 +30,8 @@
3030
"classnames"
3131
],
3232
"devDependencies": {
33-
"bundt": "1.0.1",
3433
"esm": "3.2.25",
35-
"uvu": "0.5.4"
34+
"terser": "4.8.0",
35+
"uvu": "0.5.6"
3636
}
3737
}

0 commit comments

Comments
 (0)
Please sign in to comment.