简体   繁体   English

从 rollup-plugin-copy (vite.js) 中排除子目录

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

Im trying to integrate pdftron into an existing vite.js build.我试图将pdftron集成到现有的 vite.js 构建中。

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.但所有文件加起来超过 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 .下面是我的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: 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"
  }
}

After further investigation, I found a solution.经过进一步调查,我找到了解决方案。 This is my new rollup-plugin-copy -configuration这是我的新rollup-plugin-copy

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 .请注意,我将钩子更改为writeBundle ,因此当emptyOutDir设置为true时,vite 不会删除所有内容。

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

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