sapphi-red / vite-plugin-static-copy

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

watcher: Cannot read properties of undefined (reading 'close') #63

Closed QuentiumYT closed 1 year ago

QuentiumYT commented 1 year ago

Hello, I newly installed this plugin and see TypeError: Cannot read properties of undefined (reading 'close') when I started copying.

The copy does work correctly, it's just at the end that it throws this error

Error details below

TypeError: Cannot read properties of undefined (reading 'close') at Object.closeBundle (/home/quentin/MEGAsync/JavaScript//node_modules/vite-plugin-static-copy/dist/index.cjs:1070:21) at file:///home/quentin/MEGAsync/JavaScript//node_modules/rollup/dist/es/shared/node-entry.js:25544:40 at async Promise.all (index 0) at async PluginDriver.hookParallel (file:///home/quentin/MEGAsync/JavaScript//node_modules/rollup/dist/es/shared/node-entry.js:25472:9) at async Object.close (file:///home/quentin/MEGAsync/JavaScript//node_modules/rollup/dist/es/shared/node-entry.js:26726:13) at async build (file:///home/quentin/MEGAsync/JavaScript//node_modules/vite/dist/node/chunks/dep-df561101.js:48021:13) at async CAC. (file:///home/quentin/MEGAsync/JavaScript//node_modules/vite/dist/node/cli.js:822:9)

It is caused by

async closeBundle() {
  await watcher.close();
}

My dependencies

"devDependencies": {
    "vite": "^4.4.9",
    "vite-plugin-static-copy": "^0.17.0"
}
sapphi-red commented 1 year ago

No errors happen on my machine: https://stackblitz.com/edit/vitejs-vite-iv4jd6?file=package.json

Would you create a reproduction?

QuentiumYT commented 1 year ago

I narrowed down the issue and I'm getting this issue with this simple vite config

import { viteStaticCopy } from 'vite-plugin-static-copy';

/** @type {import('vite').UserConfig} */
export default {
  build: {
    rollupOptions: {
      input: {
        main: 'main.js',
      },
      output: {
        entryFileNames: 'assets/[name].js',
      },
      plugins: [
        viteStaticCopy({
          targets: [
            {
              src: './foo/*',
              dest: './foo',
            },
          ],
        }),
      ],
    },
  },
};
sapphi-red commented 1 year ago

I see. The error is happening because you're putting the plugin in build.rollupOptions.plugins instead of plugins. A correct config would be like this:

import { viteStaticCopy } from 'vite-plugin-static-copy';

/** @type {import('vite').UserConfig} */
export default {
  plugins: [
    viteStaticCopy({
      targets: [
        {
          src: './foo/*',
          dest: './foo',
        },
      ],
    }),
  ],
  build: {
    rollupOptions: {
      input: {
        main: 'main.js',
      },
      output: {
        entryFileNames: 'assets/[name].js',
      },
    },
  },
};
QuentiumYT commented 1 year ago

May I ask why the plugins in rollup doesn't work but outside it does? I don't know what changes between the two

sapphi-red commented 1 year ago

Rollup plugins can be placed under build.rollupOptions.plugins but Vite plugins cannot be placed there. https://vitejs.dev/guide/api-plugin.html#rollup-plugin-compatibility:~:text=If%20a%20Rollup%20plugin%20only%20makes%20sense%20for%20the%20build%20phase%2C%20then%20it%20can%20be%20specified%20under%20build.rollupOptions.plugins%20instead.%20It%20will%20work%20the%20same%20as%20a%20Vite%20plugin%20with%20enforce%3A%20%27post%27%20and%20apply%3A%20%27build%27

QuentiumYT commented 1 year ago

Ohhh I didn't know about that, but the fact that it worked even in the wrong key made me feel like it was correct haha