forked from parallax/jsPDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
150 lines (141 loc) · 3.63 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* eslint-disable no-empty */
/* eslint-disable no-console */
"use strict";
var fs = require("fs");
const rollup = require("rollup");
const rollupConfig = require("./rollup.config");
var uglify = require("uglify-js");
var execSync = require("child_process").execSync;
const args = process.argv
.slice(2)
.map(arg => arg.split("="))
.reduce((args, [value, key]) => {
args[value] = key;
return args;
}, {});
switch (args.type) {
case "worker":
bundle({
type: args.type,
distFolder: "dist",
config: "./build.worker.conf.js",
context: "global",
minify: args.minify || true,
format: "cjs",
filename: "jspdf.worker"
});
break;
case "node":
bundle({
type: args.type,
distFolder: "dist",
config: "./build.node.conf.js",
context: "global",
minify: args.minify || true,
format: "cjs",
filename: "jspdf.node"
});
break;
case "browser":
default:
bundle({
type: args.type,
distFolder: "dist",
config: "./build.browser.conf.js",
minify: args.minify || true,
format: "umd",
context: "window",
filename: "jspdf"
});
break;
}
function bundle(options) {
console.log(
"Start Bundling " +
options.distFolder +
"/" +
options.filename +
".debug.js"
);
const plugins =
options.type === "node" ? rollupConfig.nodePlugins : rollupConfig.plugins;
rollup
.rollup({
input: options.config,
context: options.context,
plugins: plugins
})
.then(bundle => {
return bundle.generate({
format: options.format,
name: "jsPDF"
});
})
.then(output => {
let code = output["output"][0].code;
code = code.replace(
/Permission\s+is\s+hereby\s+granted[\S\s]+?IN\s+THE\s+SOFTWARE\./,
"Licensed under the MIT License"
);
code = code.replace(
/Permission\s+is\s+hereby\s+granted[\S\s]+?IN\s+THE\s+SOFTWARE\./g,
""
);
code = renew(code);
code = code.replace(/}\)\);\s*$/, "return jsPDF;\n$&");
code = code + "\ntry {\nmodule.exports = jsPDF;\n}\ncatch (e) {}\n"; // inserted by build.js make require('jspdf.debug') work in node\n
fs.writeFileSync(
options.distFolder + "/" + options.filename + ".debug.js",
code
);
console.log(
"Finish Bundling " +
options.distFolder +
"/" +
options.filename +
".debug.js"
);
if (options.minify === true) {
console.log(
"Minifiying " +
options.distFolder +
"/" +
options.filename +
".debug.js to " +
options.filename +
".min.js"
);
var minified = uglify.minify(code, {
output: {
comments: /@preserve|@license|copyright/i
}
});
fs.writeFileSync(
options.distFolder + "/" + options.filename + ".min.js",
minified.code
);
}
})
.catch(err => {
console.error(err);
});
}
function renew(code) {
var date = new Date().toISOString();
var version = require("./package.json").version;
var commit = "00000000";
try {
commit = execSync("git rev-parse --short=10 HEAD")
.toString()
.trim();
} catch (e) {}
code = code.replace(
/jsPDF\.version\s*=\s*['"]0\.0\.0['"]/g,
`jsPDF.version = '${version}'`
);
code = code.replace(/\$\{versionID\}/g, version);
code = code.replace(/\$\{builtOn\}/g, date);
code = code.replace("${commitID}", commit);
code = code.replace(/1\.0\.0-trunk/, version + " " + date);
return code;
}