forked from planttheidea/micro-memoize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
es-to-mjs.js
53 lines (40 loc) · 1.46 KB
/
es-to-mjs.js
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
45
46
47
48
49
50
51
52
53
const fs = require('fs');
const path = require('path');
const pkg = require('./package.json');
const BASE_PATH = __dirname;
const SOURCE_ENTRY = path.join(BASE_PATH, pkg.module);
const SOURCE_MAP = `${SOURCE_ENTRY}.map`;
const SOURCE_TYPES = path.join(BASE_PATH, 'index.d.ts');
const DESTINATION = 'mjs';
const DESTINATION_ENTRY = path.join(BASE_PATH, DESTINATION, 'index.mjs');
const DESTINATION_MAP = `${DESTINATION_ENTRY}.map`;
const DESTINATION_TYPES = path.join(BASE_PATH, DESTINATION, 'index.d.mts');
function getFilename(filename) {
return filename.replace(`${BASE_PATH}/`, '');
}
try {
if (!fs.existsSync(path.join(BASE_PATH, 'mjs'))) {
fs.mkdirSync(path.join(BASE_PATH, 'mjs'));
}
fs.copyFileSync(SOURCE_ENTRY, DESTINATION_ENTRY);
const contents = fs
.readFileSync(DESTINATION_ENTRY, { encoding: 'utf8' })
.replace(/\/\/# sourceMappingURL=(.*)/, (match, value) =>
match.replace(value, 'index.mjs.map'),
);
fs.writeFileSync(DESTINATION_ENTRY, contents, { encoding: 'utf8' });
console.log(
`Copied ${getFilename(SOURCE_ENTRY)} to ${getFilename(DESTINATION_ENTRY)}`,
);
fs.copyFileSync(SOURCE_MAP, DESTINATION_MAP);
console.log(
`Copied ${getFilename(SOURCE_MAP)} to ${getFilename(DESTINATION_MAP)}`,
);
fs.copyFileSync(SOURCE_TYPES, DESTINATION_TYPES);
console.log(
`Copied ${getFilename(SOURCE_TYPES)} to ${getFilename(DESTINATION_TYPES)}`,
);
} catch (error) {
console.error(error);
process.exit(1);
}