Rollup.js:外部依赖项中未定义的对象

r.s*_*cky 5 javascript rollupjs

我最近开始玩 rollupjs。根据可用文档配置所有内容并将其捆绑在一起后,我从外部库中收到了许多有关未定义对象的错误。此类错误:Cannot read property 'parse' of undefined来自 crypto-js。它抱怨代码中的这一行:var ciphertext = Base64.parse(openSSLStr)。所以Base64是未定义的。我很少遇到来自捆绑的不同外部库的类似错误。

我使用了一些外部依赖项:chart.js、crypto-js、mithril、moment、pluralize

它们都与 完美配合jspm。我决定尝试 rollup 来加快速度,因为 jspm 目前太慢了。现在我的一半外部依赖项停止工作。我只收到来自外部库的“未定义的东西”和“...不是函数”类型的错误。

可能是什么原因造成的?

这是我的 rollup.config.js

import babel from 'rollup-plugin-babel';
import npm from 'rollup-plugin-npm';
import commonjs from 'rollup-plugin-commonjs';
import uglify from 'rollup-plugin-uglify';

    export default {
      entry: 'app/scripts/application/main.js',
      format: 'cjs',
      plugins: [
        npm({
          jsnext: true,
          main: true,
        }),
        babel({
          exclude: 'node_modules/**',
          presets: [ 'es2015-rollup' ],
        }),
        commonjs(),
        uglify(),
      ],
      dest: 'static/js/application.js',
    };
Run Code Online (Sandbox Code Playgroud)

如果需要任何其他详细信息,请告诉我。

谢谢。

编辑

我已经完成了一个简单的测试重现,将那些在我的应用程序中生成错误的库捆绑在一起。

包.json

{
  "name": "minion",
  "private": true,
  "babel": {
    "presets": [
      "es2015-rollup"
    ]
  },
  "dependencies": {
    "chart.js": "^1.0.2",
    "crypto-js": "^3.1.6",
    "mithril": "^0.2.2-rc.1",
    "moment": "^2.11.1",
    "pluralize": "^1.2.1"
  },
  "devDependencies": {
    "babel-preset-es2015-rollup": "^1.1.1",
    "rollup-plugin-babel": "^2.3.9",
    "rollup-plugin-commonjs": "^2.2.0",
    "rollup-plugin-npm": "^1.3.0",
    "rollup-plugin-uglify": "^0.1.0"
  }
}
Run Code Online (Sandbox Code Playgroud)

rollup.config.js

import babel from 'rollup-plugin-babel';
import npm from 'rollup-plugin-npm';
import commonjs from 'rollup-plugin-commonjs';
import uglify from 'rollup-plugin-uglify';

export default {
  entry: 'app/main.js',
  format: 'cjs',
  plugins: [
    npm({
      jsnext: true,
      main: true,
    }),
    babel({
      exclude: 'node_modules/**',
      presets: [ 'es2015-rollup' ],
    }),
    commonjs(),
    //uglify(),
  ],
  dest: 'static/js/app.js',
}
Run Code Online (Sandbox Code Playgroud)

main.js

import Application from './application'
import pluralize from 'pluralize'


var text = Application.run()

console.log(`Testing encryption: ${text}`)
console.log(`Testing pluralization: ${pluralize('person')}`)
Run Code Online (Sandbox Code Playgroud)

应用程序.js

import crypt from 'crypto-js'

var Application = {
    run() {
      var ciphertext = crypt.AES.encrypt('Testing encryption...', 'password')
      var bytes  = crypt.AES.decrypt(ciphertext.toString(), 'password')
      return bytes.toString(crypt.enc.Utf8)
    }
}

export default Application
Run Code Online (Sandbox Code Playgroud)

运行上面的命令会产生错误。

小智 0

只是推测:也许是汇总和/或加密的错误。

当我尝试在 Node Red 中运行 js 函数时,我遇到了类似的错误,当我在本地运行它时,js 没问题,但TypeError: Cannot read property 'split' of undefined在远程运行时它会抛出异常。

我的代码与您的代码唯一的共同点是都使用加密技术,特别是 crypto-js 3.1.2 rollup“hmac-sha256.js”,并且代码不是导入的而是原始的。

即使删除了“split”的唯一实例,我也无法解决它(但继续在本地运行)