sapphi-red / vite-plugin-static-copy

rollup-plugin-copy with dev server support.
MIT License
297 stars 32 forks source link

filter files to copy #56

Closed obiot closed 1 year ago

obiot commented 1 year ago

Using Webpack previously and the CopyWebPackPlugin, it was possible to filter files to copy at runtime, see: https://github.com/melonjs/es6-boilerplate/blob/01e95671e1be85e4f8a272aa7ddfb3d969f6adc6/webpack.config.js#L43-L59

Would it be possible to add a similar option ?

Else thank you for this plugin !

sapphi-red commented 1 year ago

I think that's possible with the transform option.

transform could return null as a way to tell the plugin not to copy the file, this is similar to the CopyWebpackPlugin#filter option, but it expects transform to return the original content in case you want it to be copied.

obiot commented 1 year ago

Oh i missed that option, is there a proper documentation or example for it somewhere?

[EDIT], just tried and unfortunately I stumbled on the same issue described in issue #4:

error during build:
Error: "transform" option only supports a file: 'src/data/bgm' is not a file

our template does have sub-directories by default, and we copy the whole data folder to the build folder: https://github.com/melonjs/typescript-boilerplate#folder-structure

sapphi-red commented 1 year ago

Then, I'll close this one as a duplicate of #4 👍

obiot commented 1 year ago

if I "flatten" the directory structure, then I got the below to work :

  viteStaticCopy({
      targets: [
        {
          src: "src/data/*",
          dest: "data/",
          transform: async (contents, filename) => {
            // add your custom extension here if not listed
        var texture = /\.(jpe?g|gif|png|svg|heic|avif|webp|pkm|pvr)$/;
        var fnt = /\.(woff|woff2|ttf|fnt)$/;
        var map = /\.(tmx|tmj|tsx|tsj)$/;
        var audio = /\.(wav|mp3|mpeg|opus|ogg|oga|wav|aac|caf|m4a|m4b|mp4|weba|webm|dolby|flac)$/;
        var misc = /\.(xml|bin|glsl|ym|json|js)$/;

       // only copy production files
       var ret = texture.test(filename) || fnt.test(filename) || map.test(filename) || audio.test(filename) || misc.test(filename);

       if (ret === false) {
        return null;
        }
            return contents;
          }
        }

however the plugin count returns the total amount of file (copied) even for those where I returned null :

computing gzip size (1)...[vite-plugin-static-copy] Copied 6 items.

should have been 3 in my example, so there is one improvement as well there potentially. even maybe display something like : computing gzip size (1)...[vite-plugin-static-copy] Copied 3 out of 6 items.