汇总和 Redux:"index.js does not export default"

Rollup and Redux: "index.js does not export default"

我已将我的 ionic 应用程序从 beta 11 更新到 rc0。所以这意味着我已经从 angular2 rc4 切换到 angular2 stable,从 typescript 1.8 切换到 2 并使用 rollupjs 模块捆绑器。

我有一个 redux 架构并且正在使用 Redux 库。

我无法编译并收到此错误:

Error: Module c:\XXX\node_modules\redux\node_modules\symbol-observable\index.js does not export default (imported by c:\XXX\node_modules\redux\es\createStore.js)

at Module.trace (c:\XXX\node_modules\rollup\dist\rollup.js:7677:29)

at ModuleScope.findDeclaration (c:\XXX\node_modules\rollup\dist\rollup.js:7300:22)

at Scope.findDeclaration (c:\XXX\node_modules\rollup\dist\rollup.js:5351:39)

at Scope.findDeclaration (c:\XXX\node_modules\rollup\dist\rollup.js:5351:39)

at Identifier.bind (c:\XXX\node_modules\rollup\dist\rollup.js:6489:29)

at c:\XXX\node_modules\rollup\dist\rollup.js:5151:50

at MemberExpression.eachChild (c:\XXX\node_modules\rollup\dist\rollup.js:5168:5)

at MemberExpression.bind (c:\XXX\node_modules\rollup\dist\rollup.js:5151:7)

at MemberExpression.bind (c:\XXX\node_modules\rollup\dist\rollup.js:6693:24)

at c:\XXX\node_modules\rollup\dist\rollup.js:5151:50

有人知道发生了什么事吗?有人可以给我一些指示吗?我真的不明白如何处理这个。

这是我在 rollup.config.js

var ngTemplate = require('../dist/plugins/ng-template').ngTemplate;
var nodeResolve = require('rollup-plugin-node-resolve');
var commonjs = require('rollup-plugin-commonjs');

// https://github.com/rollup/rollup/wiki/JavaScript-API

module.exports = {
  /**
   * entry: The bundle's starting point. This file will
   * be included, along with the minimum necessary code
   * from its dependencies
   */
  entry: './.tmp/app/main.dev.js',

  /**
   * sourceMap: If true, a separate sourcemap file will
   * be created.
   */
  sourceMap: true,

  /**
   * format: The format of the generated bundle
   */
  format: 'iife',

  /**
   * dest: the output filename for the bundle in the buildDir
   */
  dest: 'main.js',

  // Add this to avoid Eval errors
  useStrict: false,

  /**
   * plugins: Array of plugin objects, or a single plugin object.
   * See https://github.com/rollup/rollup/wiki/Plugins for more info.
   */
  plugins: [
    ngTemplate(),
    commonjs({
        include: [
        'node_modules/rxjs/**',
        'node_modules/firebase/**',
        'node_modules/angularfire2/**'
        ],
        namedExports: {
        'node_modules/firebase/firebase.js': ['initializeApp', 'auth', 'database'],
        'node_modules/angularfire2/node_modules/firebase/firebase-browser.js': ['initializeApp', 'auth', 'database']
        }
    }),
    nodeResolve({
      module: true,
      jsnext: true,
      main: true,
      browser: true,
      extensions: ['.js']
    })
  ]

};

错误的第一部分...

Error: Module c:\XXX\node_modules\redux\node_modules\symbol-observable\index.js does not export default

...表示 symbol-observable\index.js 没有 export default 语句。那是因为它是一个 CommonJS 模块——它的全部内容如下:

module.exports = require('./lib/index');

因此您需要将其从 CommonJS 转换为 ES。您已经设置了 rollup-plugin-commonjs,所以希望这只是重新配置它的一个例子。目前,只有 rxjsfirebaseangularfire2 中的模块正在转换。

通常最好只包含所有 node_modules,因为这样您就不必手动包含依赖关系,例如 symbol-observable:

plugins: [
ngTemplate(),
commonjs({
  include: [
    'node_modules/**'
  ],
  namedExports: {
    'node_modules/firebase/firebase.js': ['initializeApp', 'auth', 'database'],
    'node_modules/angularfire2/node_modules/firebase/firebase-browser.js': ['initializeApp', 'auth', 'database']
  }
}),
nodeResolve(...)

node_modules 不是 CommonJS 的任何文件都将原封不动地通过插件。