forked from youzan/vant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-components.js
80 lines (66 loc) · 1.92 KB
/
build-components.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
/**
* Compile components
*/
const fs = require('fs-extra');
const path = require('path');
const babel = require('@babel/core');
const compiler = require('@vant/compiler');
const markdownVetur = require('markdown-vetur');
const esDir = path.join(__dirname, '../es');
const libDir = path.join(__dirname, '../lib');
const srcDir = path.join(__dirname, '../packages');
const veturDir = path.join(__dirname, '../vetur');
const compilerOption = {
babel: {
configFile: path.join(__dirname, '../babel.config.js')
}
};
const isDir = dir => fs.lstatSync(dir).isDirectory();
const isJs = path => /\.js$/.test(path);
const isSfc = path => /\.vue$/.test(path);
const isCode = path => !/(demo|test|\.md)$/.test(path);
function compile(dir) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const filePath = path.join(dir, file);
// remove unnecessary files
if (!isCode(file)) {
return fs.removeSync(filePath);
}
// scan dir
if (isDir(filePath)) {
return compile(filePath);
}
// compile sfc
if (isSfc(file)) {
const source = fs.readFileSync(filePath, 'utf-8');
fs.removeSync(filePath);
const jsPath = filePath.replace('.vue', '.js');
const vuePath = filePath + '.js';
const output = fs.existsSync(jsPath) ? vuePath : jsPath;
return fs.outputFileSync(output, compiler(source, compilerOption).js);
}
// compile js
if (isJs(file)) {
const { code } = babel.transformFileSync(filePath, compilerOption.babel);
fs.outputFileSync(filePath, code);
}
});
}
// clear dir
fs.emptyDirSync(esDir);
fs.emptyDirSync(libDir);
// compile es dir
fs.copySync(srcDir, esDir);
compile(esDir);
// compile lib dir
process.env.BABEL_MODULE = 'commonjs';
fs.copySync(srcDir, libDir);
compile(libDir);
// generate vetur tags & attributes
markdownVetur.parseAndWrite({
path: srcDir,
test: /zh-CN\.md/,
tagPrefix: 'van-',
outputDir: veturDir
});