Skip to content

Commit f895888

Browse files
committed
Merge pull request #717 from simonbengtsson/build-updates
Build updates 2
2 parents b09fd63 + c3dc391 commit f895888

File tree

5 files changed

+150
-14
lines changed

5 files changed

+150
-14
lines changed

build.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
var fs = require('fs');
2+
var rollup = require('rollup');
3+
var uglify = require('uglify-js');
4+
var babel = require('rollup-plugin-babel');
5+
var execSync = require('child_process').execSync;
6+
7+
bundle({
8+
minified: 'dist/jspdf.min.js',
9+
debug: 'dist/jspdf.debug.js'
10+
});
11+
12+
// Monkey patching adler32 and filesaver
13+
function monkeyPatch() {
14+
return {
15+
transform: function (code, id) {
16+
var file = id.split('/').pop();
17+
if (file === 'adler32cs.js') {
18+
code = code.replace(/this, function/g, 'jsPDF, function');
19+
code = code.replace(/require\('buffer'\)/g, '{}');
20+
}
21+
return code;
22+
}
23+
}
24+
}
25+
26+
// Rollup removes local variables unless used within a module.
27+
// This plugin makes sure specified local variables are preserved
28+
// and kept local. This plugin wouldn't be necessary if es2015
29+
// modules would be used.
30+
function rawjs(opts) {
31+
opts = opts || {};
32+
return {
33+
transform: function (code, id) {
34+
var variable = opts[id.split('/').pop()];
35+
if (!variable) return code;
36+
37+
var keepStr = '/*rollup-keeper-start*/window.tmp=' + variable + ';/*rollup-keeper-end*/';
38+
return code + keepStr;
39+
},
40+
transformBundle: function (code) {
41+
for (var file in opts) {
42+
var r = new RegExp(opts[file] + '\\$\\d+', 'g');
43+
code = code.replace(r, opts[file]);
44+
}
45+
var re = /\/\*rollup-keeper-start\*\/.*\/\*rollup-keeper-end\*\//g;
46+
return code.replace(re, '');
47+
}
48+
}
49+
}
50+
51+
function bundle(paths) {
52+
rollup.rollup({
53+
entry: './main.js',
54+
plugins: [
55+
monkeyPatch(),
56+
rawjs({
57+
'jspdf.js': 'jsPDF',
58+
'filesaver.tmp.js': 'saveAs',
59+
'deflate.js': 'Deflater',
60+
'zlib.js': 'FlateStream',
61+
'css_colors.js': 'CssColors',
62+
'html2pdf.js': 'html2pdf'
63+
}),
64+
babel({
65+
presets: ['es2015-rollup'],
66+
exclude: ['node_modules/**', 'libs/**']
67+
})
68+
]
69+
}).then(function (bundle) {
70+
var code = bundle.generate({format: 'umd', moduleName: 'jspdf'}).code;
71+
code = code.replace(/Permission\s+is\s+hereby\s+granted[\S\s]+?IN\s+THE\s+SOFTWARE\./, 'Licensed under the MIT License');
72+
code = code.replace(/Permission\s+is\s+hereby\s+granted[\S\s]+?IN\s+THE\s+SOFTWARE\./g, '');
73+
fs.writeFileSync(paths.debug, renew(code));
74+
75+
var minified = uglify.minify(code, {fromString: true, output: {comments: /@preserve|@license|copyright/i}});
76+
fs.writeFileSync(paths.minified, renew(minified.code));
77+
}).catch(function (err) {
78+
console.error(err);
79+
});
80+
}
81+
82+
function renew(code) {
83+
var date = new Date().toISOString();
84+
var version = require('./package.json').version;
85+
var whoami = execSync('whoami').toString().trim();
86+
var commit = execSync('git rev-parse --short=10 HEAD').toString().trim();
87+
88+
code = code.replace('${versionID}', version + ' Built on ' + date);
89+
code = code.replace('${commitID}', commit);
90+
code = code.replace(/1\.0\.0-trunk/, version + ' ' + date + ':' + whoami);
91+
92+
return code;
93+
}

