简体   繁体   English

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

[英]Rollup.js: undefined objects in external dependencies

I recently started playing with rollupjs. 我最近开始玩rollupjs。 After configuring everything as per available docs and bundling things up, I got many errors from my external libraries about undefined objects. 根据可用的文档配置所有内容并捆绑后,我从外部库中收到许多关于未定义对象的错误。 This sort of errors: Cannot read property 'parse' of undefined coming from crypto-js. 这种错误: Cannot read property 'parse' of undefined来自crypto-js Cannot read property 'parse' of undefined It complains about this line in the code: var ciphertext = Base64.parse(openSSLStr) . 它在代码中抱怨这一行: var ciphertext = Base64.parse(openSSLStr) So Base64 is undefined. 所以Base64未定义。 I have few errors like this from different external libraries bundled in. 我从捆绑的不同外部库中得到的错误很少。

I use a handful of external dependencies: chart.js, crypto-js, mithril, moment, pluralize 我使用了一些外部依赖:chart.js,crypto-js,mithril,moment,pluralize

All of them work perfectly with jspm . 所有这些都与jspm完美配合。 I decided to try rollup to speed things up as jspm is soooo slow at the moment. 我决定尝试使用汇总来加快速度,因为jspm目前很慢。 Now half of my external dependencies stopped working. 现在,我的一半外部依赖关系停止了工作。 I get "undefined things" and "...not a function" kind of errors coming from external libraries only. 我得到“未定义的东西”和“......不是函数”只有来自外部库的错误。

What could possibly be the cause of it? 可能是什么原因呢?

This is my rollup.config.js 这是我的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',
    };

Let me know if any other details are needed. 如果需要任何其他细节,请告诉我。

Thanks. 谢谢。

EDIT 编辑

I've done a simple tests-reproduction bundling those libraries that generate errors in my application. 我做了一个简单的测试 - 再现捆绑那些在我的应用程序中产生错误的库。

package.json 的package.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 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',
}

main.js 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')}`)

application.js 的application.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

Running the above will generate the errors. 运行上面的代码会产生错误。

Just speculating: Maybe is a bug of the rollup and/or crypto. 只是推测:也许是汇总和/或加密的错误。

I have a similar error when trying to run a js function in Node Red, the js is ok when I run it locally but it throws TypeError: Cannot read property 'split' of undefined when runs remotely. 尝试在Node Red中运行js函数时出现类似的错误,当我在本地运行它时,js是可以的但它会引发TypeError: Cannot read property 'split' of undefined远程运行时TypeError: Cannot read property 'split' of undefined

The only thing that my code have in common with yours is that both uses cryptography, specifically crypto-js 3.1.2 rollup "hmac-sha256.js" and the code is not imported but raw. 我的代码与你的代码唯一的共同点就是两者都使用加密技术,特别是crypto-js 3.1.2汇总“hmac-sha256.js”并且代码不是导入的而是原始的。

Even after deleting the only instance of 'split' yet I can't solve it (but keeps running locally) 即使删除了'split'的唯一实例,但我无法解决它(但仍在本地运行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM