4

我必须将一个组件从 svelte-sapper 移动到 sapper。组件就是这样使用uuid的

import { v4 as uuidv4} from uuid

我还安装了加密。当我在控制台浏览器上使用该组件时,出现此错误

rng.js:4 未捕获类型错误:crypto.randomFillSync 不是函数

在控制台上:

bundles src/index.js → dist/index.mjs, dist/index.js, public/build/bundle.js...
(!) Missing global variable names
Use output.globals to specify browser global variable names corresponding to external modules
crypto (guessing 'crypto')
crypto (guessing 'crypto')

我已经用 npm 安装了加密,该功能在 bundle.js 上

const rnds8 = new Uint8Array(16); function rng() { return crypto.randomFillSync(rnds8); }

我看到我需要在 rollup.config.js 内部使用插件元素

resolve({ browser: true }),

但它没有用。有什么建议么?


这是我的汇总

import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import pkg from './package.json';

const production = !process.env.ROLLUP_WATCH;

const name = pkg.name
    .replace(/^(@\S+\/)?(svelte-)?(\S+)/, '$3')
    .replace(/^\w/, m => m.toUpperCase())
    .replace(/-\w/g, m => m[1].toUpperCase());

export default {
    input: 'src/index.js',
    output: [
        { file: pkg.module, 'format': 'es' },
        { file: pkg.main, 'format': 'umd', name },
        {
            sourcemap: true,
            format: 'iife',
            name: 'app',
            file: 'public/build/bundle.js'
        }
    ],
    plugins: [
        svelte({
            // enable run-time checks when not in production
            dev: !production,
            // we'll extract any component CSS out into
            // a separate file - better for performance
            css: css => {
                css.write('public/build/bundle.css');
            }
        }),
        resolve({
            browser: true
        }),
        commonjs(),
        // In dev mode, call `npm run start` once
        // the bundle has been generated
        !production && serve()
    ]
};

function serve() {
    let started = false;

    return {
        writeBundle() {
            if (!started) {
                started = true;

                require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
                    stdio: ['ignore', 'inherit', 'inherit'],
                    shell: true
                });
            }
        }
    };
}
4

1 回答 1

0

我有和你一样的错误。通过添加解决它

import nodePolyfills from 'rollup-plugin-node-polyfills';

然后添加

nodePolyfills(),
resolve({
  browser: true,
})

这为我删除了警告

于 2021-05-26T12:52:33.110 回答