简体   繁体   English

system.js为pdf.js构建配置

[英]system.js build config for pdf.js

I'm trying to build pdf.js in order to embed the viewer into an app I'm working on. 我正在尝试构建pdf.js,以便将查看器嵌入到我正在开发的应用中。

I want to build the viewer by hand so it can be included in the application that is getting packaged by webpack. 我想手动构建查看器,以便可以将其包含在webpack打包的应用程序中。

The application entrypoint, index.js, has the line 应用程序入口点index.js包含以下行

import viewer from 'pdfjs-dist/web/pdf_viewer';

This results in the viewer being included in the transpiled app code, however the pdf.js viewer uses System.js to load modules that it needs to run. 这导致查看器包含在已转换的应用程序代码中,但是pdf.js查看器使用System.js加载需要运行的模块。

In the compiled version that Mozilla serves, the code has been transpiled to not use System.js; 在Mozilla服务的已编译版本中,代码已被编译为不使用System.js。 see view-source: https://mozilla.github.io/pdf.js/web/viewer.js in the the webViewerLoad function on line 3774. 请参阅第3774行的webViewerLoad函数中的view-source: https ://mozilla.github.io/pdf.js/web/viewer.js。

function webViewerLoad() {
    var config = getViewerConfiguration();
    window.PDFViewerApplication = pdfjsWebApp.PDFViewerApplication;
    pdfjsWebApp.PDFViewerApplication.run(config);
}

This differs from the non-transpiled source that can be found on https://github.com/mozilla/pdf.js/blob/master/web/viewer.js#L178 , and the source map for the Mozilla hosted viewer retains these SystemJS lines. 这不同于可在https://github.com/mozilla/pdf.js/blob/master/web/viewer.js#L178上找到的非编译源,并且Mozilla托管查看器的源映射保留了这些内容。 SystemJS行。

function webViewerLoad() {
    let config = getViewerConfiguration();
    if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) {
        Promise.all([
            SystemJS.import('pdfjs-web/app'),
            SystemJS.import('pdfjs-web/genericcom'),
            SystemJS.import('pdfjs-web/pdf_print_service'),
        ]).then(function([app, ...otherModules]) {
            window.PDFViewerApplication = app.PDFViewerApplication;
            app.PDFViewerApplication.run(config);
        });
    } else {
        window.PDFViewerApplication = pdfjsWebApp.PDFViewerApplication;
        pdfjsWebApp.PDFViewerApplication.run(config);
    }
}

What I want to know is how this was achieved, and how to replicate the configuration to build this. 我想知道的是如何实现的,以及如何复制配置来构建它。

The production viewer.js has been compiled with webpack. 生产的viewer.js已使用webpack进行了编译。 In gulpfile.js you will find the following block: 在gulpfile.js中,您将找到以下块:

function createWebBundle(defines) {
  var viewerOutputName = 'viewer.js';

  var viewerFileConfig = createWebpackConfig(defines, {
    filename: viewerOutputName,
  });
  return gulp.src('./web/viewer.js')
             .pipe(webpack2Stream(viewerFileConfig));
}

In my case I simply installed pdfjs-dist and imported pdfjs-dist/web/pdf_viewer. 就我而言,我只安装了pdfjs-dist并导入了pdfjs-dist / web / pdf_viewer。 Building with webpack worked fine. 使用webpack构建工作正常。 Getting the web worker to work right required some extra effort. 使网络工作者正常工作需要付出额外的努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM