Rollup.js 不会在包中包含节点模块 discord-rpc。有没有办法做到这一点?

Rollup.js will not include the node module discord-rpc in the bundle. Is there anyway to do this?

我只导入了 discord-rpc,而且只有 main.ts。还使用汇总插件 typscript 和 node-resolve。

main.ts 导入:

const { Client } = require("discord-rpc");

package.json 脚本和依赖项:

{
  "main": "main.js",
  "scripts": {
    "dev": "rollup --config rollup.config.js -w",
    "build": "rollup --config rollup.config.js"
  },
  "devDependencies": {
    "@rollup/plugin-commonjs": "^15.1.0",
    "@rollup/plugin-json": "^4.1.0",
    "@rollup/plugin-node-resolve": "^9.0.0",
    "@rollup/plugin-typescript": "^6.0.0",
    "@types/node": "^14.14.2",
    "obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
    "rollup": "^2.32.1",
    "tslib": "^2.0.3",
    "typescript": "^4.0.3"
  },
  "dependencies": {
    "discord-rpc": "^3.1.4"
  }
}

rollup.config.js:

import typescript from '@rollup/plugin-typescript';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

export default {
  input: [
    'main.ts',
  ],
  output: {
    dir: '.',
    sourcemap: 'inline',
    format: 'cjs',
    exports: 'default'
  },
  external: ['obsidian', 'electron'],
  plugins: [
    typescript(),
    commonjs(),
    nodeResolve({browser: true}),
  ]
};

我需要使用 rollup.js 的替代品吗?

编辑: 我尝试将 const { Client } = require("discord-rpc"); 更改为 import { Client } from "discord-rpc"; 但我仍然得到 Error: Cannot find module 'discord-rpc'

编辑 2: 保留此更改:

import { Client } from "discord-rpc";

我编辑了 rollup.config.js:

import typescript from '@rollup/plugin-typescript';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

export default {
  input: [
    'main.ts',
  ],
  output: {
    dir: '.',
    sourcemap: 'inline',
    format: 'cjs',
    exports: 'default'
  },
  external: ['obsidian', 'electron'],
  plugins: [
    nodeResolve({browser: true}), // Moving this up.
    typescript(),
    commonjs(),
  ]
};

运行时不再出现此错误:

Error: Cannot find module 'discord-rpc'

但我在构建过程中得到了这个:

(!) Unresolved dependencies
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
timers (imported by node_modules/discord-rpc/src/client.js, timers?commonjs-external)
events (imported by node_modules/discord-rpc/src/client.js, events?commonjs-external, node_modules/discord-rpc/src/transports/ipc.js, node_modules/discord-rpc/src/transports/websocket.js)

它也没有按预期工作,但我不确定这是由于我的代码还是汇总...

好的,我解决了问题!

我不得不移动一些东西: rollup.config.js:

import typescript from '@rollup/plugin-typescript';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

export default {
  input: [
    'main.ts',
  ],
  output: {
    dir: '.',
    sourcemap: 'inline',
    format: 'cjs',
    exports: 'default'
  },
  external: ['obsidian', 'electron', 'net', 'events', 'timers'],
  plugins: [
    nodeResolve({browser: true, preferBuiltins: false}),
    typescript(),
    commonjs(),
  ]
};

package.json:

{
  "dependencies": {
    "@types/discord-rpc": "^3.0.4",
    "discord-rpc": "^3.1.4"
  }
}

main.ts 进口:

import { Client } from "discord-rpc";