-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
44 lines (37 loc) · 1.7 KB
/
Copy pathbuild.js
File metadata and controls
44 lines (37 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env node
/*
* Zero-dependency build: turns the ESM source into ESM + UMD bundles and
* copies the stylesheet. No bundler required — the library has no imports.
*/
import { readFileSync, writeFileSync, mkdirSync, copyFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
const root = dirname(fileURLToPath(import.meta.url));
const src = join(root, 'src');
const dist = join(root, 'dist');
mkdirSync(dist, { recursive: true });
const esm = readFileSync(join(src, 'supersplit.js'), 'utf8');
// ---- ESM build (source is already ESM) ----
writeFileSync(join(dist, 'supersplit.esm.js'), esm);
// ---- UMD build: strip ESM export syntax, wrap as UMD returning SuperSplit ----
const body = esm
.replace(/^export\s+class\s+/m, 'class ')
.replace(/^export\s*\{[^}]*\};\s*$/m, '')
.replace(/^export\s+default\s+SuperSplit;\s*$/m, '');
const umd = `(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.SuperSplit = factory());
})(this, function () {
'use strict';
${body}
return SuperSplit;
});
`;
writeFileSync(join(dist, 'supersplit.umd.js'), umd);
// same UMD content, but a .cjs extension so Node's require() always treats it
// as CommonJS regardless of the package's "type": "module".
writeFileSync(join(dist, 'supersplit.cjs'), umd);
// ---- styles ----
copyFileSync(join(src, 'supersplit.css'), join(dist, 'supersplit.css'));
console.log('✓ built dist/supersplit.esm.js, dist/supersplit.umd.js, dist/supersplit.cjs, dist/supersplit.css');