在 Angular 中加载咖啡脚本模块

Load coffee script module in Angular

我有一个 Angular-CLI 应用程序,我尝试引入第三方依赖项,它是用 Coffee 编写的脚本。这就是我在我的组件中所做的:

const CoffeeWidget = require('coffee-loader!CoffeeWidget');

我认为使用咖啡装载机会奏效。但不是真的。现在我可以阅读 index.coffee,但在我的 index.coffeerequire 其他咖啡文件。喜欢:

Cup = require '../tools/cup.coffee'

但是它在准备好 cup.coffee 时遇到问题并说:You may need an appropriate loader to handle this file type.

还有其他人遇到过这个问题吗?

由于您直接在 require statement 中使用 coffee-loader,webpack 将仅在所需文件上使用加载器。

为了让 webpack 在任何深度找到的任何 .coffee 文件上使用 coffee-loader,请在您的 webpack 配置中扩展 module.rules 数组:

// Webpack 2 syntax

module.exports = {
  module: {
    rules: [
      {
        test: /\.coffee$/,
        use: [ 'coffee-loader' ]
      }
    ]
  }
}