main.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import './jspdf';
2+
3+
import './plugins/acroform';
4+
import './plugins/addhtml';
5+
import './plugins/addimage';
6+
import './plugins/annotations';
7+
import './plugins/autoprint';
8+
import './plugins/canvas';
9+
import './plugins/cell';
10+
import './plugins/context2d';
11+
import './plugins/from_html';
12+
import './plugins/javascript';
13+
import './plugins/outline';
14+
import './plugins/png_support';
15+
import './plugins/prevent_scale_to_fit';
16+
import './plugins/split_text_to_size';
17+
import './plugins/standard_fonts_metrics';
18+
import './plugins/svg';
19+
import './plugins/total_pages';
20+
21+
import './node_modules/cf-blob.js/Blob.js';
22+
import './node_modules/filesaver.js/FileSaver.js';
23+
import './node_modules/adler32cs/adler32cs.js';
24+
import './libs/css_colors.js';
25+
import './libs/deflate.js';
26+
import './libs/html2canvas/dist/html2canvas.js';
27+
import './libs/png_support/png.js';
28+
import './libs/png_support/zlib.js';
29+
import './libs/polyfill.js';
30+
31+
export default jsPDF;

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@
1616
"type": "git",
1717
"url": "https://github.com/MrRio/jsPDF.git"
1818
},
19-
"dependencies": {},
20-
"devDependencies": {
19+
"dependencies": {
2120
"adler32cs": "github:chick307/adler32cs.js",
2221
"cf-blob.js": "0.0.1",
23-
"filesaver.js": "github:andyinabox/FileSaver.js",
22+
"filesaver.js": "github:andyinabox/FileSaver.js"
23+
},
24+
"devDependencies": {
25+
"babel-preset-es2015-rollup": "^1.1.1",
2426
"local-web-server": "^0.5.19",
27+
"rollup": "^0.25.4",
28+
"rollup-plugin-babel": "^2.4.0",
2529
"uglify-js": "^2.6.2"
2630
},
2731
"scripts": {
2832
"start": "ws",
29-
"build": "npm install && bower install && ./build.sh",
33+
"build": "npm install && bower install && node build.js",
3034
"version": "npm run build && git add -A dist"
3135
}
3236
}

plugins/acroform.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
* http://opensource.org/licenses/mit-license
77
*/
88

9-
(AcroForm = function (jsPDFAPI) {
9+
(window.AcroForm = function (jsPDFAPI) {
1010
'use strict';
11+
12+
var AcroForm = window.AcroForm;
1113

1214
AcroForm.scale = function (x) {
1315
return (x * (acroformPlugin.internal.scaleFactor / 1));// 1 = (96 / 72)
@@ -217,10 +219,10 @@
217219
var key = i;
218220
var form = fieldArray[i];
219221
// Start Writing the Object
220-
this.internal.newObjectDeferredBegin(form.objId);
222+
this.internal.newObjectDeferredBegin(form && form.objId);
221223

222224
var content = "";
223-
content += (form.getString());
225+
content += form ? form.getString() : '';
224226
this.internal.out(content);
225227

226228
delete fieldArray[key];
@@ -402,6 +404,8 @@
402404
};
403405
})(jsPDF.API);
404406

407+
var AcroForm = window.AcroForm;
408+
405409
AcroForm.internal = {};
406410

407411
AcroForm.createFormXObject = function (formObject) {
@@ -757,10 +761,10 @@ AcroForm.internal.toPdfString = function (string) {
757761
string = string || "";
758762

759763
// put Bracket at the Beginning of the String
760-
if (String.indexOf('(', 0) !== 0) {
764+
if (string.indexOf('(') !== 0) {
761765
string = '(' + string;
762766
}
763-
767+
764768
if (string.substring(string.length - 1) != ')') {
765769
string += '(';
766770
}
@@ -1043,7 +1047,8 @@ AcroForm.Field = function () {
10431047

10441048
Object.defineProperty(this, 'hasAppearanceStream', {
10451049
enumerable: false,
1046-
configurable: true
1050+
configurable: true,
1051+
writable: true
10471052
});
10481053
};
10491054
AcroForm.Field.FieldNum = 0;
@@ -1088,9 +1093,11 @@ AcroForm.ChoiceField = function () {
10881093
configurable: false
10891094
});
10901095
this.hasAppearanceStream = true;
1091-
this.V.get = function () {
1092-
AcroForm.internal.toPdfString();
1093-
};
1096+
Object.defineProperty(this, 'V', {
1097+
get: function() {
1098+
AcroForm.internal.toPdfString();
1099+
}
1100+
});
10941101
};
10951102
AcroForm.internal.inherit(AcroForm.ChoiceField, AcroForm.Field);
10961103
window["ChoiceField"] = AcroForm.ChoiceField;

plugins/from_html.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
DrillForContent,
3434
FontNameDB,
3535
FontStyleMap,
36+
TextAlignMap,
3637
FontWeightMap,
3738
FloatMap,
3839
ClearMap,
@@ -943,7 +944,7 @@
943944
}
944945
//if we have to move the cursor to adapt the indent
945946
var indentMove = 0;
946-
var indentMore = 0;
947+
var wantedIndent = 0;
947948
//if a margin was added (by e.g. a text-alignment), move the cursor
948949
if (line[0][1]["margin-left"] !== undefined && line[0][1]["margin-left"] > 0) {
949950
wantedIndent = this.pdf.internal.getCoordinateString(line[0][1]["margin-left"]);

0 commit comments

Comments
 (0)