ERROR in Must have a source file to refactor
Asked Answered
U

3

5

Strange error with webpack compilation:

ERROR in Must have a source file to refactor.

Looking at the source code I found this message in ./node_modules/@ngtools/refactor.js:

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

The configuration of @ngtools webpack plugin is pretty straightforward:

  {
    test: /\.ts$/,
    use: '@ngtools/webpack',
  } 
Upthrow answered 30/3, 2018 at 14:18 Comment(2)
were you updated @angular/cli to 6 beta version, I started seeing this issue when I was using ngIvy with angular 6 rcConfirm
I have the same issue. Did you fix the problem?Charlacharlady
F
3

Don't forget to include AngularCompilerPlugin with correct mainPath configuration in webpack.config.js. At least that's what fixed this for me.

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")
  }),
...
]
Frontage answered 21/5, 2018 at 15:54 Comment(0)
R
3

For anyone in pain, attempting to find this solution; merely change:

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

..to this:

new AngularCompilerPlugin({
      mainPath: 'main.ts',<----
      tsConfigPath: 'src/tsconfig.app.json',
      skipCodeGeneration: true
    }),
Rozamond answered 13/9, 2018 at 19:32 Comment(0)
S
0

I received this error while running "ng build --aot=true" on an angular library. The reason was the missing files in the build configuration in angular.json file. The project was generated as an angular application instead of a library. The build section in angular.json was pointing to src/main.ts and src/style.css but there were no such files in the project. I added missing files to resolve the issue. Alternatively configuration can be changed to a library project.

"build": {
  "builder": "@angular-devkit/build-angular:browser",
  "options": {
    "outputPath": "dist/ui-common",
    "index": "src/index.html",
    "main": "src/main.ts", //missing file
Supercargo answered 30/1, 2020 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.