How optimize & reduce lib folder for WebViewer when installing though NPM

In our current project build i do install Webviewer through NPM and include the node_modules/@pdftron/webviewer/public in the project during the build process.
This increases the project size considerably with approximately 60MB.

I read the article Optimize & reduce lib folder for WebViewer. But when trying to run this script it failed due to the fact that it could not find the lib folder. This folder seems anly to be available in the download packaged and not when installing through npm.
When changing the lib folder in the optimize script to public the optimization fails on because the file PDFNet.prod.js was not available. Although the optimize script is available in the npm packaged, it seems that it is not adapted to work properly when running from the npm package.

Because the build is automated the interactive optimization script is also not really usable.
I can use the download version, run a manual optimization, and than include the optimized version as an asset in the project. This is not a desirable option because this will increase the repository size substantially.

Is there a supported way, or at least documented how to optimize the lib folder manually. So i can use npm and during the build phase remove redundant files from webviewer/lib folder ?

Cheers,
Peter

Hello,

Unfortunately our optimize script is not configured to work in the npm version of WebViewer at this time.
We are looking to potentially add support for it down the road.

As a workaround for now, please do the following:

  1. rename the public folder to “lib”
  2. in the optimize script, comment the line of code that has the word “PDFNet.js” in it.

Sorry for the inconvenience.

Anthony Chen

Hi Anthony,

Thanks for the clarification. Still running an interactive script in the build process is not an option.
The current project relies on Grunt and therefore added the following grunt-contrib-copy task to the build process.
This copies all the necessary files and folders for the Webviewer without Office OOXML support and Chrome/Edge Chromium support only. This reduced the deployed Webviewer sources from 65MB to 8.3MB

    npm_static_assets: [
      /* Interpretation of PDFtron optimize-for-prod.js and
       * https://www.pdftron.com/documentation/web/guides/optimizing-lib-folder/#optimize-via-script
       */
      /* Base PDFTron files */
      {
        src: '*',
        cwd: 'node_modules/@pdftron/webviewer/public/core',
        dest: 'build/assets/webviewer/core',
        expand: true
      },
      {
        src: '**/*',
        cwd: 'node_modules/@pdftron/webviewer/public/core/assets',
        dest: 'build/assets/webviewer/core/assets',
        expand: true
      },
      {
        src: '**/*',
        cwd: 'node_modules/@pdftron/webviewer/public/core/external',
        dest: 'build/assets/webviewer/core/external',
        expand: true
      },
      /*  Base /pdf files */
      {
        src: ['pdfnet.res', 'PDFworker.js', 'ResizableWorker.js', 'iframe.html', 'WasmThread.js'],
        cwd: 'node_modules/@pdftron/webviewer/public/core/pdf',
        dest: 'build/assets/webviewer/core/pdf',
        expand: true
      },
      /* /pdf/lean ThreadedWasm files for webviewer only */
      {
        src: '*ThreadedWasm*',
        cwd: 'node_modules/@pdftron/webviewer/public/core/pdf/lean',
        dest: 'build/assets/webviewer/core/pdf/lean',
        filter: 'isFile',
        expand: true
      },
      {
        src: ['MemGrow.wasm', 'MemGrow.worker.js'],
        cwd: 'node_modules/@pdftron/webviewer/public/core/pdf/simple_wasm',
        dest: 'build/assets/webviewer/core/pdf/simple_wasm',
        filter: 'isFile',
        expand: true
      },
      /* ui folder */
      {
        src: '**/*',
        cwd: 'node_modules/@pdftron/webviewer/public/ui',
        dest: 'build/assets/webviewer/ui',
        expand: true
      }
    ]

Thank-you for telling me your concern.
I just want to make sure things are clear so that we can address this backlog item properly later down the road.

Is the issue:
The npm run optimize script is not working for you when you use the npm version of WebViewer?

Or is the issue:
You would like a non-interactive script for both the standalone and npm version of WebViewer?

Thanks,
Anthony

Hi Anthony,

Both points mentioned are an issue. If the optimize script is delivered with the NPM package it should work with the NPM packaged. Otherwise there is no use of adding it to the package. I think currently the best option is to remove the script from the NPM package since it modifies the content of node_modules/@pdftron outside the control of NPM, which can result in unexpected behaviour.

A non-interactive version of the script would be very usefull, but should not modify the package in-place but export it to a specified location.

Cheers,
Peter

Thank-you for providing more clarifications.
I have noted down your feedback in our backlog.

Thanks,
Anthony