3 Star 17 Fork 4

tomgu/pdf.js-4.0.379

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gulpfile.mjs 69.07 KB
一键复制 编辑 原始数据 按行查看 历史
tomgu 提交于 2024-01-22 16:02 . 增加对跨域的处理
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492
/* 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.
*/
/* eslint-env node */
import { exec, spawn, spawnSync } from "child_process";
import autoprefixer from "autoprefixer";
import babel from "@babel/core";
import crypto from "crypto";
import { fileURLToPath } from "url";
import fs from "fs";
import gulp from "gulp";
import merge from "merge-stream";
import { mkdirp } from "mkdirp";
import path from "path";
import postcss from "gulp-postcss";
import postcssDarkThemeClass from "postcss-dark-theme-class";
import postcssDirPseudoClass from "postcss-dir-pseudo-class";
import postcssDiscardComments from "postcss-discard-comments";
import postcssNesting from "postcss-nesting";
import { preprocess } from "./external/builder/builder.mjs";
import { preprocessPDFJSCode } from "./external/builder/preprocessor2.mjs";
import rename from "gulp-rename";
import replace from "gulp-replace";
import rimraf from "rimraf";
import stream from "stream";
import streamqueue from "streamqueue";
import through from "through2";
import Vinyl from "vinyl";
import webpack2 from "webpack";
import webpackStream from "webpack-stream";
import zip from "gulp-zip";
// eslint-disable-next-line sort-imports
import connect from "gulp-connect";
import { createProxyMiddleware } from "http-proxy-middleware";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const BUILD_DIR = "build/";
const L10N_DIR = "l10n/";
const TEST_DIR = "test/";
const EXTENSION_SRC_DIR = "extensions/";
const BASELINE_DIR = BUILD_DIR + "baseline/";
const MOZCENTRAL_BASELINE_DIR = BUILD_DIR + "mozcentral.baseline/";
const GENERIC_DIR = BUILD_DIR + "generic/";
const GENERIC_LEGACY_DIR = BUILD_DIR + "generic-legacy/";
const COMPONENTS_DIR = BUILD_DIR + "components/";
const COMPONENTS_LEGACY_DIR = BUILD_DIR + "components-legacy/";
const IMAGE_DECODERS_DIR = BUILD_DIR + "image_decoders/";
const IMAGE_DECODERS_LEGACY_DIR = BUILD_DIR + "image_decoders-legacy/";
const DEFAULT_PREFERENCES_DIR = BUILD_DIR + "default_preferences/";
const MINIFIED_DIR = BUILD_DIR + "minified/";
const MINIFIED_LEGACY_DIR = BUILD_DIR + "minified-legacy/";
const JSDOC_BUILD_DIR = BUILD_DIR + "jsdoc/";
const GH_PAGES_DIR = BUILD_DIR + "gh-pages/";
const DIST_DIR = BUILD_DIR + "dist/";
const TYPES_DIR = BUILD_DIR + "types/";
const TMP_DIR = BUILD_DIR + "tmp/";
const TYPESTEST_DIR = BUILD_DIR + "typestest/";
const COMMON_WEB_FILES = [
"web/images/*.{png,svg,gif}",
"web/debugger.{css,mjs}",
];
const MOZCENTRAL_DIFF_FILE = "mozcentral.diff";
const DIST_REPO_URL = "https://github.com/mozilla/pdfjs-dist";
const CONFIG_FILE = "pdfjs.config";
const config = JSON.parse(fs.readFileSync(CONFIG_FILE).toString());
const ENV_TARGETS = [
"last 2 versions",
"Chrome >= 92",
"Firefox ESR",
"Safari >= 15.4",
"Node >= 18",
"> 1%",
"not IE > 0",
"not dead",
];
// Default Autoprefixer config used for generic, components, minified-pre
const AUTOPREFIXER_CONFIG = {
overrideBrowserslist: ENV_TARGETS,
};
// Default Babel targets used for generic, components, minified-pre
const BABEL_TARGETS = ENV_TARGETS.join(", ");
const DEFINES = Object.freeze({
SKIP_BABEL: true,
TESTING: undefined,
// The main build targets:
GENERIC: false,
MOZCENTRAL: false,
GECKOVIEW: false,
CHROME: false,
MINIFIED: false,
COMPONENTS: false,
LIB: false,
IMAGE_DECODERS: false,
});
function transform(charEncoding, transformFunction) {
return through.obj(function (vinylFile, enc, done) {
const transformedFile = vinylFile.clone();
transformedFile.contents = Buffer.from(
transformFunction(transformedFile.contents),
charEncoding
);
done(null, transformedFile);
});
}
function safeSpawnSync(command, parameters, options) {
// Execute all commands in a shell.
options = options || {};
options.shell = true;
// `options.shell = true` requires parameters to be quoted.
parameters = parameters.map(param => {
if (!/[\s`~!#$*(){[|\\;'"<>?]/.test(param)) {
return param;
}
return '"' + param.replaceAll(/([$\\"`])/g, "\\$1") + '"';
});
const result = spawnSync(command, parameters, options);
if (result.status !== 0) {
console.log(
'Error: command "' +
command +
'" with parameters "' +
parameters +
'" exited with code ' +
result.status
);
process.exit(result.status);
}
return result;
}
function startNode(args, options) {
// Node.js decreased the maximum header size from 80 KB to 8 KB in newer
// releases, which is not sufficient for some of our reference test files
// (such as `issue6360.pdf`), so we need to restore this value. Note that
// this argument needs to be before all other arguments as it needs to be
// passed to the Node.js process itself and not to the script that it runs.
args.unshift("--max-http-header-size=80000");
return spawn("node", args, options);
}
function createStringSource(filename, content) {
const source = stream.Readable({ objectMode: true });
source._read = function () {
this.push(
new Vinyl({
path: filename,
contents: Buffer.from(content),
})
);
this.push(null);
};
return source;
}
function createWebpackConfig(
defines,
output,
{
disableVersionInfo = false,
disableSourceMaps = false,
disableLicenseHeader = false,
defaultPreferencesDir = null,
} = {}
) {
const versionInfo = !disableVersionInfo
? getVersionJSON()
: { version: 0, commit: 0 };
const bundleDefines = {
...defines,
BUNDLE_VERSION: versionInfo.version,
BUNDLE_BUILD: versionInfo.commit,
TESTING: defines.TESTING ?? process.env.TESTING === "true",
BROWSER_PREFERENCES: defaultPreferencesDir
? getBrowserPreferences(defaultPreferencesDir)
: {},
DEFAULT_PREFERENCES: defaultPreferencesDir
? getDefaultPreferences(defaultPreferencesDir)
: {},
DEFAULT_FTL: defines.GENERIC ? getDefaultFtl() : "",
};
const licenseHeaderLibre = fs
.readFileSync("./src/license_header_libre.js")
.toString();
const enableSourceMaps =
!bundleDefines.MOZCENTRAL &&
!bundleDefines.CHROME &&
!bundleDefines.LIB &&
!bundleDefines.TESTING &&
!disableSourceMaps;
const isModule = output.library?.type === "module";
const skipBabel = bundleDefines.SKIP_BABEL;
// `core-js`, see https://github.com/zloirock/core-js/issues/514,
// should be excluded from processing.
const babelExcludes = ["node_modules[\\\\\\/]core-js"];
const babelExcludeRegExp = new RegExp(`(${babelExcludes.join("|")})`);
const babelPresets = skipBabel
? undefined
: [
[
"@babel/preset-env",
{
corejs: "3.34.0",
exclude: ["web.structured-clone"],
shippedProposals: true,
useBuiltIns: "usage",
},
],
];
const babelPlugins = [];
const plugins = [];
if (!disableLicenseHeader) {
plugins.push(
new webpack2.BannerPlugin({ banner: licenseHeaderLibre, raw: true })
);
}
const experiments = isModule ? { outputModule: true } : undefined;
// Required to expose e.g., the `window` object.
output.globalObject = "globalThis";
const basicAlias = {
pdfjs: "src",
"pdfjs-web": "web",
"pdfjs-lib": "web/pdfjs",
"fluent-bundle": "node_modules/@fluent/bundle/esm/index.js",
"fluent-dom": "node_modules/@fluent/dom/esm/index.js",
};
const libraryAlias = {
"display-fetch_stream": "src/display/stubs.js",
"display-network": "src/display/stubs.js",
"display-node_stream": "src/display/stubs.js",
"display-node_utils": "src/display/stubs.js",
};
const viewerAlias = {
"web-alt_text_manager": "web/alt_text_manager.js",
"web-annotation_editor_params": "web/annotation_editor_params.js",
"web-com": "",
"web-l10n_utils": "web/stubs.js",
"web-pdf_attachment_viewer": "web/pdf_attachment_viewer.js",
"web-pdf_cursor_tools": "web/pdf_cursor_tools.js",
"web-pdf_document_properties": "web/pdf_document_properties.js",
"web-pdf_find_bar": "web/pdf_find_bar.js",
"web-pdf_layer_viewer": "web/pdf_layer_viewer.js",
"web-pdf_outline_viewer": "web/pdf_outline_viewer.js",
"web-pdf_presentation_mode": "web/pdf_presentation_mode.js",
"web-pdf_sidebar": "web/pdf_sidebar.js",
"web-pdf_thumbnail_viewer": "web/pdf_thumbnail_viewer.js",
"web-print_service": "",
"web-secondary_toolbar": "web/secondary_toolbar.js",
"web-toolbar": "web/toolbar.js",
};
if (bundleDefines.CHROME) {
libraryAlias["display-fetch_stream"] = "src/display/fetch_stream.js";
libraryAlias["display-network"] = "src/display/network.js";
viewerAlias["web-com"] = "web/chromecom.js";
viewerAlias["web-print_service"] = "web/pdf_print_service.js";
} else if (bundleDefines.GENERIC) {
// Aliases defined here must also be replicated in the paths section of
// the tsconfig.json file for the type generation to work.
// In the tsconfig.json files, the .js extension must be omitted.
libraryAlias["display-fetch_stream"] = "src/display/fetch_stream.js";
libraryAlias["display-network"] = "src/display/network.js";
libraryAlias["display-node_stream"] = "src/display/node_stream.js";
libraryAlias["display-node_utils"] = "src/display/node_utils.js";
viewerAlias["web-com"] = "web/genericcom.js";
viewerAlias["web-l10n_utils"] = "web/l10n_utils.js";
viewerAlias["web-print_service"] = "web/pdf_print_service.js";
} else if (bundleDefines.MOZCENTRAL) {
if (bundleDefines.GECKOVIEW) {
const gvAlias = {
"web-l10n_utils": "web/stubs.js",
"web-toolbar": "web/toolbar-geckoview.js",
};
for (const key in viewerAlias) {
viewerAlias[key] = gvAlias[key] || "web/stubs-geckoview.js";
}
}
viewerAlias["web-com"] = "web/firefoxcom.js";
viewerAlias["web-print_service"] = "web/firefox_print_service.js";
}
const alias = { ...basicAlias, ...libraryAlias, ...viewerAlias };
for (const key in alias) {
alias[key] = path.join(__dirname, alias[key]);
}
return {
mode: "production",
optimization: {
mangleExports: false,
minimize: false,
},
experiments,
output,
performance: {
hints: false, // Disable messages about larger file sizes.
},
plugins,
resolve: {
alias,
},
devtool: enableSourceMaps ? "source-map" : undefined,
module: {
rules: [
{
loader: "babel-loader",
exclude: babelExcludeRegExp,
options: {
presets: babelPresets,
plugins: babelPlugins,
targets: BABEL_TARGETS,
},
},
{
loader: path.join(__dirname, "external/webpack/pdfjsdev-loader.mjs"),
options: {
rootPath: __dirname,
saveComments: false,
defines: bundleDefines,
},
},
],
},
// Avoid shadowing actual Node.js variables with polyfills, by disabling
// polyfills/mocks - https://webpack.js.org/configuration/node/
node: false,
};
}
function webpack2Stream(webpackConfig) {
// Replacing webpack1 to webpack2 in the webpack-stream.
return webpackStream(webpackConfig, webpack2);
}
function getVersionJSON() {
return JSON.parse(fs.readFileSync(BUILD_DIR + "version.json").toString());
}
function checkChromePreferencesFile(chromePrefsPath, webPrefs) {
const chromePrefs = JSON.parse(fs.readFileSync(chromePrefsPath).toString());
const chromePrefsKeys = Object.keys(chromePrefs.properties).filter(key => {
const description = chromePrefs.properties[key].description;
// Deprecated keys are allowed in the managed preferences file.
// The code maintainer is responsible for adding migration logic to
// extensions/chromium/options/migration.js and web/chromecom.js .
return !description || !description.startsWith("DEPRECATED.");
});
let ret = true;
// Verify that every entry in webPrefs is also in preferences_schema.json.
for (const [key, value] of Object.entries(webPrefs)) {
if (!chromePrefsKeys.includes(key)) {
// Note: this would also reject keys that are present but marked as
// DEPRECATED. A key should not be marked as DEPRECATED if it is still
// listed in webPrefs.
ret = false;
console.log(
`Warning: ${chromePrefsPath} does not contain an entry for pref: ${key}`
);
} else if (chromePrefs.properties[key].default !== value) {
ret = false;
console.log(
`Warning: not the same values (for "${key}"): ` +
`${chromePrefs.properties[key].default} !== ${value}`
);
}
}
// Verify that preferences_schema.json does not contain entries that are not
// in webPrefs (app_options.js).
for (const key of chromePrefsKeys) {
if (!(key in webPrefs)) {
ret = false;
console.log(
`Warning: ${chromePrefsPath} contains an unrecognized pref: ${key}. ` +
`Remove it, or prepend "DEPRECATED. " and add migration logic to ` +
`extensions/chromium/options/migration.js and web/chromecom.js.`
);
}
}
return ret;
}
function tweakWebpackOutput(jsName) {
const replacer = ["__non_webpack_import__\\("];
if (jsName) {
replacer.push(
" __webpack_exports__ = {};",
" __webpack_exports__ = await __webpack_exports__;"
);
}
const regex = new RegExp(`(${replacer.join("|")})`, "gm");
return replace(regex, match => {
switch (match) {
case "__non_webpack_import__(":
return "import(/* webpackIgnore: true */ ";
case " __webpack_exports__ = {};":
return ` __webpack_exports__ = globalThis.${jsName} = {};`;
case " __webpack_exports__ = await __webpack_exports__;":
return ` __webpack_exports__ = globalThis.${jsName} = await (globalThis.${jsName}Promise = __webpack_exports__);`;
}
return match;
});
}
function createMainBundle(defines) {
const mainFileConfig = createWebpackConfig(defines, {
filename: "pdf.mjs",
library: {
type: "module",
},
});
return gulp
.src("./src/pdf.js")
.pipe(webpack2Stream(mainFileConfig))
.pipe(tweakWebpackOutput("pdfjsLib"));
}
function createScriptingBundle(defines, extraOptions = undefined) {
const scriptingFileConfig = createWebpackConfig(
defines,
{
filename: "pdf.scripting.mjs",
library: {
type: "module",
},
},
extraOptions
);
return gulp
.src("./src/pdf.scripting.js")
.pipe(webpack2Stream(scriptingFileConfig))
.pipe(tweakWebpackOutput());
}
function createSandboxExternal(defines) {
const licenseHeader = fs.readFileSync("./src/license_header.js").toString();
const ctx = {
saveComments: false,
defines,
};
return gulp
.src("./src/pdf.sandbox.external.js")
.pipe(rename("pdf.sandbox.external.sys.mjs"))
.pipe(
transform("utf8", content => {
content = preprocessPDFJSCode(ctx, content);
return `${licenseHeader}\n${content}`;
})
);
}
function createTemporaryScriptingBundle(defines, extraOptions = undefined) {
return createScriptingBundle(defines, {
disableVersionInfo: !!(extraOptions && extraOptions.disableVersionInfo),
disableSourceMaps: true,
disableLicenseHeader: true,
}).pipe(gulp.dest(TMP_DIR));
}
function createSandboxBundle(defines, extraOptions = undefined) {
const scriptingPath = TMP_DIR + "pdf.scripting.mjs";
// Insert the source as a string to be `eval`-ed in the sandbox.
const sandboxDefines = {
...defines,
PDF_SCRIPTING_JS_SOURCE: fs.readFileSync(scriptingPath).toString(),
};
fs.unlinkSync(scriptingPath);
const sandboxFileConfig = createWebpackConfig(
sandboxDefines,
{
filename: "pdf.sandbox.mjs",
library: {
type: "module",
},
},
extraOptions
);
return gulp
.src("./src/pdf.sandbox.js")
.pipe(webpack2Stream(sandboxFileConfig))
.pipe(tweakWebpackOutput("pdfjsSandbox"));
}
function createWorkerBundle(defines) {
const workerFileConfig = createWebpackConfig(defines, {
filename: "pdf.worker.mjs",
library: {
type: "module",
},
});
return gulp
.src("./src/pdf.worker.js")
.pipe(webpack2Stream(workerFileConfig))
.pipe(tweakWebpackOutput("pdfjsWorker"));
}
function createWebBundle(defines, options) {
const viewerFileConfig = createWebpackConfig(
defines,
{
filename: "viewer.mjs",
library: {
type: "module",
},
},
{
defaultPreferencesDir: options.defaultPreferencesDir,
}
);
return gulp
.src("./web/viewer.js")
.pipe(webpack2Stream(viewerFileConfig))
.pipe(tweakWebpackOutput());
}
function createGVWebBundle(defines, options) {
const viewerFileConfig = createWebpackConfig(
defines,
{
filename: "viewer-geckoview.mjs",
library: {
type: "module",
},
},
{
defaultPreferencesDir: options.defaultPreferencesDir,
}
);
return gulp
.src("./web/viewer-geckoview.js")
.pipe(webpack2Stream(viewerFileConfig))
.pipe(tweakWebpackOutput());
}
function createComponentsBundle(defines) {
const componentsFileConfig = createWebpackConfig(defines, {
filename: "pdf_viewer.mjs",
library: {
type: "module",
},
});
return gulp
.src("./web/pdf_viewer.component.js")
.pipe(webpack2Stream(componentsFileConfig))
.pipe(tweakWebpackOutput("pdfjsViewer"));
}
function createImageDecodersBundle(defines) {
const componentsFileConfig = createWebpackConfig(defines, {
filename: "pdf.image_decoders.mjs",
library: {
type: "module",
},
});
return gulp
.src("./src/pdf.image_decoders.js")
.pipe(webpack2Stream(componentsFileConfig))
.pipe(tweakWebpackOutput("pdfjsImageDecoders"));
}
function createCMapBundle() {
return gulp.src(["external/bcmaps/*.bcmap", "external/bcmaps/LICENSE"], {
base: "external/bcmaps",
});
}
function createStandardFontBundle() {
return gulp.src(
[
"external/standard_fonts/*.pfb",
"external/standard_fonts/*.ttf",
"external/standard_fonts/LICENSE_FOXIT",
"external/standard_fonts/LICENSE_LIBERATION",
],
{
base: "external/standard_fonts",
}
);
}
function checkFile(filePath) {
try {
const stat = fs.lstatSync(filePath);
return stat.isFile();
} catch {
return false;
}
}
function checkDir(dirPath) {
try {
const stat = fs.lstatSync(dirPath);
return stat.isDirectory();
} catch {
return false;
}
}
function replaceInFile(filePath, find, replacement) {
let content = fs.readFileSync(filePath).toString();
content = content.replace(find, replacement);
fs.writeFileSync(filePath, content);
}
function getTempFile(prefix, suffix) {
mkdirp.sync(BUILD_DIR + "tmp/");
const bytes = crypto.randomBytes(6).toString("hex");
const filePath = BUILD_DIR + "tmp/" + prefix + bytes + suffix;
fs.writeFileSync(filePath, "");
return filePath;
}
function createTestSource(testsName, { bot = false, xfaOnly = false } = {}) {
const source = stream.Readable({ objectMode: true });
source._read = function () {
console.log();
console.log("### Running " + testsName + " tests");
const PDF_TEST = process.env.PDF_TEST || "test_manifest.json";
let forceNoChrome = false;
const args = ["test.mjs"];
switch (testsName) {
case "browser":
if (!bot) {
args.push("--reftest");
} else {
const os = process.env.OS;
if (/windows/i.test(os)) {
// The browser-tests are too slow in Google Chrome on the Windows
// bot, causing a timeout, hence disabling them for now.
forceNoChrome = true;
}
}
if (xfaOnly) {
args.push("--xfaOnly");
}
args.push("--manifestFile=" + PDF_TEST);
break;
case "unit":
args.push("--unitTest");
break;
case "font":
args.push("--fontTest");
break;
case "integration":
args.push("--integration");
break;
default:
this.emit("error", new Error("Unknown name: " + testsName));
return null;
}
if (bot) {
args.push("--strictVerify");
}
if (process.argv.includes("--noChrome") || forceNoChrome) {
args.push("--noChrome");
}
if (process.argv.includes("--headless")) {
args.push("--headless");
}
const testProcess = startNode(args, { cwd: TEST_DIR, stdio: "inherit" });
testProcess.on("close", function (code) {
source.push(null);
});
return undefined;
};
return source;
}
function makeRef(done, bot) {
console.log();
console.log("### Creating reference images");
let forceNoChrome = false;
const args = ["test.mjs", "--masterMode"];
if (bot) {
const os = process.env.OS;
if (/windows/i.test(os)) {
// The browser-tests are too slow in Google Chrome on the Windows
// bot, causing a timeout, hence disabling them for now.
forceNoChrome = true;
}
args.push("--noPrompts", "--strictVerify");
}
if (process.argv.includes("--noChrome") || forceNoChrome) {
args.push("--noChrome");
}
if (process.argv.includes("--headless")) {
args.push("--headless");
}
const testProcess = startNode(args, { cwd: TEST_DIR, stdio: "inherit" });
testProcess.on("close", function (code) {
done();
});
}
gulp.task("default", function (done) {
console.log("Available tasks:");
const tasks = Object.keys(gulp.registry().tasks());
for (const taskName of tasks.sort()) {
if (taskName.endsWith("-pre")) {
continue;
}
console.log(" " + taskName);
}
done();
});
function createBuildNumber(done) {
console.log();
console.log("### Getting extension build number");
exec(
"git log --format=oneline " + config.baseVersion + "..",
function (err, stdout, stderr) {
let buildNumber = 0;
if (!err) {
// Build number is the number of commits since base version
buildNumber = stdout ? stdout.match(/\n/g).length : 0;
} else {
console.log(
"This is not a Git repository; using default build number."
);
}
console.log("Extension build number: " + buildNumber);
const version = config.versionPrefix + buildNumber;
exec('git log --format="%h" -n 1', function (err2, stdout2, stderr2) {
let buildCommit = "";
if (!err2) {
buildCommit = stdout2.replace("\n", "");
}
createStringSource(
"version.json",
JSON.stringify(
{
version,
build: buildNumber,
commit: buildCommit,
},
null,
2
)
)
.pipe(gulp.dest(BUILD_DIR))
.on("end", done);
});
}
);
}
function buildDefaultPreferences(defines, dir) {
console.log();
console.log("### Building default preferences");
const bundleDefines = {
...defines,
LIB: true,
TESTING: defines.TESTING ?? process.env.TESTING === "true",
};
const defaultPreferencesConfig = createWebpackConfig(
bundleDefines,
{
filename: "app_options.mjs",
library: {
type: "module",
},
},
{
disableVersionInfo: true,
}
);
return gulp
.src("web/app_options.js")
.pipe(webpack2Stream(defaultPreferencesConfig))
.pipe(gulp.dest(DEFAULT_PREFERENCES_DIR + dir));
}
async function parseDefaultPreferences(dir) {
console.log();
console.log("### Parsing default preferences");
// eslint-disable-next-line no-unsanitized/method
const { AppOptions, OptionKind } = await import(
"./" + DEFAULT_PREFERENCES_DIR + dir + "app_options.mjs"
);
const browserPrefs = AppOptions.getAll(OptionKind.BROWSER);
if (Object.keys(browserPrefs).length === 0) {
throw new Error("No browser preferences found.");
}
const prefs = AppOptions.getAll(OptionKind.PREFERENCE);
if (Object.keys(prefs).length === 0) {
throw new Error("No default preferences found.");
}
fs.writeFileSync(
DEFAULT_PREFERENCES_DIR + dir + "browser_preferences.json",
JSON.stringify(browserPrefs)
);
fs.writeFileSync(
DEFAULT_PREFERENCES_DIR + dir + "default_preferences.json",
JSON.stringify(prefs)
);
}
function getBrowserPreferences(dir) {
const str = fs
.readFileSync(DEFAULT_PREFERENCES_DIR + dir + "browser_preferences.json")
.toString();
return JSON.parse(str);
}
function getDefaultPreferences(dir) {
const str = fs
.readFileSync(DEFAULT_PREFERENCES_DIR + dir + "default_preferences.json")
.toString();
return JSON.parse(str);
}
function getDefaultFtl() {
const content = fs.readFileSync("l10n/en-US/viewer.ftl").toString(),
stringBuf = [];
// Strip out comments and line-breaks.
const regExp = /^\s*#/;
for (const line of content.split("\n")) {
if (!line || regExp.test(line)) {
continue;
}
stringBuf.push(line);
}
return stringBuf.join("\n");
}
gulp.task("locale", function () {
const VIEWER_LOCALE_OUTPUT = "web/locale/";
console.log();
console.log("### Building localization files");
rimraf.sync(VIEWER_LOCALE_OUTPUT);
mkdirp.sync(VIEWER_LOCALE_OUTPUT);
const subfolders = fs.readdirSync(L10N_DIR);
subfolders.sort();
const viewerOutput = Object.create(null);
const locales = [];
for (const locale of subfolders) {
const dirPath = L10N_DIR + locale;
if (!checkDir(dirPath)) {
continue;
}
if (!/^[a-z][a-z]([a-z])?(-[A-Z][A-Z])?$/.test(locale)) {
console.log("Skipping invalid locale: " + locale);
continue;
}
mkdirp.sync(VIEWER_LOCALE_OUTPUT + "/" + locale);
locales.push(locale);
if (checkFile(dirPath + "/viewer.ftl")) {
// The L10n-implementations, in the viewer, use lowercase language-codes
// internally.
viewerOutput[locale.toLowerCase()] = `${locale}/viewer.ftl`;
}
}
const glob = locales.length === 1 ? locales[0] : `{${locales.join(",")}}`;
return merge([
createStringSource("locale.json", JSON.stringify(viewerOutput)).pipe(
gulp.dest(VIEWER_LOCALE_OUTPUT)
),
gulp
.src(`${L10N_DIR}/${glob}/viewer.ftl`, {
base: L10N_DIR,
})
.pipe(gulp.dest(VIEWER_LOCALE_OUTPUT)),
]);
});
gulp.task("cmaps", async function () {
const CMAP_INPUT = "external/cmaps";
const VIEWER_CMAP_OUTPUT = "external/bcmaps";
console.log();
console.log("### Building cmaps");
// Testing a file that usually present.
if (!checkFile(CMAP_INPUT + "/UniJIS-UCS2-H")) {
console.log("./external/cmaps has no cmap files, download them from:");
console.log(" https://github.com/adobe-type-tools/cmap-resources");
throw new Error("cmap files were not found");
}
// Remove old bcmap files.
fs.readdirSync(VIEWER_CMAP_OUTPUT).forEach(function (file) {
if (/\.bcmap$/i.test(file)) {
fs.unlinkSync(VIEWER_CMAP_OUTPUT + "/" + file);
}
});
const { compressCmaps } = await import(
"./external/cmapscompress/compress.mjs"
);
compressCmaps(CMAP_INPUT, VIEWER_CMAP_OUTPUT, true);
});
function preprocessCSS(source, defines) {
const outName = getTempFile("~preprocess", ".css");
preprocess(source, outName, defines);
const out = fs.readFileSync(outName).toString();
fs.unlinkSync(outName);
const i = source.lastIndexOf("/");
return createStringSource(source.substr(i + 1), out);
}
function discardCommentsCSS() {
let copyrightNum = 0;
function remove(comment) {
// Remove all comments, except the *first* license header.
if (comment.startsWith("Copyright") && copyrightNum++ === 0) {
return false;
}
return true;
}
return postcssDiscardComments({ remove });
}
function preprocessHTML(source, defines) {
const outName = getTempFile("~preprocess", ".html");
preprocess(source, outName, defines);
const out = fs.readFileSync(outName).toString();
fs.unlinkSync(outName);
const i = source.lastIndexOf("/");
return createStringSource(source.substr(i + 1), `${out.trimEnd()}\n`);
}
function buildGeneric(defines, dir) {
rimraf.sync(dir);
return merge([
createMainBundle(defines).pipe(gulp.dest(dir + "build")),
createWorkerBundle(defines).pipe(gulp.dest(dir + "build")),
createSandboxBundle(defines).pipe(gulp.dest(dir + "build")),
createWebBundle(defines, {
defaultPreferencesDir: defines.SKIP_BABEL
? "generic/"
: "generic-legacy/",
}).pipe(gulp.dest(dir + "web")),
gulp.src(COMMON_WEB_FILES, { base: "web/" }).pipe(gulp.dest(dir + "web")),
gulp.src("LICENSE").pipe(gulp.dest(dir)),
gulp
.src(["web/locale/*/viewer.ftl", "web/locale/locale.json"], {
base: "web/",
})
.pipe(gulp.dest(dir + "web")),
createCMapBundle().pipe(gulp.dest(dir + "web/cmaps")),
createStandardFontBundle().pipe(gulp.dest(dir + "web/standard_fonts")),
preprocessHTML("web/viewer.html", defines).pipe(gulp.dest(dir + "web")),
preprocessCSS("web/viewer.css", defines)
.pipe(
postcss([
postcssDirPseudoClass(),
discardCommentsCSS(),
postcssNesting(),
postcssDarkThemeClass(),
autoprefixer(AUTOPREFIXER_CONFIG),
])
)
.pipe(gulp.dest(dir + "web")),
gulp
.src("web/compressed.tracemonkey-pldi-09.pdf")
.pipe(gulp.dest(dir + "web")),
]);
}
// Builds the generic production viewer that is only compatible with up-to-date
// HTML5 browsers, which implement modern ECMAScript features.
gulp.task(
"generic",
gulp.series(
createBuildNumber,
"locale",
function scriptingGeneric() {
const defines = { ...DEFINES, GENERIC: true };
return merge([
buildDefaultPreferences(defines, "generic/"),
createTemporaryScriptingBundle(defines),
]);
},
async function prefsGeneric() {
await parseDefaultPreferences("generic/");
},
function createGeneric() {
console.log();
console.log("### Creating generic viewer");
const defines = { ...DEFINES, GENERIC: true };
return buildGeneric(defines, GENERIC_DIR);
}
)
);
// Builds the generic production viewer that should be compatible with most
// older HTML5 browsers.
gulp.task(
"generic-legacy",
gulp.series(
createBuildNumber,
"locale",
function scriptingGenericLegacy() {
const defines = { ...DEFINES, GENERIC: true, SKIP_BABEL: false };
return merge([
buildDefaultPreferences(defines, "generic-legacy/"),
createTemporaryScriptingBundle(defines),
]);
},
async function prefsGenericLegacy() {
await parseDefaultPreferences("generic-legacy/");
},
function createGenericLegacy() {
console.log();
console.log("### Creating generic (legacy) viewer");
const defines = { ...DEFINES, GENERIC: true, SKIP_BABEL: false };
return buildGeneric(defines, GENERIC_LEGACY_DIR);
}
)
);
function buildComponents(defines, dir) {
rimraf.sync(dir);
const COMPONENTS_IMAGES = [
"web/images/annotation-*.svg",
"web/images/loading-icon.gif",
"web/images/altText_*.svg",
"web/images/editor-toolbar-*.svg",
"web/images/toolbarButton-menuArrow.svg",
];
return merge([
createComponentsBundle(defines).pipe(gulp.dest(dir)),
gulp.src(COMPONENTS_IMAGES).pipe(gulp.dest(dir + "images")),
preprocessCSS("web/pdf_viewer.css", defines)
.pipe(
postcss([
postcssDirPseudoClass(),
discardCommentsCSS(),
postcssNesting(),
autoprefixer(AUTOPREFIXER_CONFIG),
])
)
.pipe(gulp.dest(dir)),
]);
}
gulp.task(
"components",
gulp.series(createBuildNumber, function createComponents() {
console.log();
console.log("### Creating generic components");
const defines = { ...DEFINES, COMPONENTS: true, GENERIC: true };
return buildComponents(defines, COMPONENTS_DIR);
})
);
gulp.task(
"components-legacy",
gulp.series(createBuildNumber, function createComponentsLegacy() {
console.log();
console.log("### Creating generic (legacy) components");
const defines = {
...DEFINES,
COMPONENTS: true,
GENERIC: true,
SKIP_BABEL: false,
};
return buildComponents(defines, COMPONENTS_LEGACY_DIR);
})
);
gulp.task(
"image_decoders",
gulp.series(createBuildNumber, function createImageDecoders() {
console.log();
console.log("### Creating image decoders");
const defines = { ...DEFINES, GENERIC: true, IMAGE_DECODERS: true };
return createImageDecodersBundle(defines).pipe(
gulp.dest(IMAGE_DECODERS_DIR)
);
})
);
gulp.task(
"image_decoders-legacy",
gulp.series(createBuildNumber, function createImageDecodersLegacy() {
console.log();
console.log("### Creating (legacy) image decoders");
const defines = {
...DEFINES,
GENERIC: true,
IMAGE_DECODERS: true,
SKIP_BABEL: false,
};
return createImageDecodersBundle(defines).pipe(
gulp.dest(IMAGE_DECODERS_LEGACY_DIR)
);
})
);
function buildMinified(defines, dir) {
rimraf.sync(dir);
return merge([
createMainBundle(defines).pipe(gulp.dest(dir + "build")),
createWorkerBundle(defines).pipe(gulp.dest(dir + "build")),
createSandboxBundle(defines).pipe(gulp.dest(dir + "build")),
createImageDecodersBundle({ ...defines, IMAGE_DECODERS: true }).pipe(
gulp.dest(dir + "image_decoders")
),
]);
}
async function parseMinified(dir) {
const pdfFile = fs.readFileSync(dir + "build/pdf.mjs").toString();
const pdfWorkerFile = fs
.readFileSync(dir + "build/pdf.worker.mjs")
.toString();
const pdfSandboxFile = fs
.readFileSync(dir + "build/pdf.sandbox.mjs")
.toString();
const pdfImageDecodersFile = fs
.readFileSync(dir + "image_decoders/pdf.image_decoders.mjs")
.toString();
console.log();
console.log("### Minifying js files");
const { minify } = await import("terser");
const options = {
compress: {
// V8 chokes on very long sequences, work around that.
sequences: false,
},
keep_classnames: true,
keep_fnames: true,
module: true,
};
fs.writeFileSync(
dir + "build/pdf.min.mjs",
(await minify(pdfFile, options)).code
);
fs.writeFileSync(
dir + "build/pdf.worker.min.mjs",
(await minify(pdfWorkerFile, options)).code
);
fs.writeFileSync(
dir + "build/pdf.sandbox.min.mjs",
(await minify(pdfSandboxFile, options)).code
);
fs.writeFileSync(
dir + "image_decoders/pdf.image_decoders.min.mjs",
(await minify(pdfImageDecodersFile, options)).code
);
console.log();
console.log("### Cleaning js files");
fs.unlinkSync(dir + "build/pdf.mjs");
fs.unlinkSync(dir + "build/pdf.worker.mjs");
fs.unlinkSync(dir + "build/pdf.sandbox.mjs");
fs.renameSync(dir + "build/pdf.min.mjs", dir + "build/pdf.mjs");
fs.renameSync(dir + "build/pdf.worker.min.mjs", dir + "build/pdf.worker.mjs");
fs.renameSync(
dir + "build/pdf.sandbox.min.mjs",
dir + "build/pdf.sandbox.mjs"
);
fs.renameSync(
dir + "image_decoders/pdf.image_decoders.min.mjs",
dir + "image_decoders/pdf.image_decoders.mjs"
);
}
gulp.task(
"minified",
gulp.series(
createBuildNumber,
"locale",
function scriptingMinified() {
const defines = { ...DEFINES, MINIFIED: true, GENERIC: true };
return merge([
buildDefaultPreferences(defines, "minified/"),
createTemporaryScriptingBundle(defines),
]);
},
async function prefsMinified() {
await parseDefaultPreferences("minified/");
},
function createMinified() {
console.log();
console.log("### Creating minified viewer");
const defines = { ...DEFINES, MINIFIED: true, GENERIC: true };
return buildMinified(defines, MINIFIED_DIR);
},
async function compressMinified(done) {
await parseMinified(MINIFIED_DIR);
done();
}
)
);
gulp.task(
"minified-legacy",
gulp.series(
createBuildNumber,
"locale",
function scriptingMinifiedLegacy() {
const defines = {
...DEFINES,
MINIFIED: true,
GENERIC: true,
SKIP_BABEL: false,
};
return merge([
buildDefaultPreferences(defines, "minified-legacy/"),
createTemporaryScriptingBundle(defines),
]);
},
async function prefsMinifiedLegacy() {
await parseDefaultPreferences("minified-legacy/");
},
function createMinifiedLegacy() {
console.log();
console.log("### Creating minified (legacy) viewer");
const defines = {
...DEFINES,
MINIFIED: true,
GENERIC: true,
SKIP_BABEL: false,
};
return buildMinified(defines, MINIFIED_LEGACY_DIR);
},
async function compressMinifiedLegacy(done) {
await parseMinified(MINIFIED_LEGACY_DIR);
done();
}
)
);
function preprocessDefaultPreferences(content) {
const licenseHeader = fs.readFileSync("./src/license_header.js").toString();
const MODIFICATION_WARNING =
"//\n// THIS FILE IS GENERATED AUTOMATICALLY, DO NOT EDIT MANUALLY!\n//\n";
const bundleDefines = {
...DEFINES,
DEFAULT_PREFERENCES: getDefaultPreferences("mozcentral/"),
};
content = preprocessPDFJSCode(
{
rootPath: __dirname,
defines: bundleDefines,
},
content
);
return licenseHeader + "\n" + MODIFICATION_WARNING + "\n" + content + "\n";
}
function replaceMozcentralCSS() {
return replace(/var\(--(inline-(?:start|end))\)/g, "$1");
}
gulp.task(
"mozcentral",
gulp.series(
createBuildNumber,
function scriptingMozcentral() {
const defines = { ...DEFINES, MOZCENTRAL: true };
return buildDefaultPreferences(defines, "mozcentral/");
},
async function prefsMozcentral() {
await parseDefaultPreferences("mozcentral/");
},
function createMozcentral() {
console.log();
console.log("### Building mozilla-central extension");
const defines = { ...DEFINES, MOZCENTRAL: true };
const gvDefines = { ...defines, GECKOVIEW: true };
const MOZCENTRAL_DIR = BUILD_DIR + "mozcentral/",
MOZCENTRAL_EXTENSION_DIR = MOZCENTRAL_DIR + "browser/extensions/pdfjs/",
MOZCENTRAL_CONTENT_DIR = MOZCENTRAL_EXTENSION_DIR + "content/",
MOZCENTRAL_L10N_DIR =
MOZCENTRAL_DIR + "browser/locales/en-US/pdfviewer/",
FIREFOX_CONTENT_DIR = EXTENSION_SRC_DIR + "/firefox/content/";
const MOZCENTRAL_WEB_FILES = [
...COMMON_WEB_FILES,
"!web/images/toolbarButton-openFile.svg",
];
const MOZCENTRAL_AUTOPREFIXER_CONFIG = {
overrideBrowserslist: ["last 1 firefox versions"],
};
// Clear out everything in the firefox extension build directory
rimraf.sync(MOZCENTRAL_DIR);
return merge([
createMainBundle(defines).pipe(
gulp.dest(MOZCENTRAL_CONTENT_DIR + "build")
),
createScriptingBundle(defines).pipe(
gulp.dest(MOZCENTRAL_CONTENT_DIR + "build")
),
createSandboxExternal(defines).pipe(
gulp.dest(MOZCENTRAL_CONTENT_DIR + "build")
),
createWorkerBundle(defines).pipe(
gulp.dest(MOZCENTRAL_CONTENT_DIR + "build")
),
createWebBundle(defines, { defaultPreferencesDir: "mozcentral/" }).pipe(
gulp.dest(MOZCENTRAL_CONTENT_DIR + "web")
),
createGVWebBundle(gvDefines, {
defaultPreferencesDir: "mozcentral/",
}).pipe(gulp.dest(MOZCENTRAL_CONTENT_DIR + "web")),
gulp
.src(MOZCENTRAL_WEB_FILES, { base: "web/" })
.pipe(gulp.dest(MOZCENTRAL_CONTENT_DIR + "web")),
createCMapBundle().pipe(
gulp.dest(MOZCENTRAL_CONTENT_DIR + "web/cmaps")
),
createStandardFontBundle().pipe(
gulp.dest(MOZCENTRAL_CONTENT_DIR + "web/standard_fonts")
),
preprocessHTML("web/viewer.html", defines).pipe(
gulp.dest(MOZCENTRAL_CONTENT_DIR + "web")
),
preprocessHTML("web/viewer-geckoview.html", gvDefines).pipe(
gulp.dest(MOZCENTRAL_CONTENT_DIR + "web")
),
preprocessCSS("web/viewer.css", defines)
.pipe(
postcss([
discardCommentsCSS(),
autoprefixer(MOZCENTRAL_AUTOPREFIXER_CONFIG),
])
)
.pipe(replaceMozcentralCSS())
.pipe(gulp.dest(MOZCENTRAL_CONTENT_DIR + "web")),
preprocessCSS("web/viewer-geckoview.css", gvDefines)
.pipe(
postcss([
discardCommentsCSS(),
autoprefixer(MOZCENTRAL_AUTOPREFIXER_CONFIG),
])
)
.pipe(replaceMozcentralCSS())
.pipe(gulp.dest(MOZCENTRAL_CONTENT_DIR + "web")),
gulp.src("l10n/en-US/*.ftl").pipe(gulp.dest(MOZCENTRAL_L10N_DIR)),
gulp.src("LICENSE").pipe(gulp.dest(MOZCENTRAL_EXTENSION_DIR)),
gulp
.src(FIREFOX_CONTENT_DIR + "PdfJsDefaultPreferences.sys.mjs")
.pipe(transform("utf8", preprocessDefaultPreferences))
.pipe(gulp.dest(MOZCENTRAL_CONTENT_DIR)),
]);
}
)
);
gulp.task(
"chromium",
gulp.series(
createBuildNumber,
"locale",
function scriptingChromium() {
const defines = { ...DEFINES, CHROME: true, SKIP_BABEL: false };
return merge([
buildDefaultPreferences(defines, "chromium/"),
createTemporaryScriptingBundle(defines),
]);
},
async function prefsChromium() {
await parseDefaultPreferences("chromium/");
},
function createChromium() {
console.log();
console.log("### Building Chromium extension");
const defines = { ...DEFINES, CHROME: true, SKIP_BABEL: false };
const CHROME_BUILD_DIR = BUILD_DIR + "/chromium/",
CHROME_BUILD_CONTENT_DIR = CHROME_BUILD_DIR + "/content/";
const CHROME_WEB_FILES = [
...COMMON_WEB_FILES,
"!web/images/toolbarButton-openFile.svg",
];
// Clear out everything in the chrome extension build directory
rimraf.sync(CHROME_BUILD_DIR);
const version = getVersionJSON().version;
return merge([
createMainBundle(defines).pipe(
gulp.dest(CHROME_BUILD_CONTENT_DIR + "build")
),
createWorkerBundle(defines).pipe(
gulp.dest(CHROME_BUILD_CONTENT_DIR + "build")
),
createSandboxBundle(defines).pipe(
gulp.dest(CHROME_BUILD_CONTENT_DIR + "build")
),
createWebBundle(defines, { defaultPreferencesDir: "chromium/" }).pipe(
gulp.dest(CHROME_BUILD_CONTENT_DIR + "web")
),
gulp
.src(CHROME_WEB_FILES, { base: "web/" })
.pipe(gulp.dest(CHROME_BUILD_CONTENT_DIR + "web")),
gulp
.src(["web/locale/*/viewer.ftl", "web/locale/locale.json"], {
base: "web/",
})
.pipe(gulp.dest(CHROME_BUILD_CONTENT_DIR + "web")),
createCMapBundle().pipe(
gulp.dest(CHROME_BUILD_CONTENT_DIR + "web/cmaps")
),
createStandardFontBundle().pipe(
gulp.dest(CHROME_BUILD_CONTENT_DIR + "web/standard_fonts")
),
preprocessHTML("web/viewer.html", defines).pipe(
gulp.dest(CHROME_BUILD_CONTENT_DIR + "web")
),
preprocessCSS("web/viewer.css", defines)
.pipe(
postcss([
postcssDirPseudoClass(),
discardCommentsCSS(),
postcssNesting(),
postcssDarkThemeClass(),
autoprefixer(AUTOPREFIXER_CONFIG),
])
)
.pipe(gulp.dest(CHROME_BUILD_CONTENT_DIR + "web")),
gulp.src("LICENSE").pipe(gulp.dest(CHROME_BUILD_DIR)),
gulp
.src("extensions/chromium/manifest.json")
.pipe(replace(/\bPDFJSSCRIPT_VERSION\b/g, version))
.pipe(gulp.dest(CHROME_BUILD_DIR)),
gulp
.src(
[
"extensions/chromium/**/*.{html,js,css,png}",
"extensions/chromium/preferences_schema.json",
],
{ base: "extensions/chromium/" }
)
.pipe(gulp.dest(CHROME_BUILD_DIR)),
]);
}
)
);
gulp.task("jsdoc", function (done) {
console.log();
console.log("### Generating documentation (JSDoc)");
const JSDOC_FILES = ["src/display/api.js"];
rimraf(JSDOC_BUILD_DIR, function () {
mkdirp(JSDOC_BUILD_DIR).then(function () {
const command =
'"node_modules/.bin/jsdoc" -d ' +
JSDOC_BUILD_DIR +
" " +
JSDOC_FILES.join(" ");
exec(command, done);
});
});
});
gulp.task("types", function (done) {
console.log("### Generating TypeScript definitions using `tsc`");
exec(
`"node_modules/.bin/tsc" --outDir ${TYPES_DIR} --project .`,
function () {
exec(`"node_modules/.bin/tsc-alias" --outDir ${TYPES_DIR}`, done);
}
);
});
function buildLibHelper(bundleDefines, inputStream, outputDir) {
function babelPluginReplaceNonWebpackImport(b) {
return {
visitor: {
Identifier(curPath, state) {
if (curPath.node.name === "__non_webpack_import__") {
curPath.replaceWith(b.types.identifier("import"));
}
},
},
};
}
function preprocessLib(content) {
const skipBabel = bundleDefines.SKIP_BABEL;
content = preprocessPDFJSCode(ctx, content);
content = babel.transform(content, {
sourceType: "module",
presets: skipBabel
? undefined
: [["@babel/preset-env", { loose: false, modules: false }]],
plugins: [babelPluginReplaceNonWebpackImport],
targets: BABEL_TARGETS,
}).code;
content = content.replaceAll(
/(\sfrom\s".*?)(?:\/src)(\/[^"]*"?;)$/gm,
(all, prefix, suffix) => prefix + suffix
);
return licenseHeaderLibre + content;
}
const ctx = {
rootPath: __dirname,
saveComments: false,
defines: bundleDefines,
map: {
"pdfjs-lib": "../pdf.js",
"display-fetch_stream": "./fetch_stream.js",
"display-network": "./network.js",
"display-node_stream": "./node_stream.js",
"display-node_utils": "./node_utils.js",
"fluent-bundle": "../../../node_modules/@fluent/bundle/esm/index.js",
"fluent-dom": "../../../node_modules/@fluent/dom/esm/index.js",
"web-l10n_utils": "../web/l10n_utils.js",
},
};
const licenseHeaderLibre = fs
.readFileSync("./src/license_header_libre.js")
.toString();
return inputStream
.pipe(transform("utf8", preprocessLib))
.pipe(gulp.dest(outputDir));
}
function buildLib(defines, dir) {
const versionInfo = getVersionJSON();
const bundleDefines = {
...defines,
BUNDLE_VERSION: versionInfo.version,
BUNDLE_BUILD: versionInfo.commit,
TESTING: defines.TESTING ?? process.env.TESTING === "true",
BROWSER_PREFERENCES: getBrowserPreferences(
defines.SKIP_BABEL ? "lib/" : "lib-legacy/"
),
DEFAULT_PREFERENCES: getDefaultPreferences(
defines.SKIP_BABEL ? "lib/" : "lib-legacy/"
),
DEFAULT_FTL: getDefaultFtl(),
};
const inputStream = merge([
gulp.src(
[
"src/{core,display,shared}/**/*.js",
"src/{pdf,pdf.image_decoders,pdf.worker}.js",
],
{ base: "src/" }
),
gulp.src(["web/*.js", "!web/{pdfjs,viewer}.js"], { base: "." }),
gulp.src("test/unit/*.js", { base: "." }),
]);
return buildLibHelper(bundleDefines, inputStream, dir);
}
gulp.task(
"lib",
gulp.series(
createBuildNumber,
function scriptingLib() {
const defines = { ...DEFINES, GENERIC: true, LIB: true };
return merge([
buildDefaultPreferences(defines, "lib/"),
createTemporaryScriptingBundle(defines),
]);
},
async function prefsLib() {
await parseDefaultPreferences("lib/");
},
function createLib() {
const defines = { ...DEFINES, GENERIC: true, LIB: true };
return merge([
buildLib(defines, "build/lib/"),
createSandboxBundle(defines).pipe(gulp.dest("build/lib/")),
]);
}
)
);
gulp.task(
"lib-legacy",
gulp.series(
createBuildNumber,
function scriptingLibLegacy() {
const defines = {
...DEFINES,
GENERIC: true,
LIB: true,
SKIP_BABEL: false,
};
return merge([
buildDefaultPreferences(defines, "lib-legacy/"),
createTemporaryScriptingBundle(defines),
]);
},
async function prefsLibLegacy() {
await parseDefaultPreferences("lib-legacy/");
},
function createLibLegacy() {
const defines = {
...DEFINES,
GENERIC: true,
LIB: true,
SKIP_BABEL: false,
};
return merge([
buildLib(defines, "build/lib-legacy/"),
createSandboxBundle(defines).pipe(gulp.dest("build/lib-legacy/")),
]);
}
)
);
function compressPublish(targetName, dir) {
return gulp
.src(dir + "**")
.pipe(zip(targetName))
.pipe(gulp.dest(BUILD_DIR))
.on("end", function () {
console.log("Built distribution file: " + targetName);
});
}
gulp.task(
"publish",
gulp.series("generic", "generic-legacy", function createPublish(done) {
const version = JSON.parse(
fs.readFileSync(BUILD_DIR + "version.json").toString()
).version;
config.stableVersion = version;
return merge([
createStringSource(CONFIG_FILE, JSON.stringify(config, null, 2)).pipe(
gulp.dest(".")
),
compressPublish("pdfjs-" + version + "-dist.zip", GENERIC_DIR),
compressPublish(
"pdfjs-" + version + "-legacy-dist.zip",
GENERIC_LEGACY_DIR
),
]);
})
);
function setTestEnv(done) {
process.env.TESTING = "true";
// TODO: Re-write the relevant unit-tests, which are using `new Date(...)`,
// to not required the following time-zone hack since it doesn't work
// when the unit-tests are run directly in the browser.
process.env.TZ = "UTC";
done();
}
gulp.task(
"test",
gulp.series(setTestEnv, "generic", "components", function runTest() {
return streamqueue(
{ objectMode: true },
createTestSource("unit"),
createTestSource("browser"),
createTestSource("integration")
);
})
);
gulp.task(
"bottest",
gulp.series(setTestEnv, "generic", "components", function runBotTest() {
return streamqueue(
{ objectMode: true },
createTestSource("unit", { bot: true }),
createTestSource("browser", { bot: true }),
createTestSource("integration")
);
})
);
gulp.task(
"xfatest",
gulp.series(setTestEnv, "generic", "components", function runXfaTest() {
return streamqueue(
{ objectMode: true },
createTestSource("unit"),
createTestSource("browser", { xfaOnly: true }),
createTestSource("integration")
);
})
);
gulp.task(
"botxfatest",
gulp.series(setTestEnv, "generic", "components", function runBotXfaTest() {
return streamqueue(
{ objectMode: true },
createTestSource("unit", { bot: true }),
createTestSource("browser", { bot: true, xfaOnly: true }),
createTestSource("integration")
);
})
);
gulp.task(
"browsertest",
gulp.series(setTestEnv, "generic", "components", function runBrowserTest() {
return createTestSource("browser");
})
);
gulp.task(
"botbrowsertest",
gulp.series(
setTestEnv,
"generic",
"components",
function runBotBrowserTest() {
return streamqueue(
{ objectMode: true },
createTestSource("browser", { bot: true })
);
}
)
);
gulp.task(
"unittest",
gulp.series(setTestEnv, "generic", function runUnitTest() {
return createTestSource("unit");
})
);
gulp.task(
"integrationtest",
gulp.series(setTestEnv, "generic", function runIntegrationTest() {
return createTestSource("integration");
})
);
gulp.task(
"fonttest",
gulp.series(setTestEnv, function runFontTest() {
return createTestSource("font");
})
);
gulp.task(
"makeref",
gulp.series(setTestEnv, "generic", "components", function runMakeref(done) {
makeRef(done);
})
);
gulp.task(
"botmakeref",
gulp.series(
setTestEnv,
"generic",
"components",
function runBotMakeref(done) {
makeRef(done, true);
}
)
);
gulp.task(
"typestest",
gulp.series(
setTestEnv,
"generic",
"types",
function createTypesTest() {
return merge([
packageJson().pipe(gulp.dest(TYPESTEST_DIR)),
gulp
.src("external/dist/**/*", {
base: "external/dist",
removeBOM: false,
})
.pipe(gulp.dest(TYPESTEST_DIR)),
gulp
.src(TYPES_DIR + "**/*", { base: TYPES_DIR })
.pipe(gulp.dest(TYPESTEST_DIR + "types/")),
]);
},
function runTypesTest(done) {
exec('"node_modules/.bin/tsc" -p test/types', function (err, stdout) {
if (err) {
console.log(`Couldn't compile TypeScript test: ${stdout}`);
}
done(err);
});
}
)
);
function createBaseline(done) {
console.log();
console.log("### Creating baseline environment");
const baselineCommit = process.env.BASELINE;
if (!baselineCommit) {
done(new Error("Missing baseline commit. Specify the BASELINE variable."));
return;
}
let initializeCommand = "git fetch origin";
if (!checkDir(BASELINE_DIR)) {
mkdirp.sync(BASELINE_DIR);
initializeCommand = "git clone ../../ .";
}
const workingDirectory = path.resolve(process.cwd(), BASELINE_DIR);
exec(initializeCommand, { cwd: workingDirectory }, function (error) {
if (error) {
done(new Error("Baseline clone/fetch failed."));
return;
}
exec(
"git checkout " + baselineCommit,
{ cwd: workingDirectory },
function (error2) {
if (error2) {
done(new Error("Baseline commit checkout failed."));
return;
}
console.log('Baseline commit "' + baselineCommit + '" checked out.');
done();
}
);
});
}
gulp.task(
"unittestcli",
gulp.series(setTestEnv, "lib-legacy", function runUnitTestCli(done) {
const options = [
"node_modules/jasmine/bin/jasmine",
"JASMINE_CONFIG_PATH=test/unit/clitests.json",
];
const jasmineProcess = startNode(options, { stdio: "inherit" });
jasmineProcess.on("close", function (code) {
if (code !== 0) {
done(new Error("Unit tests failed."));
return;
}
done();
});
})
);
gulp.task("lint", function (done) {
console.log();
console.log("### Linting JS/CSS files");
// Ensure that we lint the Firefox specific *.jsm files too.
const esLintOptions = [
"node_modules/eslint/bin/eslint",
"--ext",
".js,.jsm,.mjs,.json",
".",
"--report-unused-disable-directives",
];
if (process.argv.includes("--fix")) {
esLintOptions.push("--fix");
}
const styleLintOptions = [
"node_modules/stylelint/bin/stylelint.mjs",
"**/*.css",
"--report-needless-disables",
];
if (process.argv.includes("--fix")) {
styleLintOptions.push("--fix");
}
const esLintProcess = startNode(esLintOptions, { stdio: "inherit" });
esLintProcess.on("close", function (esLintCode) {
if (esLintCode !== 0) {
done(new Error("ESLint failed."));
return;
}
const styleLintProcess = startNode(styleLintOptions, { stdio: "inherit" });
styleLintProcess.on("close", function (styleLintCode) {
if (styleLintCode !== 0) {
done(new Error("Stylelint failed."));
return;
}
console.log("files checked, no errors found");
done();
});
});
});
gulp.task(
"lint-chromium",
gulp.series(
function scriptingLintChromium() {
const defines = {
...DEFINES,
CHROME: true,
SKIP_BABEL: false,
TESTING: false,
};
return buildDefaultPreferences(defines, "lint-chromium/");
},
async function prefsLintChromium() {
await parseDefaultPreferences("lint-chromium/");
},
function runLintChromium(done) {
console.log();
console.log("### Checking supplemental Chromium files");
if (
!checkChromePreferencesFile(
"extensions/chromium/preferences_schema.json",
getDefaultPreferences("lint-chromium/")
)
) {
done(new Error("chromium/preferences_schema is not in sync."));
return;
}
done();
}
)
);
gulp.task(
"dev-sandbox",
gulp.series(
function scriptingDevSandbox() {
const defines = { ...DEFINES, GENERIC: true, TESTING: true };
return createTemporaryScriptingBundle(defines, {
disableVersionInfo: true,
});
},
function createDevSandbox() {
console.log();
console.log("### Building development sandbox");
const defines = { ...DEFINES, GENERIC: true, TESTING: true };
const sandboxDir = BUILD_DIR + "dev-sandbox/";
rimraf.sync(sandboxDir);
return createSandboxBundle(defines, {
disableVersionInfo: true,
}).pipe(gulp.dest(sandboxDir));
}
)
);
gulp.task(
"server",
gulp.parallel(
function watchLocale() {
gulp.watch(
"l10n/**/*.ftl",
{ ignoreInitial: false },
gulp.series("locale")
);
},
function watchDevSandbox() {
gulp.watch(
[
"src/pdf.{sandbox,sandbox.external,scripting}.js",
"src/scripting_api/*.js",
"src/shared/scripting_utils.js",
"external/quickjs/*.js",
],
{ ignoreInitial: false },
gulp.series("dev-sandbox")
);
},
async function createServer() {
console.log();
console.log("### Starting local server");
const { WebServer } = await import("./test/webserver.mjs");
const server = new WebServer();
server.port = 8888;
server.start();
}
)
);
gulp.task(
"proxyServerX",
gulp.parallel(
function watchLocale() {
gulp.watch(
"l10n/**/*.ftl",
{ ignoreInitial: false },
gulp.series("locale")
);
},
function watchDevSandbox() {
gulp.watch(
[
"src/pdf.{sandbox,sandbox.external,scripting}.js",
"src/scripting_api/*.js",
"src/shared/scripting_utils.js",
"external/quickjs/*.js",
],
{ ignoreInitial: false },
gulp.series("dev-sandbox")
);
},
async function createServer() {
connect.server({
root: ["./"],
host: "127.0.0.1",
port: 8888,
livereload: true,
middleware() {
return [
createProxyMiddleware("/v1", {
target: "http://localhost:8181",
// target: "http://121.40.43.153:8080/",
changeOrigin: true,
}),
];
},
});
}
)
);
gulp.task("clean", function (done) {
console.log();
console.log("### Cleaning up project builds");
rimraf(BUILD_DIR, done);
});
gulp.task("importl10n", async function () {
const { downloadL10n } = await import("./external/importL10n/locales.mjs");
console.log();
console.log("### Importing translations from mozilla-central");
if (!fs.existsSync(L10N_DIR)) {
fs.mkdirSync(L10N_DIR);
}
await downloadL10n(L10N_DIR);
});
function ghPagesPrepare() {
console.log();
console.log("### Creating web site");
rimraf.sync(GH_PAGES_DIR);
return merge([
gulp
.src(GENERIC_DIR + "**/*", { base: GENERIC_DIR, removeBOM: false })
.pipe(gulp.dest(GH_PAGES_DIR)),
gulp
.src(GENERIC_LEGACY_DIR + "**/*", {
base: GENERIC_LEGACY_DIR,
removeBOM: false,
})
.pipe(gulp.dest(GH_PAGES_DIR + "legacy/")),
gulp
.src(JSDOC_BUILD_DIR + "**/*", { base: JSDOC_BUILD_DIR })
.pipe(gulp.dest(GH_PAGES_DIR + "api/draft/")),
]);
}
gulp.task("wintersmith", async function () {
const { default: wintersmith } = await import("wintersmith");
const env = wintersmith("docs/config.json");
return new Promise((resolve, reject) => {
env.build(GH_PAGES_DIR, function (error) {
if (error) {
reject(error);
return;
}
replaceInFile(
GH_PAGES_DIR + "/getting_started/index.html",
/STABLE_VERSION/g,
config.stableVersion
);
console.log("Done building with wintersmith.");
resolve();
});
});
});
gulp.task(
"web",
gulp.series(
"generic",
"generic-legacy",
"jsdoc",
ghPagesPrepare,
"wintersmith"
)
);
function packageJson() {
const VERSION = getVersionJSON().version;
const DIST_NAME = "pdfjs-dist";
const DIST_DESCRIPTION = "Generic build of Mozilla's PDF.js library.";
const DIST_KEYWORDS = ["Mozilla", "pdf", "pdf.js"];
const DIST_HOMEPAGE = "http://mozilla.github.io/pdf.js/";
const DIST_BUGS_URL = "https://github.com/mozilla/pdf.js/issues";
const DIST_LICENSE = "Apache-2.0";
const npmManifest = {
name: DIST_NAME,
version: VERSION,
main: "build/pdf.mjs",
types: "types/src/pdf.d.ts",
description: DIST_DESCRIPTION,
keywords: DIST_KEYWORDS,
homepage: DIST_HOMEPAGE,
bugs: DIST_BUGS_URL,
license: DIST_LICENSE,
optionalDependencies: {
canvas: "^2.11.2",
"path2d-polyfill": "^2.0.1",
},
browser: {
canvas: false,
fs: false,
http: false,
https: false,
url: false,
},
repository: {
type: "git",
url: DIST_REPO_URL,
},
engines: {
node: ">=18",
},
};
return createStringSource(
"package.json",
JSON.stringify(npmManifest, null, 2)
);
}
gulp.task(
"dist-pre",
gulp.series(
"generic",
"generic-legacy",
"components",
"components-legacy",
"image_decoders",
"image_decoders-legacy",
"minified",
"minified-legacy",
"types",
function createDist() {
console.log();
console.log("### Cloning baseline distribution");
rimraf.sync(DIST_DIR);
mkdirp.sync(DIST_DIR);
safeSpawnSync("git", ["clone", "--depth", "1", DIST_REPO_URL, DIST_DIR]);
console.log();
console.log("### Overwriting all files");
rimraf.sync(path.join(DIST_DIR, "*"));
return merge([
packageJson().pipe(gulp.dest(DIST_DIR)),
gulp
.src("external/dist/**/*", {
base: "external/dist",
removeBOM: false,
})
.pipe(gulp.dest(DIST_DIR)),
gulp.src(GENERIC_DIR + "LICENSE").pipe(gulp.dest(DIST_DIR)),
gulp
.src(GENERIC_DIR + "web/cmaps/**/*", { base: GENERIC_DIR + "web" })
.pipe(gulp.dest(DIST_DIR)),
gulp
.src(GENERIC_DIR + "web/standard_fonts/**/*", {
base: GENERIC_DIR + "web",
})
.pipe(gulp.dest(DIST_DIR)),
gulp
.src([
GENERIC_DIR + "build/{pdf,pdf.worker,pdf.sandbox}.mjs",
GENERIC_DIR + "build/{pdf,pdf.worker,pdf.sandbox}.mjs.map",
])
.pipe(gulp.dest(DIST_DIR + "build/")),
gulp
.src([
GENERIC_LEGACY_DIR + "build/{pdf,pdf.worker,pdf.sandbox}.mjs",
GENERIC_LEGACY_DIR + "build/{pdf,pdf.worker,pdf.sandbox}.mjs.map",
])
.pipe(gulp.dest(DIST_DIR + "legacy/build/")),
gulp
.src(MINIFIED_DIR + "build/pdf.mjs")
.pipe(rename("pdf.min.mjs"))
.pipe(gulp.dest(DIST_DIR + "build/")),
gulp
.src(MINIFIED_DIR + "build/pdf.worker.mjs")
.pipe(rename("pdf.worker.min.mjs"))
.pipe(gulp.dest(DIST_DIR + "build/")),
gulp
.src(MINIFIED_DIR + "build/pdf.sandbox.mjs")
.pipe(rename("pdf.sandbox.min.mjs"))
.pipe(gulp.dest(DIST_DIR + "build/")),
gulp
.src(MINIFIED_DIR + "image_decoders/pdf.image_decoders.mjs")
.pipe(rename("pdf.image_decoders.min.mjs"))
.pipe(gulp.dest(DIST_DIR + "image_decoders/")),
gulp
.src(MINIFIED_LEGACY_DIR + "build/pdf.mjs")
.pipe(rename("pdf.min.mjs"))
.pipe(gulp.dest(DIST_DIR + "legacy/build/")),
gulp
.src(MINIFIED_LEGACY_DIR + "build/pdf.worker.mjs")
.pipe(rename("pdf.worker.min.mjs"))
.pipe(gulp.dest(DIST_DIR + "legacy/build/")),
gulp
.src(MINIFIED_LEGACY_DIR + "build/pdf.sandbox.mjs")
.pipe(rename("pdf.sandbox.min.mjs"))
.pipe(gulp.dest(DIST_DIR + "legacy/build/")),
gulp
.src(MINIFIED_LEGACY_DIR + "image_decoders/pdf.image_decoders.mjs")
.pipe(rename("pdf.image_decoders.min.mjs"))
.pipe(gulp.dest(DIST_DIR + "legacy/image_decoders/")),
gulp
.src(COMPONENTS_DIR + "**/*", { base: COMPONENTS_DIR })
.pipe(gulp.dest(DIST_DIR + "web/")),
gulp
.src(COMPONENTS_LEGACY_DIR + "**/*", { base: COMPONENTS_LEGACY_DIR })
.pipe(gulp.dest(DIST_DIR + "legacy/web/")),
gulp
.src(IMAGE_DECODERS_DIR + "**/*", { base: IMAGE_DECODERS_DIR })
.pipe(gulp.dest(DIST_DIR + "image_decoders/")),
gulp
.src(IMAGE_DECODERS_LEGACY_DIR + "**/*", {
base: IMAGE_DECODERS_LEGACY_DIR,
})
.pipe(gulp.dest(DIST_DIR + "legacy/image_decoders/")),
gulp
.src(TYPES_DIR + "**/*", { base: TYPES_DIR })
.pipe(gulp.dest(DIST_DIR + "types/")),
]);
}
)
);
gulp.task(
"dist-install",
gulp.series("dist-pre", function createDistInstall(done) {
let distPath = DIST_DIR;
const opts = {};
const installPath = process.env.PDFJS_INSTALL_PATH;
if (installPath) {
opts.cwd = installPath;
distPath = path.relative(installPath, distPath);
}
safeSpawnSync("npm", ["install", distPath], opts);
done();
})
);
gulp.task(
"dist",
gulp.series("dist-pre", function createDist(done) {
const VERSION = getVersionJSON().version;
console.log();
console.log("### Committing changes");
let reason = process.env.PDFJS_UPDATE_REASON;
// Attempt to work-around the broken link, see https://github.com/mozilla/pdf.js/issues/10391
if (typeof reason === "string") {
const reasonParts =
/^(See )(mozilla\/pdf\.js)@tags\/(v\d+\.\d+\.\d+)\s*$/.exec(reason);
if (reasonParts) {
reason =
reasonParts[1] +
"https://github.com/" +
reasonParts[2] +
"/releases/tag/" +
reasonParts[3];
}
}
const message =
"PDF.js version " + VERSION + (reason ? " - " + reason : "");
safeSpawnSync("git", ["add", "*"], { cwd: DIST_DIR });
safeSpawnSync("git", ["commit", "-am", message], { cwd: DIST_DIR });
safeSpawnSync("git", ["tag", "-a", "v" + VERSION, "-m", message], {
cwd: DIST_DIR,
});
console.log();
console.log("Done. Push with");
console.log(
" cd " + DIST_DIR + "; git push --tags " + DIST_REPO_URL + " master"
);
console.log();
done();
})
);
gulp.task(
"mozcentralbaseline",
gulp.series(createBaseline, function createMozcentralBaseline(done) {
console.log();
console.log("### Creating mozcentral baseline environment");
// Create a mozcentral build.
rimraf.sync(BASELINE_DIR + BUILD_DIR);
const workingDirectory = path.resolve(process.cwd(), BASELINE_DIR);
safeSpawnSync("gulp", ["mozcentral"], {
env: process.env,
cwd: workingDirectory,
stdio: "inherit",
});
// Copy the mozcentral build to the mozcentral baseline directory.
rimraf.sync(MOZCENTRAL_BASELINE_DIR);
mkdirp.sync(MOZCENTRAL_BASELINE_DIR);
gulp
.src([BASELINE_DIR + BUILD_DIR + "mozcentral/**/*"])
.pipe(gulp.dest(MOZCENTRAL_BASELINE_DIR))
.on("end", function () {
// Commit the mozcentral baseline.
safeSpawnSync("git", ["init"], { cwd: MOZCENTRAL_BASELINE_DIR });
safeSpawnSync("git", ["add", "."], { cwd: MOZCENTRAL_BASELINE_DIR });
safeSpawnSync("git", ["commit", "-m", '"mozcentral baseline"'], {
cwd: MOZCENTRAL_BASELINE_DIR,
});
done();
});
})
);
gulp.task(
"mozcentraldiff",
gulp.series(
"mozcentral",
"mozcentralbaseline",
function createMozcentralDiff(done) {
console.log();
console.log("### Creating mozcentral diff");
// Create the diff between the current mozcentral build and the
// baseline mozcentral build, which both exist at this point.
// The mozcentral baseline directory is a Git repository, so we
// remove all files and copy the current mozcentral build files
// into it to create the diff.
rimraf.sync(MOZCENTRAL_BASELINE_DIR + "*");
gulp
.src([BUILD_DIR + "mozcentral/**/*"])
.pipe(gulp.dest(MOZCENTRAL_BASELINE_DIR))
.on("end", function () {
safeSpawnSync("git", ["add", "-A"], { cwd: MOZCENTRAL_BASELINE_DIR });
const diff = safeSpawnSync(
"git",
["diff", "--binary", "--cached", "--unified=8"],
{ cwd: MOZCENTRAL_BASELINE_DIR }
).stdout;
createStringSource(MOZCENTRAL_DIFF_FILE, diff)
.pipe(gulp.dest(BUILD_DIR))
.on("end", function () {
console.log(
"Result diff can be found at " +
BUILD_DIR +
MOZCENTRAL_DIFF_FILE
);
done();
});
});
}
)
);
gulp.task("externaltest", function (done) {
console.log();
console.log("### Running test-fixtures.js");
safeSpawnSync("node", ["external/builder/test-fixtures.mjs"], {
stdio: "inherit",
});
console.log();
console.log("### Running test-fixtures_esprima.js");
safeSpawnSync("node", ["external/builder/test-fixtures_esprima.mjs"], {
stdio: "inherit",
});
done();
});
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gu_yongqiang/pdf.js-4.0.379.git
[email protected]:gu_yongqiang/pdf.js-4.0.379.git
gu_yongqiang
pdf.js-4.0.379
pdf.js-4.0.379
master

搜索帮助