-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpre-build.js
50 lines (45 loc) · 1.24 KB
/
pre-build.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
import fs from 'fs';
import path from 'path';
const srcDirectory = 'src';
/**
* Returns a list of files in a folder (with recursive traversal).
*/
function getFiles(dir) {
const results = [];
const files = fs.readdirSync(dir, {
recursive: true,
});
for (const filePath of files) {
const fullPath = path.join(dir, filePath);
const stat = fs.statSync(fullPath);
if (stat.isFile()) {
results.push(fullPath);
}
}
return results;
}
/**
* Normalize imports to comply with ES-module standards.
*/
function normalizeImports() {
console.info('Import normalization...');
const regexpForImport = /(from\s+)(["'])(?!.*\.js)(\.?\.\/.*)(["'])/gm;
const stringToReplace = '$1$2$3.js$4';
const files = getFiles(srcDirectory);
for (const file of files) {
const currentContent = fs.readFileSync(file, {
encoding: 'utf8',
});
const newContent = currentContent.replace(regexpForImport, stringToReplace);
fs.writeFileSync(file, newContent, {
encoding: 'utf8',
});
}
console.info('Import normalization has been completed.');
};
function runPreBuildTasks() {
console.info('Starting pre-build tasks...');
normalizeImports();
console.info('Pre-build tasks are completed.');
}
runPreBuildTasks();