sapphi-red / vite-plugin-static-copy

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

Compatibility with Nuxt 3? #38

Closed stephiescastle closed 1 year ago

stephiescastle commented 2 years ago

Great plugin! We've been using it with Nuxt 3, and vite-plugin-static-copy@0.9.0 worked great with nuxt@3.0.0-rc.8. However, now that Nuxt 3 stable has been released, we're having trouble getting it to work.

Configuration

On a mac using node v18.12.1:

"nuxt": "3.0.0",
"vite-plugin-static-copy": "^0.12.0"
// nuxt.config.ts
import { viteStaticCopy } from "vite-plugin-static-copy";
import path from "node:path";

export default defineNuxtConfig({
  vite: {
    plugins: [
      viteStaticCopy({
        targets: [
          {
            src: path.resolve(__dirname, "./path/to/colors.svg"),
            dest: "copied-files/",
          },
        ],
      }),
    ],
  },
});

Results

On npm run dev, the files aren't copied, and no error is shown.

On npm run build, the following error occurs, and the app fails to build due to missing asset colors.svg:

 ERROR  [vite-plugin-static-copy] Copy count was not set.  

 ERROR  [vite]: Rollup failed to resolve import "/copied-files/colors.svg" from "app.vue".                                                                          09:37:10
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`

 ERROR  [vite]: Rollup failed to resolve import "/copied-files/colors.svg" from "app.vue".                                                                          09:37:10
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`

  This is most likely unintended because it can break your application at runtime.
  If you do want to externalize this module explicitly add it to
  `build.rollupOptions.external`
  at onRollupWarning (node_modules/vite/dist/node/chunks/dep-67e7f8ab.js:45526:19)
  at onwarn (node_modules/vite/dist/node/chunks/dep-67e7f8ab.js:45297:13)
  at Object.onwarn (node_modules/rollup/dist/es/shared/rollup.js:23263:13)
  at ModuleLoader.handleResolveId (node_modules/rollup/dist/es/shared/rollup.js:22158:26)
  at node_modules/rollup/dist/es/shared/rollup.js:22119:26
sapphi-red commented 2 years ago

For dev, I guess this is a bug in nuxt. https://github.com/nuxt/nuxt.js/issues/15735

For build, maybe replacing

<template>
  <img src="/copied-files/colors.svg" />
</template>

with

<template>
  <img :src="'/copied-files/colors.svg'" />
</template>

works?

stephiescastle commented 1 year ago

Confirming that the suggested workaround for build does work. Looking forward to a fix for https://github.com/nuxt/nuxt.js/issues/15735!

sapphi-red commented 1 year ago

Closing as I confirmed the bug in nuxt was fixed in nuxt 3.1.1. https://stackblitz.com/edit/github-hqd9rq-mjmrun?file=nuxt.config.ts