简体   繁体   English

rollup.js中的rxjs不会导出“主题”

[英]'Subject' is not exported by rxjs in rollup.js

I am trying to set up my project to use rollup, as part of an angular2 move to AOT compilation, however, I am getting the following issue. 我正在尝试将我的项目设置为使用汇总,作为angular2移动到AOT编译的一部分,但是,我遇到了以下问题。

Error: 'Subject' is not exported by node_modules\\rxjs\\Subject.js 错误:node_modules \\ rxjs \\ Subject.js不会导出“Subject”

This is my rollup.js file: 这是我的rollup.js文件:

import rollup from 'rollup';
import nodeResolve from 'rollup-plugin-node-resolve'
import commonjs    from 'rollup-plugin-commonjs';
import uglify      from 'rollup-plugin-uglify'

export default {
  entry: 'client/main.js',
  dest: 'public/assets/js/build.js',
  sourceMap: false,
  format: 'iife',
  plugins: [
      nodeResolve({jsnext: true, module: true}),
      commonjs({
        include: 'node_modules/rxjs/**',
        include: 'node_modules/angular2-jwt/**',
      }),
      uglify()
  ]
}

Why is this happening, I have followed the angular2 cookbook guide? 为什么会这样,我跟着angular2食谱指南?

You'll need to use the namedExports option with rollup-plugin-commonjs: https://github.com/rollup/rollup-plugin-commonjs#custom-named-exports . 您需要将namedExports选项与rollup-plugin-commonjs一起使用: https//github.com/rollup/rollup-plugin-commonjs#custom-named-exports

Also, you may find it useful to include: 'node_modules/**' rather than individual packages, as otherwise any dependencies of your dependencies will bypass the plugin (in the config above, you have duplicate include properties – perhaps that's just a typo? If you need to pass multiple values, use an array). 此外,您可能会发现include: 'node_modules/**'内容非常有用include: 'node_modules/**'而不是单个包,否则依赖项的任何依赖项都会绕过插件(在上面的配置中,您有重复的include属性 - 也许这只是一个错字?如果需要传递多个值,请使用数组)。

commonjs({
  include: 'node_modules/**',
  namedExports: {
    'node_modules/rxjs/Subject.js': [ 'Subject' ]
  }
})

I finally figured this out on my system. 我终于在我的系统上找到了这个。

The named export solution is wrong since rollup-plugin-commonjs will handle the exports in Subject.js just fine. 命名导出解决方案是错误的,因为rollup-plugin-commonjs将处理Subject.js中的导出就好了。

The problem for me was the "includes" option in rollup-plugin-commonjs. 对我来说问题是rollup-plugin-commonjs中的“includes”选项。

There are two issues. 有两个问题。

Number one: when specifying the includes in the options as "node_modules/rxjs/**" you have to be sure the full path resolves to where you expect it to. 第一:当将选项中的包含指定为“node_modules / rxjs / **”时,您必须确保完整路径解析到您期望的位置。

Example: 例:

I run my build command from "c:/foo/build" but my files are in "c:/my-source" then the include patterns might resolve to "c:/build/node_modules" which means when the commonjs plugin is checking if it should handle "node_modules/rxjs/ " it will see that "c:/my-source/node_modules/rxjs/ " does not match "c:/build/node_modules/rxjs/**" thus it will not convert the exports to ES6. 我从“c:/ foo / build”运行我的构建命令,但我的文件在“c:/ my-source”中,然后包含模式可能会解析为“c:/ build / node_modules”,这意味着当commonjs插件正在检查时如果它应该处理“node_modules / rxjs / ”,它将看到“c:/ my-source / node_modules / rxjs / ”与“c:/ build / node_modules / rxjs / **”不匹配,因此它不会转换导出到ES6。

The second issue is case sensitivity. 第二个问题是区分大小写。 The include patterns are case sensitive. 包含模式区分大小写。 This tripped me up too. 这也让我感到沮丧。

Both of these issues can be confirmed by opening the "node_modules\\rollup-plugin-commonjs\\dist\\rollup-plugin-commonjs.cjs.js" file and debugging the "transform" function. 通过打开“node_modules \\ rollup-plugin-commonjs \\ dist \\ rollup-plugin-commonjs.cjs.js”文件并调试“转换”功能,可以确认这两个问题。

If you modify the code (at the very beginning of the transform function) to be something like this 如果你修改代码(在转换函数的最开头)是这样的

if (id.includes('rxjs\\Subject.js')) {
        debugger;
}

and then run the code in a debugger, you can step through the code until it gets to the filter function. 然后在调试器中运行代码,您可以逐步执行代码,直到它到达过滤器函数。 There you will see the filter function is skipping the "rxjs/Subject.js" file. 在那里你会看到过滤器功能正在跳过“rxjs / Subject.js”文件。

I almost guarantee this is the problem when the error occurs. 我几乎保证这是错误发生时的问题。

我发现这可以与符号链接结合使用。

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

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