UNPKG

1.04 kBJavaScriptView Raw
1const babel = require('babel-core');
2const { existsSync, mkdirSync, writeFileSync, readdirSync, lstatSync } = require('fs');
3
4const sourceDir = './src';
5const destinationDir = './dist';
6const jsxFiles = [];
7
8readdirSync(sourceDir).forEach(path => {
9 if (lstatSync(`${sourceDir}/${path}`).isDirectory()) {
10 if (path === 'node_modules' || path === 'test') return;
11 readdirSync(`${sourceDir}/${path}`).forEach(file => {
12 if ( ! file.endsWith('.jsx')) return;
13 jsxFiles.push({
14 path,
15 file,
16 });
17 });
18 }
19});
20
21jsxFiles.push({
22 path: '',
23 file: 'index.jsx',
24});
25
26if (!existsSync(destinationDir)) mkdirSync(destinationDir);
27
28jsxFiles.forEach(jsxFile => {
29 const { path, file } = jsxFile;
30
31 const jsContent = babel.transformFileSync(`${sourceDir}/${path}/${file}`);
32
33 if (!existsSync(`${destinationDir}/${path}`)) mkdirSync(`${destinationDir}/${path}`);
34
35 writeFileSync(
36 `${destinationDir}/${path}/${file.replace('.jsx', '.js')}`,
37 jsContent.code.replace('.scss', '.css').toString()
38 );
39});