sapphi-red / vite-plugin-static-copy

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

[ERROR] "vite-plugin-static-copy" resolved to an ESM file. ESM file cannot be loaded by `require`. #139

Closed Robotboy36 closed 1 week ago

Robotboy36 commented 1 week ago

[ERROR] "vite-plugin-static-copy" resolved to an ESM file. ESM file cannot be loaded by require. See https://vite.dev/guide/troubleshooting.html#this-package-is-esm-only for more details. [plugin externalize-deps]


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

plugins: [
            vue(),
            ViteMpaPlugin(),
            viteStaticCopy({
                targets: [
                    {
                        src: 'src/static/images/*',  // 静态资源来源目录
                        dest: 'assets/images', // 输出目录,相对于 dist
                    }
                ]
            }),
        ],
Robotboy36 commented 1 week ago

"vite-plugin-static-copy": "^2.1.0"

Robotboy36 commented 1 week ago

不明所以

sapphi-red commented 1 week ago

Please read the error message and the linked page.

"vite-plugin-static-copy" resolved to an ESM file. ESM file cannot be loaded by require. See https://vitejs.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.

Robotboy36 commented 1 week ago

试过了, 不行

Robotboy36 commented 1 week ago

import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import path from 'path' import autoprefixer from 'autoprefixer' import ViteMpaPlugin from 'vite-plugin-multiple-entry'; import { viteStaticCopy } from 'vite-plugin-static-copy'

// https://vitejs.dev/config/ export default defineConfig(({ command, mode, ssrBuild }) => { console.log('构建环境', command, mode);

const config = {
    base: mode === 'development' ? '/' : '/',
    productionSourceMap: false,
    integrity: true,
    crossorigin: 'anonymous',
    define: {
        ENV: JSON.stringify(mode),
    },
    resolve: {
        alias: {
            '@': path.join(__dirname, './src')
        }
    },
    plugins: [
        vue(),
        ViteMpaPlugin(),
        viteStaticCopy({
            targets: [
                {
                    src: 'src/static/images/*',  // 静态资源来源目录
                    dest: 'assets/images', // 输出目录,相对于 dist
                }
            ]
        }),
    ],
    css: {
        preprocessorOptions: {
            scss: {
                api: 'modern-compiler'
            }
        },
        postcss: {
            plugins: [
                autoprefixer({
                    overrideBrowserslist: ['Android 4.1', 'iOS 7.1', 'Chrome > 31', 'ff > 31', 'ie >= 8'],
                }),
            ],
        },
    },
    server: {
        port: 8080,
        strictPort: true,
        hmr: true, // 开启热更新
    },
    build: {
        outDir: path.join(__dirname, 'dist'),
        target: 'es2015',
        emptyOutDir: true,
        chunkSizeWarningLimit: 500,
        terserOptions: {
            compress: {
                drop_console: mode === 'production',
                drop_debugger: mode === 'production',
            },
        },
    },
};

return config;

})

sapphi-red commented 1 week ago

I need a reproduction to answer it in that case.