-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
113 lines (106 loc) · 3.32 KB
/
build.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const fs=require(`fs`);
const UglifyJS = require(`uglify-js`);
String.prototype.replaceAll=function(org,tgt){
return this.split(org).join(tgt);
}
//创建文件夹
try{
fs.mkdirSync(`dist`);
fs.mkdirSync(`npm`);
}catch(e){}
// 读取VERSION.md获取版本号
let versionMd=fs.readFileSync(`VERSION.md`,`utf-8`);
let versionSplit=versionMd.split(`\n`);
let version=``;
for(let v of versionSplit){
if(v.includes(`Version: `)){
version=v.split(` `)[2];
break;
}
}
console.log(`Build version: ${version}`);
// 读取npm/package.json
let packageData={};
try{
packageData=JSON.parse(fs.readFileSync(`npm/package.json`,`utf-8`));
}catch(e){}
if(version == packageData.version){
console.warn(`Version ${version} is same as package.json!`);
}
// 压缩非模块脚本
const uglifyList=[
`jquery.extensions.dom`,
`html.dom`,
`vue.extensions.dom`,
`react.extensions.dom`,
]
for(let s of uglifyList){
try{
let origin=fs.readFileSync(`${s}.js`,`utf-8`);
let target=UglifyJS.minify(origin);
if (target.error) {
console.error(`Error in minifying JS file: ${target.error}`);
} else {
fs.writeFileSync(`dist/${s}.min.js`, target.code, {encoding:`utf-8`});
console.log(`Compression ${s} completed successfully!`);
}
}catch(e){
console.error(`An error occurred while compressing the JS file: ${e}`);
}
}
// 处理模块脚本
const moduleOriginTag=`/* --- FOR MODULE --- */`;
const moduleTargetTag=`/* MODULE INSERT */`;
const moduleList=[
{origin:`react.extensions.dom`,target:`react.extensions.dom.module`,output:`react-extensions-dom-module`},
]
for(let m of moduleList){
try{
let origin=fs.readFileSync(`${m.origin}.js`,`utf-8`);
let target=fs.readFileSync(`${m.target}.m`,`utf-8`);
let originCode=origin.split(moduleOriginTag)[1];
let targetCode=target.replaceAll(moduleTargetTag, originCode).trim();
fs.writeFileSync(`dist/${m.output}.js`, targetCode, {encoding:`utf-8`});
fs.writeFileSync(`npm/index.js`, targetCode, {encoding:`utf-8`});
console.log(`Output module ${m.output} completed successfully!`);
}catch(e){
console.error(`An error occurred while output the JS module file: ${e}`);
}
}
// 创建NPM包
// 创建package.json
const packageJson={
name: `react-extensions-dom`,
version: version,
description: `A plugin for React, use object instead of JSX to manage React elements.`,
main: `index.js`,
keywords:[`react`,`extensions`,`dom`,`jsx`],
scripts: {
test: `echo "Error: no test specified" && exit 1`,
},
repository: {
type: `git`,
url: `git+https://github.com/road0001/JQueryDOM.git`,
},
author: ``,
license: `MIT`,
bugs: {
url: `https://github.com/road0001/JQueryDOM/issues`,
},
homepage: `https://github.com/road0001/JQueryDOM#readme`,
}
// 写入package.json
try{
fs.writeFileSync(`npm/package.json`, JSON.stringify(packageJson, null, `\t`), {encoding:`utf-8`});
console.log(`Output package.json completed successfully!`);
}catch(e){
console.error(`An error occurred while output the package.json file: ${e}`);
}
// 复制README.react.md
try {
fs.copyFileSync(`README.react.md`, `npm/README.md`);
console.log(`Copy README.react.md completed successfully!`);
} catch (err) {
console.error(`An error occurred while copy the README.react.md file: ${e}`);
}
console.log(`Build completed!`);