-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.js
64 lines (56 loc) · 1.52 KB
/
preprocess.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
54
55
56
57
58
59
60
61
62
63
64
const svelte = require('svelte/compiler');
const sveltePreprocess = require('svelte-preprocess');
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const { sveltePreprocessConfig } = require('./svelte-preprocess.config');
const main = () => {
// source file paths
const srcPath = path.join(__dirname, 'src/components');
// read glob of files in directory
glob(path.join(srcPath, '**/*'), {}, (error, files) => {
// handling error
if (error) {
console.error('Unable to scan directory: ' + error);
return;
}
// listing all files using forEach
files
.filter((path) => !fs.lstatSync(path).isDirectory())
.forEach(async (file) => {
// load file
const sourceFile = fs.readFileSync(file, 'utf-8');
const distFile = file.replace('/src/components/', '/dist/');
// create directory and file
fs.mkdirSync(path.dirname(distFile), {
recursive: true,
});
// process .svelte file
if (file.endsWith('.svelte')) {
// run autopreprocessor
await parseSvelte(sourceFile, distFile);
}
// copy other files
else {
// copy static files
fs.copyFileSync(file, distFile);
}
});
});
};
const parseSvelte = async (source, destination) => {
try {
const item = await svelte.preprocess(
source,
sveltePreprocess(sveltePreprocessConfig),
{
filename: path.basename(destination),
}
);
// write compiled code to dist file
fs.writeFileSync(destination, item.code);
} catch (error) {
console.error(error);
}
};
main();