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 ( / t h i s , f u n c t i o n / g, 'jsPDF, function' ) ;
19+ code = code . replace ( / r e q u i r e \( ' b u f f e r ' \) / 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 = / \/ \* r o l l u p - k e e p e r - s t a r t \* \/ .* \/ \* r o l l u p - k e e p e r - e n d \* \/ / 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 ( / P e r m i s s i o n \s + i s \s + h e r e b y \s + g r a n t e d [ \S \s ] + ?I N \s + T H E \s + S O F T W A R E \. / , 'Licensed under the MIT License' ) ;
72+ code = code . replace ( / P e r m i s s i o n \s + i s \s + h e r e b y \s + g r a n t e d [ \S \s ] + ?I N \s + T H E \s + S O F T W A R E \. / g, '' ) ;
73+ fs . writeFileSync ( paths . debug , renew ( code ) ) ;
74+
75+ var minified = uglify . minify ( code , { fromString : true , output : { comments : / @ p r e s e r v e | @ l i c e n s e | c o p y r i g h t / 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 - t r u n k / , version + ' ' + date + ':' + whoami ) ;
91+
92+ return code ;
93+ }
0 commit comments