1 | const babel = require('babel-core');
|
2 | const { existsSync, mkdirSync, writeFileSync, readdirSync, lstatSync } = require('fs');
|
3 |
|
4 | const sourceDir = './src';
|
5 | const destinationDir = './dist';
|
6 | const jsxFiles = [];
|
7 |
|
8 | readdirSync(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 |
|
21 | jsxFiles.push({
|
22 | path: '',
|
23 | file: 'index.jsx',
|
24 | });
|
25 |
|
26 | if (!existsSync(destinationDir)) mkdirSync(destinationDir);
|
27 |
|
28 | jsxFiles.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 | });
|