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

Rollup.js: undefined objects in external dependencies

本文关键字:未定义 对象 依赖 js 外部 汇总      更新时间:2023-09-26

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

我使用了一些外部依赖项:图表.js,crypto-js,秘银,时刻复数

所有这些都与jspm完美配合。我决定尝试汇总以加快速度,因为 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',
    };

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

谢谢。

编辑

我已经做了一个简单的测试-复制捆绑那些在我的应用程序中产生错误的库。

包.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"
  }
}

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',
}

主.js

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

var text = Application.run()
console.log(`Testing encryption: ${text}`)
console.log(`Testing pluralization: ${pluralize('person')}`)

应用.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

运行上述操作将生成错误。

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

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

我的代码与您的代码的唯一共同点是两者都使用加密,特别是crypto-js 3.1.2汇总"hmac-sha256.js",并且代码不是导入而是原始的。

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