使用angular 2应用程序构建安装crypto-js无法正常工作

时间:2018-07-11 07:07:00

标签: angular build

我有一个angular 2应用程序,其中安装了crypto-js来通过以下命令对字符串进行加密和解密。

  

npm安装crypto-js

在本地一切都很好,但是当我进行构建时,它给了我以下错误

Uncaught ReferenceError: require is not defined

这是我的systemjs.config.js

 // map tells the System loader where to look for things
    map: {
        // other
        'crypto-js': 'npm:crypto-js',
    }
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
        // other
        'crypto-js': {
            format: 'cjs',
            defaultExtension: 'js',
            main: './crypto-js.js'
        }
    }   

这是我的rollup-config.js

export default {
  entry: 'app/main.js',
  dest: 'dist/build.js', // output a single application bundle
  sourceMap: false,
  format: 'iife',
  external: [ 'moment', 'crypto-js' ],
  globals : {
    'moment' : 'moment',
    'crypto-js' : 'CryptoJS',
  },
}

这是我使用此组件的地方

import * as CryptoJS from 'crypto-js';
var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), 'abcdefghijklmnop');

0 个答案:

没有答案