3

I am struggling with my angular library development. First of all, I am building an angular 8 library (current cli version 9) and I want to live see my library code changes while I am serving the demo app in which I am using this library.

Current behavior: When I am making changes on library source code, angular demo application reloads but I am non seeing any changes to take affect. This behavior was mainly applied with css changes and not every time on a .ts file.

Expected behavior: To being able to see every change I am making on the library source code.

Current angular.json file

    {
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "bDataTableDemo": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/bDataTableDemo",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": false,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "src/styles.scss"
            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.min.js"
            ]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "bDataTableDemo:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "bDataTableDemo:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "bDataTableDemo:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "bDataTableDemo:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "bDataTableDemo:serve:production"
            }
          }
        }
      }
    },
    "ng-bootstrap-table": {
      "projectType": "library",
      "root": "projects/ng-bootstrap-table",
      "sourceRoot": "projects/ng-bootstrap-table/src",
      "prefix": "lib",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-ng-packagr:build",
          "options": {
            "tsConfig": "projects/ng-bootstrap-table/tsconfig.lib.json",
            "project": "projects/ng-bootstrap-table/ng-package.json"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "projects/ng-bootstrap-table/src/test.ts",
            "tsConfig": "projects/ng-bootstrap-table/tsconfig.spec.json",
            "karmaConfig": "projects/ng-bootstrap-table/karma.conf.js"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "projects/ng-bootstrap-table/tsconfig.lib.json",
              "projects/ng-bootstrap-table/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "bDataTableDemo"
}

And my tsconfig.json file

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "ng-bootstrap-table": [
        "dist/ng-bootstrap-table"
      ],
      "ng-bootstrap-table/*": [
        "dist/ng-bootstrap-table/*"
      ]
    }
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

What should I change in order to have the expected behavior?

P.S. I am new to angular libraries, so I can't find a solution yet.

Thank you in advance!!

Anshita Singh
  • 1,583
  • 1
  • 17
  • 40
atheodos
  • 131
  • 12

2 Answers2

1

You should edit the paths in tsconfig to point to your library's index.ts instead of dist/ - which is the built artifact generated with ng build my-library:

"my-library" : [ "libs/my-library/index.ts" ]
Fahd Lihidheb
  • 671
  • 4
  • 20
  • Thank you for your reply! I have tried this by adding an index.ts file and export all from puplic-api.ts file, in the src folder of my lib, but seems not working at all. May I have other errors eg in the way I am importing the lib in my demo project? – atheodos Feb 15 '20 at 21:33
  • Yes! But anything was changed :/ – atheodos Feb 15 '20 at 22:15
0

Well, I have been working on this approach and, I have been able to see the library change in my app using npm-link

  1. On your library
  • Open the terminal and execute npm run build
  • Then, navigate on the terminal to the dist/pkg folder and run the command npm link
  • Copy the library name exposed in the result from the npm link
  1. On your app
  • On the angular.json file search the path projects.project-name.architect.build.options and, add the following property: "preserveSymlinks": true
  • Open the terminal and run the command npm link <name-of-your-library>
HarleySG
  • 161
  • 1
  • 4