Skip to content

Commit 70beb11

Browse files
committed
Merge pull request mozilla#1953 from brendandahl/b2g
Add B2G build and new preprocessor.
2 parents a0d7f02 + 0bf5834 commit 70beb11

File tree

11 files changed

+547
-303
lines changed

11 files changed

+547
-303
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ You can also view all the test pdf files on the right side serving
6161

6262
+ http://localhost:8888/test/pdfs/?frame
6363

64-
### Building pdf.js
64+
### Building pdf.js.
6565

66-
In order to bundle all `src/` files into a final `pdf.js`, issue:
66+
In order to bundle all `src/` files into a final `pdf.js` and build the generic viewer, issue:
6767

68-
$ node make bundle
68+
$ node make generic
6969

70-
This will generate the file `build/pdf.js` that can be included in your final project. (WARNING: That's a large file! Consider minifying it).
70+
This will generate the file `build/generic/build/pdf.js` that can be included in your final project. The pdf.js file is large and should be minified for production. Also, if you would like to support more browsers than firefox you'll also need to include `compatibility.js` from `build/generic/web/`.
7171

7272

7373
# Learning

external/builder/builder.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
require('../shelljs/make');
2+
var fs = require('fs'),
3+
path = require('path'),
4+
vm = require('vm');
5+
6+
/**
7+
* A simple preprocessor that is based on the firefox preprocessor
8+
* see (https://developer.mozilla.org/en/Build/Text_Preprocessor). The main
9+
* difference is that this supports a subset of the commands and it supports
10+
* preproccesor commands in html style comments.
11+
* Currently Supported commands:
12+
* - if
13+
* - else
14+
* - endif
15+
* - include
16+
* - expand
17+
*/
18+
function preprocess(inFilename, outFilename, defines) {
19+
// TODO make this really read line by line.
20+
var lines = fs.readFileSync(inFilename).toString().split('\n');
21+
var totalLines = lines.length;
22+
var out = '';
23+
var i = 0;
24+
function readLine() {
25+
if (i < totalLines) {
26+
return lines[i++];
27+
}
28+
return null;
29+
}
30+
var writeLine = typeof outFilename === 'function' ? outFilename : function(line) {
31+
out += line + '\n';
32+
}
33+
function include(file) {
34+
var realPath = fs.realpathSync(inFilename);
35+
var dir = path.dirname(realPath);
36+
preprocess(path.join(dir, file), writeLine, defines);
37+
}
38+
function expand(line) {
39+
line = line.replace(/__[\w]+__/g, function(variable) {
40+
variable = variable.substring(2, variable.length - 2);
41+
if (variable in defines) {
42+
return defines[variable];
43+
}
44+
return '';
45+
});
46+
writeLine(line);
47+
}
48+
49+
var s, state = 0, stack = [];
50+
var control = /^(?:\/\/|<!--)\s*#(if|else|endif|expand|include)(?:\s+(.*?)(?:-->)?$)?/;
51+
var lineNumber = 0;
52+
while ((s = readLine()) !== null) {
53+
++lineNumber;
54+
var m = control.exec(s);
55+
if (m) {
56+
switch (m[1]) {
57+
case 'if':
58+
stack.push(state);
59+
try {
60+
state = vm.runInNewContext(m[2], defines) ? 3 : 1;
61+
} catch (e) {
62+
console.error('Could not evalute line \'' + m[2] + '\' at ' +
63+
fs.realpathSync(inFilename) + ':' + lineNumber);
64+
throw e;
65+
}
66+
break;
67+
case 'else':
68+
state = state === 1 ? 3 : 2;
69+
break;
70+
case 'endif':
71+
state = stack.pop();
72+
break;
73+
case 'expand':
74+
if (state === 0 || state === 3)
75+
expand(m[2]);
76+
break;
77+
case 'include':
78+
if (state === 0 || state === 3)
79+
include(m[2]);
80+
break;
81+
}
82+
} else {
83+
if (state === 0) {
84+
writeLine(s);
85+
} else if(state === 3) {
86+
writeLine(s.replace(/^\/\/|^<!--|-->/g, " "));
87+
}
88+
}
89+
}
90+
if (state !== 0 || stack.length !== 0)
91+
throw new Error('Missing endif in preprocessor.');
92+
if (typeof outFilename !== 'function')
93+
fs.writeFileSync(outFilename, out);
94+
}
95+
exports.preprocess = preprocess;
96+
97+
/**
98+
* Simplifies common build steps.
99+
* @param setup
100+
* .defines defines for preprocessors
101+
* .copy array of arrays of source and destination pairs of files to copy
102+
* .preprocess array of arrays of source and destination pairs of files
103+
* run through preprocessor
104+
*/
105+
function build(setup) {
106+
var defines = setup.defines;
107+
108+
setup.copy.forEach(function(option) {
109+
var source = option[0];
110+
var destination = option[1];
111+
cp('-R', source, destination);
112+
});
113+
114+
setup.preprocess.forEach(function(option) {
115+
var sources = option[0];
116+
var destination = option[1];
117+
118+
sources = ls('-R', sources);
119+
sources.forEach(function(source) {
120+
// ??? Warn if the source is wildcard and dest is file?
121+
var destWithFolder = destination;
122+
if (test('-d', destination))
123+
destWithFolder += '/' + path.basename(source);
124+
preprocess(source, destWithFolder, defines);
125+
});
126+
});
127+
}
128+
exports.build = build;
129+
130+
/**
131+
* Merge two defines arrays. Values in the second param will override values in
132+
* the first.
133+
*/
134+
function merge(defaults, defines) {
135+
var ret = {};
136+
for (var key in defaults)
137+
ret[key] = defaults[key];
138+
for (key in defines)
139+
ret[key] = defines[key];
140+
return ret;
141+
}
142+
exports.merge = merge;

0 commit comments

Comments
 (0)