node.js - 如何在 Svelte 中导入加密货币?
问题描述
我创建了一个基本的苗条项目(它使用汇总)。我想像这样将本机库、加密导入项目中。
// src/lib/encrypt.js
import { randomBytes } from "crypto";
const encrypt = () => {
return randomBytes(4);
};
export { encrypt };
并将其导入到一个像这样的苗条文件中,
// src/App.svelte
<script>
import {encrypt} from './lib/encrypt';
</script>
<main>
<p>{encrypt()}</p>
</main>
这样做,产量,
Uncaught TypeError: crypto.randomBytes is not a function
at encrypt (encrypt.js:4)
at Object.create [as c] (App.svelte:9)
at init (index.mjs:1496)
at new App (App.svelte:3)
at main.js:3
如何启用加密以适用于我的场景?
注意:这可能与汇总的工作方式有关。经过一番研究,我发现它rollup-plugin-node-builtins
用于加载本机模块。这似乎也不起作用。我尝试配置 rollup.config.js
// rollup.config.js
import svelte from "rollup-plugin-svelte";
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import livereload from "rollup-plugin-livereload";
import { terser } from "rollup-plugin-terser";
import css from "rollup-plugin-css-only";
import preprocess from "svelte-preprocess";
import builtins from "rollup-plugin-node-builtins";
const production = !process.env.ROLLUP_WATCH;
function serve() {
let server;
function toExit() {
if (server) server.kill(0);
}
return {
writeBundle() {
if (server) return;
server = require("child_process").spawn(
"npm",
["run", "start", "--", "--dev"],
{
stdio: ["ignore", "inherit", "inherit"],
shell: true,
}
);
process.on("SIGTERM", toExit);
process.on("exit", toExit);
},
};
}
export default {
input: "src/main.js",
output: {
sourcemap: true,
format: "iife",
name: "app",
file: "public/build/bundle.js",
},
plugins: [
svelte({
preprocess: preprocess(),
compilerOptions: {
// enable run-time checks when not in production
dev: !production,
},
}),
css({ output: "bundle.css" }),
resolve({
browser: true,
dedupe: ["svelte"],
}),
builtins({ crypto: true }),
commonjs(),
!production && serve(),
!production && livereload("public"),
production && terser(),
],
watch: {
clearScreen: false,
},
};
这产生了,
[!] Error: Unexpected token (Note that you need @rollup/plugin-json to import JSON files)
解决方案
推荐阅读
- timer - 串行监视器上的计时器值
- microsoft-graph-api - 找不到列出电子邮件回复的方法
- random - 如何在 AMPL 中生成随机参数
- angularjs - 对于搜索框,如果“在 ENTER 上搜索”,哪种实现方法正确?以 ng-submit 或 n-keypress 的形式使用它?
- c# - 根据角色和某些条件在 mvc 视图中渲染特定部分
- sql - 写入查询以迭代就业数据表并执行服务持续时间的总和
- r - 使用 Caret 训练不同的 ML 模型
- python - 为什么我在 Python 中使用 ctypes 从 C 函数中得到错误的地址
- docker - 谷歌云运行容器网络
- reporting-services - 具有多列的 SSRS 分组