From c39f1aedb2e2405c91528c23c852b0b3a682f64f Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 5 Dec 2020 15:51:39 +0100 Subject: [PATCH] Re-implement working `dev-sandbox`/`watch-dev-sandbox` gulp-tasks Compared to the, previously removed, `sandbox`/`watch-sandbox` gulp-tasks, these ones should work even when run against an non-existent/empty `build`-folder. Also, to ensure that the development viewer actually works out-of-the-box, `gulp server` will now also include `gulp watch-dev-sandbox` to remove the need to *manually* invoke the build-tasks. Finally, this patch also removes the `web/devcom.js` file since it shouldn't actually be needed, assuming that the "sandbox"-loading code in the `web/genericcom.js` file is actually *correctly* implemented. --- gulpfile.js | 68 +++++++++++++++++++++++++++++++++++++++------- web/app_options.js | 16 +++++------ web/devcom.js | 43 ----------------------------- web/genericcom.js | 2 +- web/viewer.js | 3 -- 5 files changed, 67 insertions(+), 65 deletions(-) delete mode 100644 web/devcom.js diff --git a/gulpfile.js b/gulpfile.js index 9d3142c95..056cf2293 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -173,9 +173,15 @@ function createStringSource(filename, content) { function createWebpackConfig( defines, output, - { disableSourceMaps = false, disableLicenseHeader = false } = {} + { + disableVersionInfo = false, + disableSourceMaps = false, + disableLicenseHeader = false, + } = {} ) { - var versionInfo = getVersionJSON(); + const versionInfo = !disableVersionInfo + ? getVersionJSON() + : { version: 0, commit: 0 }; var bundleDefines = builder.merge(defines, { BUNDLE_VERSION: versionInfo.version, BUNDLE_BUILD: versionInfo.commit, @@ -359,8 +365,9 @@ function createScriptingBundle(defines, extraOptions = undefined) { .pipe(replaceJSRootName(scriptingAMDName, "pdfjsScripting")); } -function createTemporaryScriptingBundle(defines) { +function createTemporaryScriptingBundle(defines, extraOptions = undefined) { return createScriptingBundle(defines, { + disableVersionInfo: !!(extraOptions && extraOptions.disableVersionInfo), disableSourceMaps: true, disableLicenseHeader: true, }).pipe(gulp.dest(TMP_DIR)); @@ -1711,16 +1718,57 @@ gulp.task( }) ); -gulp.task("server", function () { - console.log(); - console.log("### Starting local server"); +gulp.task( + "dev-sandbox", + gulp.series( + function scripting() { + const defines = builder.merge(DEFINES, { GENERIC: true, TESTING: true }); + return createTemporaryScriptingBundle(defines, { + disableVersionInfo: true, + }); + }, + function () { + console.log(); + console.log("### Building development sandbox"); - var WebServer = require("./test/webserver.js").WebServer; - var server = new WebServer(); - server.port = 8888; - server.start(); + const defines = builder.merge(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("watch-dev-sandbox", function () { + gulp.watch( + [ + "src/pdf.{sandbox,scripting}.js", + "src/scripting_api/*.js", + "src/shared/scripting_utils.js", + "external/quickjs/*.js", + ], + { ignoreInitial: false }, + gulp.series("dev-sandbox") + ); }); +gulp.task( + "server", + gulp.parallel("watch-dev-sandbox", function () { + 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 (done) { console.log(); console.log("### Cleaning up project builds"); diff --git a/web/app_options.js b/web/app_options.js index d13f2026d..caf2455ca 100644 --- a/web/app_options.js +++ b/web/app_options.js @@ -223,14 +223,6 @@ const defaultOptions = { value: false, kind: OptionKind.API, }, - scriptingSrc: { - /** @type {string} */ - value: - typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION") - ? "../build/generic/build/pdf.sandbox.js" - : "../build/pdf.sandbox.js", - kind: OptionKind.VIEWER, - }, verbosity: { /** @type {number} */ value: 1, @@ -265,6 +257,14 @@ if ( value: typeof navigator !== "undefined" ? navigator.language : "en-US", kind: OptionKind.VIEWER, }; + defaultOptions.sandboxBundleSrc = { + /** @type {string} */ + value: + typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION") + ? "../build/dev-sandbox/pdf.sandbox.js" + : "../build/pdf.sandbox.js", + kind: OptionKind.VIEWER, + }; } const userOptions = Object.create(null); diff --git a/web/devcom.js b/web/devcom.js deleted file mode 100644 index 3df13f604..000000000 --- a/web/devcom.js +++ /dev/null @@ -1,43 +0,0 @@ -/* Copyright 2017 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. - */ - -import { DefaultExternalServices, PDFViewerApplication } from "./app.js"; -import { loadScript, shadow } from "pdfjs-lib"; - -const DevCom = {}; - -class DevExternalServices extends DefaultExternalServices { - static get scripting() { - const promise = loadScript("../build/pdf.sandbox.js").then(() => { - return window.pdfjsSandbox.QuickJSSandbox(); - }); - const sandbox = { - createSandbox(data) { - promise.then(sbx => sbx.create(data)); - }, - dispatchEventInSandbox(event) { - promise.then(sbx => sbx.dispatchEvent(event)); - }, - destroySandbox() { - promise.then(sbx => sbx.nukeSandbox()); - }, - }; - - return shadow(this, "scripting", sandbox); - } -} -PDFViewerApplication.externalServices = DevExternalServices; - -export { DevCom }; diff --git a/web/genericcom.js b/web/genericcom.js index f1f7dba38..73f78b045 100644 --- a/web/genericcom.js +++ b/web/genericcom.js @@ -53,7 +53,7 @@ class GenericExternalServices extends DefaultExternalServices { } static get scripting() { - const promise = loadScript(AppOptions.get("scriptingSrc")).then(() => { + const promise = loadScript(AppOptions.get("sandboxBundleSrc")).then(() => { return window.pdfjsSandbox.QuickJSSandbox(); }); const sandbox = { diff --git a/web/viewer.js b/web/viewer.js index 1bc2b35a8..d447c962a 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -56,9 +56,6 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) { require("./chromecom.js"); } -if (typeof PDFJSDev === "undefined") { - import("./devcom.js"); -} if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME || GENERIC")) { require("./pdf_print_service.js"); }