1 Star 0 Fork 0

iorichina/pdf.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gulpfile.js 16.57 KB
一键复制 编辑 原始数据 按行查看 历史
Yury Delendik 提交于 2016-05-02 09:58 . Moves all testing into gulpfile.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
/* Copyright 2016 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* jshint node:true */
/* globals target */
'use strict';
var fs = require('fs');
var gulp = require('gulp');
var gutil = require('gulp-util');
var rimraf = require('rimraf');
var stream = require('stream');
var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var streamqueue = require('streamqueue');
var zip = require('gulp-zip');
var BUILD_DIR = 'build/';
var L10N_DIR = 'l10n/';
var TEST_DIR = 'test/';
var makeFile = require('./make.js');
var stripCommentHeaders = makeFile.stripCommentHeaders;
var builder = makeFile.builder;
var CONFIG_FILE = 'pdfjs.config';
var config = JSON.parse(fs.readFileSync(CONFIG_FILE).toString());
var DEFINES = {
PRODUCTION: true,
// The main build targets:
GENERIC: false,
FIREFOX: false,
MOZCENTRAL: false,
CHROME: false,
MINIFIED: false,
SINGLE_FILE: false,
COMPONENTS: false
};
function createStringSource(filename, content) {
var source = stream.Readable({ objectMode: true });
source._read = function () {
this.push(new gutil.File({
cwd: '',
base: '',
path: filename,
contents: new Buffer(content)
}));
this.push(null);
};
return source;
}
function stripUMDHeaders(content) {
var reg = new RegExp(
'if \\(typeof define === \'function\' && define.amd\\) \\{[^}]*' +
'\\} else if \\(typeof exports !== \'undefined\'\\) \\{[^}]*' +
'\\} else ', 'g');
return content.replace(reg, '');
}
function bundle(filename, outfilename, pathPrefix, initFiles, amdName, defines,
isMainFile, versionInfo) {
// Reading UMD headers and building loading orders of modules. The
// readDependencies returns AMD module names: removing 'pdfjs' prefix and
// adding '.js' extensions to the name.
var umd = require('./external/umdutils/verifier.js');
initFiles = initFiles.map(function (p) { return pathPrefix + p; });
var files = umd.readDependencies(initFiles).loadOrder.map(function (name) {
return pathPrefix + name.replace(/^[\w\-]+\//, '') + '.js';
});
var crlfchecker = require('./external/crlfchecker/crlfchecker.js');
crlfchecker.checkIfCrlfIsPresent(files);
var bundleContent = files.map(function (file) {
var content = fs.readFileSync(file);
// Prepend a newline because stripCommentHeaders only strips comments that
// follow a line feed. The file where bundleContent is inserted already
// contains a license header, so the header of bundleContent can be removed.
content = stripCommentHeaders('\n' + content);
// Removes AMD and CommonJS branches from UMD headers.
content = stripUMDHeaders(content);
return content;
}).join('');
var jsName = amdName.replace(/[\-_\.\/]\w/g, function (all) {
return all[1].toUpperCase();
});
// Avoiding double processing of the bundle file.
var templateContent = fs.readFileSync(filename).toString();
var tmpFile = outfilename + '.tmp';
fs.writeFileSync(tmpFile, templateContent.replace(
/\/\/#expand\s+__BUNDLE__\s*\n/, function (all) { return bundleContent; }));
bundleContent = null;
templateContent = null;
// This just preprocesses the empty pdf.js file, we don't actually want to
// preprocess everything yet since other build targets use this file.
builder.preprocess(tmpFile, outfilename,
builder.merge(defines, {
BUNDLE_VERSION: versionInfo.version,
BUNDLE_BUILD: versionInfo.commit,
BUNDLE_AMD_NAME: amdName,
BUNDLE_JS_NAME: jsName,
MAIN_FILE: isMainFile
}));
fs.unlinkSync(tmpFile);
}
function createBundle(defines) {
var versionJSON = JSON.parse(
fs.readFileSync(BUILD_DIR + 'version.json').toString());
console.log();
console.log('### Bundling files into pdf.js');
var mainFiles = [
'display/global.js'
];
var workerFiles = [
'core/worker.js'
];
var mainAMDName = 'pdfjs-dist/build/pdf';
var workerAMDName = 'pdfjs-dist/build/pdf.worker';
var mainOutputName = 'pdf.js';
var workerOutputName = 'pdf.worker.js';
// Extension does not need network.js file.
if (!defines.FIREFOX && !defines.MOZCENTRAL) {
workerFiles.push('core/network.js');
}
if (defines.SINGLE_FILE) {
// In singlefile mode, all of the src files will be bundled into
// the main pdf.js output.
mainFiles = mainFiles.concat(workerFiles);
workerFiles = null; // no need for worker file
mainAMDName = 'pdfjs-dist/build/pdf.combined';
workerAMDName = null;
mainOutputName = 'pdf.combined.js';
workerOutputName = null;
}
var state = 'mainfile';
var source = stream.Readable({ objectMode: true });
source._read = function () {
var tmpFile;
switch (state) {
case 'mainfile':
// 'buildnumber' shall create BUILD_DIR for us
tmpFile = BUILD_DIR + '~' + mainOutputName + '.tmp';
bundle('src/pdf.js', tmpFile, 'src/', mainFiles, mainAMDName,
defines, true, versionJSON);
this.push(new gutil.File({
cwd: '',
base: '',
path: mainOutputName,
contents: fs.readFileSync(tmpFile)
}));
fs.unlinkSync(tmpFile);
state = workerFiles ? 'workerfile' : 'stop';
break;
case 'workerfile':
// 'buildnumber' shall create BUILD_DIR for us
tmpFile = BUILD_DIR + '~' + workerOutputName + '.tmp';
bundle('src/pdf.js', tmpFile, 'src/', workerFiles, workerAMDName,
defines, false, versionJSON);
this.push(new gutil.File({
cwd: '',
base: '',
path: workerOutputName,
contents: fs.readFileSync(tmpFile)
}));
fs.unlinkSync(tmpFile);
state = 'stop';
break;
case 'stop':
this.push(null);
break;
}
};
return source;
}
function createWebBundle(defines) {
var versionJSON = JSON.parse(
fs.readFileSync(BUILD_DIR + 'version.json').toString());
var template, files, outputName, amdName;
if (defines.COMPONENTS) {
amdName = 'pdfjs-dist/web/pdf_viewer';
template = 'web/pdf_viewer.component.js';
files = [
'pdf_viewer.js',
'pdf_history.js',
'pdf_find_controller.js',
'download_manager.js'
];
outputName = 'pdf_viewer.js';
} else {
amdName = 'pdfjs-dist/web/viewer';
outputName = 'viewer.js';
template = 'web/viewer.js';
files = ['app.js'];
if (defines.FIREFOX || defines.MOZCENTRAL) {
files.push('firefoxcom.js');
} else if (defines.CHROME) {
files.push('chromecom.js', 'mozPrintCallback_polyfill.js');
} else if (defines.GENERIC) {
files.push('mozPrintCallback_polyfill.js');
}
}
var source = stream.Readable({ objectMode: true });
source._read = function () {
// 'buildnumber' shall create BUILD_DIR for us
var tmpFile = BUILD_DIR + '~' + outputName + '.tmp';
bundle(template, tmpFile, 'web/', files, amdName, defines, false,
versionJSON);
this.push(new gutil.File({
cwd: '',
base: '',
path: outputName,
contents: fs.readFileSync(tmpFile)
}));
fs.unlinkSync(tmpFile);
this.push(null);
};
return source;
}
function checkFile(path) {
try {
var stat = fs.lstatSync(path);
return stat.isFile();
} catch (e) {
return false;
}
}
function createTestSource(testsName) {
var source = stream.Readable({ objectMode: true });
source._read = function () {
console.log();
console.log('### Running ' + testsName + ' tests');
var PDF_TEST = process.env['PDF_TEST'] || 'test_manifest.json';
var PDF_BROWSERS = process.env['PDF_BROWSERS'] ||
'resources/browser_manifests/browser_manifest.json';
if (!checkFile('test/' + PDF_BROWSERS)) {
console.log('Browser manifest file test/' + PDF_BROWSERS +
' does not exist.');
console.log('Copy and adjust the example in ' +
'test/resources/browser_manifests.');
this.emit('error', new Error('Missing manifest file'));
return null;
}
var args = ['test.js'];
switch (testsName) {
case 'browser':
args.push('--reftest', '--manifestFile=' + PDF_TEST);
break;
case 'browser (no reftest)':
args.push('--manifestFile=' + PDF_TEST);
break;
case 'unit':
args.push('--unitTest');
break;
case 'font':
args.push('--fontTest');
break;
default:
this.emit('error', new Error('Unknown name: ' + testsName));
return null;
}
args.push('--browserManifestFile=' + PDF_BROWSERS);
var testProcess = spawn('node', args, {cwd: TEST_DIR, stdio: 'inherit'});
testProcess.on('close', function (code) {
source.push(null);
});
};
return source;
}
gulp.task('default', function() {
console.log('Available tasks:');
var tasks = Object.keys(gulp.tasks);
tasks.sort();
tasks.forEach(function (taskName) {
console.log(' ' + taskName);
});
});
gulp.task('buildnumber', function (done) {
console.log();
console.log('### Getting extension build number');
exec('git log --format=oneline ' + config.baseVersion + '..',
function (err, stdout, stderr) {
var buildNumber = 0;
if (!err) {
// Build number is the number of commits since base version
buildNumber = stdout ? stdout.match(/\n/g).length : 0;
}
console.log('Extension build number: ' + buildNumber);
var version = config.versionPrefix + buildNumber;
exec('git log --format="%h" -n 1', function (err, stdout, stderr) {
var buildCommit = '';
if (!err) {
buildCommit = stdout.replace('\n', '');
}
createStringSource('version.json', JSON.stringify({
version: version,
build: buildNumber,
commit: buildCommit
}, null, 2))
.pipe(gulp.dest(BUILD_DIR))
.on('end', done);
});
});
});
gulp.task('bundle-firefox', ['buildnumber'], function () {
var defines = builder.merge(DEFINES, {FIREFOX: true});
return streamqueue({ objectMode: true },
createBundle(defines), createWebBundle(defines))
.pipe(gulp.dest(BUILD_DIR));
});
gulp.task('bundle-mozcentral', ['buildnumber'], function () {
var defines = builder.merge(DEFINES, {MOZCENTRAL: true});
return streamqueue({ objectMode: true },
createBundle(defines), createWebBundle(defines))
.pipe(gulp.dest(BUILD_DIR));
});
gulp.task('bundle-chromium', ['buildnumber'], function () {
var defines = builder.merge(DEFINES, {CHROME: true});
return streamqueue({ objectMode: true },
createBundle(defines), createWebBundle(defines))
.pipe(gulp.dest(BUILD_DIR));
});
gulp.task('bundle-singlefile', ['buildnumber'], function () {
var defines = builder.merge(DEFINES, {SINGLE_FILE: true});
return createBundle(defines).pipe(gulp.dest(BUILD_DIR));
});
gulp.task('bundle-generic', ['buildnumber'], function () {
var defines = builder.merge(DEFINES, {GENERIC: true});
return streamqueue({ objectMode: true },
createBundle(defines), createWebBundle(defines))
.pipe(gulp.dest(BUILD_DIR));
});
gulp.task('bundle-minified', ['buildnumber'], function () {
var defines = builder.merge(DEFINES, {MINIFIED: true, GENERIC: true});
return streamqueue({ objectMode: true },
createBundle(defines), createWebBundle(defines))
.pipe(gulp.dest(BUILD_DIR));
});
gulp.task('bundle-components', ['buildnumber'], function () {
var defines = builder.merge(DEFINES, {COMPONENTS: true, GENERIC: true});
return createWebBundle(defines).pipe(gulp.dest(BUILD_DIR));
});
gulp.task('bundle', ['buildnumber'], function () {
return createBundle(DEFINES).pipe(gulp.dest(BUILD_DIR));
});
gulp.task('publish', ['generic'], function (done) {
var version = JSON.parse(
fs.readFileSync(BUILD_DIR + 'version.json').toString()).version;
config.stableVersion = config.betaVersion;
config.betaVersion = version;
createStringSource(CONFIG_FILE, JSON.stringify(config, null, 2))
.pipe(gulp.dest('.'))
.on('end', function () {
var targetName = 'pdfjs-' + version + '-dist.zip';
gulp.src(BUILD_DIR + 'generic/**')
.pipe(zip(targetName))
.pipe(gulp.dest(BUILD_DIR))
.on('end', function () {
console.log('Built distribution file: ' + targetName);
done();
});
});
});
gulp.task('test', function () {
return streamqueue({ objectMode: true },
createTestSource('unit'), createTestSource('browser'));
});
gulp.task('bottest', function () {
return streamqueue({ objectMode: true },
createTestSource('unit'), createTestSource('font'),
createTestSource('browser (no reftest)'));
});
gulp.task('browsertest', function () {
return createTestSource('browser');
});
gulp.task('browsertest-noreftest', function () {
return createTestSource('browser (no reftest)');
});
gulp.task('unittest', function () {
return createTestSource('unit');
});
gulp.task('fonttest', function () {
return createTestSource('font');
});
gulp.task('botmakeref', function (done) {
console.log();
console.log('### Creating reference images');
var PDF_BROWSERS = process.env['PDF_BROWSERS'] ||
'resources/browser_manifests/browser_manifest.json';
if (!checkFile('test/' + PDF_BROWSERS)) {
console.log('Browser manifest file test/' + PDF_BROWSERS +
' does not exist.');
console.log('Copy and adjust the example in ' +
'test/resources/browser_manifests.');
done(new Error('Missing manifest file'));
return;
}
var args = ['test.js', '--masterMode', '--noPrompts',
'--browserManifestFile=' + PDF_BROWSERS];
var testProcess = spawn('node', args, {cwd: TEST_DIR, stdio: 'inherit'});
testProcess.on('close', function (code) {
done();
});
});
gulp.task('lint', function (done) {
console.log();
console.log('### Linting JS files');
// Lint the Firefox specific *.jsm files.
var options = ['node_modules/jshint/bin/jshint', '--extra-ext', '.jsm', '.'];
var jshintProcess = spawn('node', options, {stdio: 'inherit'});
jshintProcess.on('close', function (code) {
if (code !== 0) {
done(new Error('jshint failed.'));
return;
}
console.log();
console.log('### Checking UMD dependencies');
var umd = require('./external/umdutils/verifier.js');
if (!umd.validateFiles({'pdfjs': './src', 'pdfjs-web': './web'})) {
done(new Error('UMD check failed.'));
return;
}
console.log('files checked, no errors found');
done();
});
});
gulp.task('server', function (done) {
console.log();
console.log('### Starting local server');
var WebServer = require('./test/webserver.js').WebServer;
var server = new WebServer();
server.port = 8888;
server.start();
});
gulp.task('clean', function(callback) {
console.log();
console.log('### Cleaning up project builds');
rimraf(BUILD_DIR, callback);
});
gulp.task('makefile', function () {
var makefileContent = 'help:\n\tgulp\n\n';
var targetsNames = [];
for (var i in target) {
makefileContent += i + ':\n\tgulp ' + i + '\n\n';
targetsNames.push(i);
}
makefileContent += '.PHONY: ' + targetsNames.join(' ') + '\n';
return createStringSource('Makefile', makefileContent)
.pipe(gulp.dest('.'));
});
gulp.task('importl10n', function(done) {
var locales = require('./external/importL10n/locales.js');
console.log();
console.log('### Importing translations from mozilla-aurora');
if (!fs.existsSync(L10N_DIR)) {
fs.mkdirSync(L10N_DIR);
}
locales.downloadL10n(L10N_DIR, done);
});
// Getting all shelljs registered tasks and register them with gulp
var gulpContext = false;
for (var taskName in global.target) {
if (taskName in gulp.tasks) {
continue;
}
var task = (function (shellJsTask) {
return function () {
gulpContext = true;
try {
shellJsTask.call(global.target);
} finally {
gulpContext = false;
}
};
})(global.target[taskName]);
gulp.task(taskName, task);
}
Object.keys(gulp.tasks).forEach(function (taskName) {
var oldTask = global.target[taskName] || function () {
gulp.run(taskName);
};
global.target[taskName] = function (args) {
// The require('shelljs/make') import in make.js will try to execute tasks
// listed in arguments, guarding with gulpContext
if (gulpContext) {
oldTask.call(global.target, args);
}
};
});
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/iorichina/pdf.js.git
git@gitee.com:iorichina/pdf.js.git
iorichina
pdf.js
pdf.js
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385