简体   繁体   English

尝试NPM使用Angular-CLI发布并安装自定义angular2组件; 只在第一次编译

[英]Trying to NPM publish and install a custom angular2 component with Angular-CLI; only compiles the first time

I'm having a most unusual and frustrating problem. 我遇到了一个最不寻常且令人沮丧的问题。

I have an Ng2 component called via-date-picker that I am trying to NPM publish so that it can be easily used in other projects. 我正在尝试NPM发布Ng2组件,称为via-date- picker,以便可以在其他项目中轻松使用。 In order to do so, I have made it into an Angular2 component library. 为了做到这一点,我将其放入Angular2组件库。 The via-date-picker exports a module called ViaDatePickerModule , which I want to import elsewhere. via-date- picker导出了一个名为ViaDatePickerModule的模块,我想将其导入其他地方。

In order to test and make sure that it is being published correctly, I am NPM-installing and importing it into an otherwise empty Angular-CLI project that I am calling npm-test . 为了测试并确保它被正确发布,我正在安装NPM并将其导入另一个我叫npm-test的空Angular-CLI项目中。

So I run my npm-test application using "ng serve", and I get this error: 所以我使用“ ng serve”运行我的npm-test应用程序,并且出现此错误:

ERROR in ViaDatePickerModule is not an NgModule webpack: Failed to compile ViaDatePickerModule中的错误不是NgModule Webpack:无法编译

Yet despite that error, the project compiles anyway: 尽管存在该错误,该项目还是会编译:

有错误,但仍然可以编译

And when I open up my project, lo and behold, everything works!! 当我打开项目时,瞧瞧,一切正常!!

But this only happens the first time that I run the project. 但这仅在我一次运行项目时发生。 On successive attempts to run the project via "ng serve", I get the same compilation error, but this time the project just flat-out refuses to complete it's compilation: 通过“ ng serve”连续尝试运行项目时,我遇到了相同的编译错误,但是这次只是平淡无奇的项目拒绝完成其编译:

在此处输入图片说明

I have no idea why I'm getting this error, and why Angular-CLI will run my project sometimes but not others. 我不知道为什么会收到此错误,为什么Angular-CLI有时会运行我的项目,而其他人不会运行。

I've scoured the web for answers and tried every solution I can find for this error, as well as every other thing I can think of: 我在网上搜寻了答案,并尝试了可以​​找到的解决此错误的所有方法,以及我能想到的所有其他方法:

  • I've tried adjusting the tsConfig settings in my component library 我尝试过调整组件库中的tsConfig设置
  • I've tried using rollup.js instead of gulp.js to build my component library 我尝试使用rollup.js而不是gulp.js构建我的组件库
  • I've tried copying existing, working component libraries, then carefully swapping out the existing code for my own 我尝试过复制现有的有效组件库,然后小心地将现有代码换为自己的
  • I've downgraded Angular CLI 我已将Angular CLI降级
  • I've upgraded Angular CLI 我已经升级了Angular CLI
  • I've downgrade Typescript 我已将打字稿降级
  • I've upgraded Typescript 我已经升级打字稿
  • I've deleted and re-installed node_modules several times 我已经多次删除并重新安装了node_modules
  • I've deleted and re-started my whole project twice 我已删除并重新启动了整个项目两次

No matter what I do, I keep coming to the same webpack error that I posted above; 无论我做什么,我都会遇到上面发布的同一webpack错误; that ViaDatePickerModule is not an NgModule. ViaDatePickerModule不是NgModule。 I'm completely out of ideas. 我完全没有主意。 Any help that anyone could provide would be crazy helpful. 任何人都可以提供的任何帮助都是疯狂的帮助。

For the sake of complete thoroughness, I've created a public repo on github here containing all the files involves, divided into two main directories: 为了完整起见,我在github上创建了一个公共仓库,其中包含所有涉及的文件,分为两个主要目录:

  • COMPONENT_BEFORE_PUBLISHING: contains the component library from which I am running "npm publish" COMPONENT_BEFORE_PUBLISHING:包含我正在运行的组件库“ npm publish”
  • WHAT_IS_IMPORTED_INTO_NODE_MODULES: contains the resulting directory that is being imported into the node_modules directory of my npm-test project WHAT_IS_IMPORTED_INTO_NODE_MODULES:包含要导入到我的npm-test项目的node_modules目录中的结果目录

Again, any help that anyone could provide would be extremely, extremely appreciated! 同样,任何人都可以提供的任何帮助将非常非常感谢! Really, I would be eternally grateful. 真的,我将永远感激不已。

If you are %100 sure that ALL of your consumers will import your components, modules ...etc from a TS project such as angular-cli . 如果您100%确定所有消费者都将从TS项目(例如angular-cli导入组件,模块... angular-cli You can publish your TS source directly without transpiling. 您可以直接发布TS源,而无需进行编译。 Ie. 就是 you'll be publishing static .ts files that can be imported in any project that will do the transpiling for you. 您将发布静态.ts文件,这些文件可以导入将为您进行转码的任何项目中。

However, if your want your library to also be consumed as a JS es5 or es6 module , then you should transpile. 但是,如果您还希望将库作为JS es5es6模块使用 ,则应该进行转换。

Also, you can try the angular compiler ngc instead of the typescript compiler tsc ? 另外,您可以尝试使用角度编译器ngc而不是打字稿编译器tsc ngc is a wrapper around tsc . ngctsc的包装器。 You could start there, There are many library starters put there that can help you start an angular library and get it optimized for AOT compilation. 您可以从这里开始。有很多库启动器可以帮助您启动角度库并使它针对AOT编译进行优化。

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

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