viterollupjspdftron

Exclude subdirectory from rollup-plugin-copy (vite.js)


Im trying to integrate pdftron into an existing vite.js build.

As the mentioned in the link above, I copied all necessary assets into my output-folder. But all the files sum up to more than 300MB. So I want to exclude some feature which I don't need. The vendor ships a script for optimizing the library, but it's not possible to run the optimization script automated right now.

Anyhow, I want to mimic the behavior of the optimization script by removing the biggest unnecessary folders from the build. But somehow I'm unable to exclude the subdirectories from beeing copied. I guess there is a very stupid mistake within my configuration. Below there is an extract from my vite.config.js. I tried multiple different ways of excluding the directory (as you can see it turned into a guessing game). Nothing worked so far.

If necessary, I will provide a demo project.

import copy from "rollup-plugin-copy";


const outputDir = 'build';
/**
 * @type {import('vite').UserConfig}
 */
const config = {
  build: {
    outDir: outputDir,
    emptyOutDir: false,
    assetsDir: '.',
    lib: {
      entry: 'index.js',
      name: 'my-lib',
      fileName: 'my-lib.js',
      formats: ['es']

    },

    rollupOptions: {
      output: {
        entryFileNames: `my-lib.js`,
        chunkFileNames: `[name].js`,
        assetFileNames: `[name].[ext]`
      },
      plugins: [copy({
        targets: [
          {
            src: ['node_modules/@pdftron/webviewer/public/*', '!core/pdf/full/*'],
            dest: outputDir + '/pdftron1'
          },
          {
            src: ['node_modules/@pdftron/webviewer/public', '!node_modules/@pdftron/webviewer/public/core/pdf/full/*'],
            dest: outputDir + '/pdftron2'
          },
          {
            src: ['node_modules/@pdftron/webviewer/public', '!node_modules/@pdftron/webviewer/public/core/pdf/full'],
            dest: outputDir + '/pdftron3'
          },
          {
            src: ['node_modules/@pdftron/webviewer/public', '!node_modules/@pdftron/webviewer/public/core/pdf/full/'],
            dest: outputDir + '/pdftron4'
          },
          {
            src: ['node_modules/@pdftron/webviewer/public', '!node_modules/@pdftron/webviewer/public/core/pdf/full/**'],
            dest: outputDir + '/pdftron5'
          },
        ]
      })
      ]

    },

  },
}

export default config

package.json:

{
  "name": "pdftron-vite-example",
  "version": "1.0.0",
  "scripts": {
    "build": "vite build"
  },
  "dependencies": {
    "@pdftron/webviewer": "8.7.0"
  },

  "devDependencies": {
    "vite": "^2.9.13",
    "rollup-plugin-copy": "^3.3.0"
  }
}

Solution

  • After further investigation, I found a solution. This is my new rollup-plugin-copy-configuration

    copy({
        targets: [
          {
            src: [
              'node_modules/@pdftron/webviewer/public/core/assets'
              , 'node_modules/@pdftron/webviewer/public/core/external'
              , 'node_modules/@pdftron/webviewer/public/core/pdf'
              , '!node_modules/@pdftron/webviewer/public/core/pdf/full'
              , 'node_modules/@pdftron/webviewer/public/core/*'
              , 'node_modules/@pdftron/webviewer/public/ui/assets'
              , 'node_modules/@pdftron/webviewer/public/ui/chunks'
              , 'node_modules/@pdftron/webviewer/public/ui/i18n'
              , 'node_modules/@pdftron/webviewer/public/ui/*'
            ],
    
            dest: outputDir,
            expandDirectories: true,
            onlyFiles: true,
          }
        ],
        flatten: false,
        hook: 'writeBundle'
      })
    

    The solution is based on this issue.

    Notice I changed the hook to writeBundle, so vite will not delete everything when emptyOutDir is set to true.