【问题标题】:ERROR in Must have a source file to refactor必须有要重构的源文件中的错误
【发布时间】:2018-09-09 14:17:20
【问题描述】:

webpack 编译出现奇怪错误:

必须有要重构的源文件中的错误。

看源码发现./node_modules/@ngtools/refactor.js有这条消息:

...   
if (!sourceFile) {
  throw new Error('Must have a source file to refactor.');
}

@ngtools webpack 插件的配置非常简单:

  {
    test: /\.ts$/,
    use: '@ngtools/webpack',
  } 

【问题讨论】:

  • 你是否将@angular/cli 更新到了 6 beta 版本,当我使用带有 angular 6 rc 的 ngIvy 时,我开始看到这个问题
  • 我也有同样的问题。你解决问题了吗?

标签: angular webpack ngtools


【解决方案1】:

不要忘记在webpack.config.js 中包含AngularCompilerPlugin 和正确的mainPath 配置。至少这就是我解决这个问题的原因。

plugins: [
  /**
   * Angular's webpack plugin to compile typescript and AOT for templates
   **/
  new ngTools.AngularCompilerPlugin({
    tsConfigPath: path.join(CONTEXT_DIR, "tsconfig.json"),
    mainPath: path.join(CONTEXT_DIR, "src/main.ts")
  }),
...
]

【讨论】:

    【解决方案2】:

    对于任何痛苦的人,试图找到这个解决方案;只是改变:

    new AngularCompilerPlugin({
          mainPath: 'src/main.ts',<----
          tsConfigPath: 'src/tsconfig.app.json',
          skipCodeGeneration: true
        }),
    

    ..到这个:

    new AngularCompilerPlugin({
          mainPath: 'main.ts',<----
          tsConfigPath: 'src/tsconfig.app.json',
          skipCodeGeneration: true
        }),
    

    【讨论】:

      【解决方案3】:

      我在 Angular 库上运行“ng build --aot=true”时收到此错误。原因是 angular.json 文件中的构建配置中缺少文件。该项目是作为一个角度应用程序而不是一个库生成的。 angular.json 中的构建部分指向 src/main.ts 和 src/style.css 但项目中没有这样的文件。我添加了丢失的文件来解决问题。或者,可以将配置更改为库项目。

      "build": {
        "builder": "@angular-devkit/build-angular:browser",
        "options": {
          "outputPath": "dist/ui-common",
          "index": "src/index.html",
          "main": "src/main.ts", //missing file
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-02
        • 2018-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多