-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
build-react.js
87 lines (74 loc) · 3.06 KB
/
build-react.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */
/* eslint no-console: "off" */
/* eslint global-require: "off" */
/* eslint no-param-reassign: ["off"] */
const exec = require('exec-sh');
const glob = require('glob');
const path = require('path');
const bannerReact = require('./banners/react.js');
const getOutput = require('./get-output.js');
const fs = require('./utils/fs-extra.js');
const removeDtsComments = (buildPath) => {
glob(
'*.js',
{ cwd: path.resolve(__dirname, `${buildPath}/react/components`) },
(err, componentFiles) => {
componentFiles.forEach((file) => {
let fileContent = fs.readFileSync(
path.resolve(__dirname, `${buildPath}/react/components`, file),
);
if (fileContent.indexOf('/* dts-imports') >= 0) {
const imports = fileContent.split('/* dts-imports')[1].split('*/')[0] || '';
fileContent = fileContent.replace(imports, '');
}
if (fileContent.indexOf('/* dts-props') >= 0) {
const props = fileContent.split('/* dts-props')[1].split('*/')[0] || '';
fileContent = fileContent.replace(props, '');
}
if (fileContent.indexOf('/* dts-extends') >= 0) {
const propsExtends = fileContent.split('/* dts-extends')[1].split('*/')[0] || '';
fileContent = fileContent.replace(propsExtends, '');
}
fileContent = fileContent
.replace('/* dts-imports*/\n', '')
.replace('/* dts-props*/\n', '')
.replace('/* dts-extends*/\n', '');
fs.writeFileSync(path.resolve(`${buildPath}/react/components`, file), fileContent);
});
},
);
};
// Build React
async function buildReact(cb) {
const buildPath = getOutput();
const files = fs.readdirSync('src/react/components').filter((file) => file.indexOf('.d.ts') < 0);
const componentImports = [];
const componentExports = [];
files.forEach((fileName) => {
const componentName = fileName
.replace('.jsx', '')
.split('-')
.map((word) => word[0].toUpperCase() + word.substr(1))
.join('');
const fileBase = fileName.replace('.jsx', '');
componentImports.push(`import ${componentName} from './components/${fileBase}.js';`);
componentExports.push(componentName);
});
const pluginContent = fs
.readFileSync('src/react/framework7-react.js', 'utf-8')
.replace('// IMPORT_COMPONENTS', componentImports.join('\n'))
.replace('// EXPORT_COMPONENTS', `export { ${componentExports.join(', ')} }`);
fs.writeFileSync(`${buildPath}/react/framework7-react.js`, pluginContent);
await exec.promise(
`npx cross-env MODULES=esm npx babel --config-file ./babel-react.config.js src/react --out-dir ${buildPath}/react --ignore "src/react/framework7-react.js","*.ts"`,
);
const esmContent = fs.readFileSync(`${buildPath}/react/framework7-react.js`, 'utf-8');
fs.writeFileSync(
`${buildPath}/react/framework7-react.js`,
`${bannerReact.trim()}\n${esmContent}`,
);
// remove dts-comments
removeDtsComments(buildPath);
if (cb) cb();
}
module.exports = buildReact;