1

When I build my angular library app i get the artifacts in the folder dist/. This means when the library is imported in another application it is done like this:

import { XXX } from 'libname/dist'

However, I would like the the import statement to be without dist folder. I can't change the directory from where the npm publish command is run so would like a solution (if possible) using .npmignore or package.json (files or main) properties.

Is this possible? The same question is posted here but I am not sure how to remove the dist folder and keep files/sub-folders in the package using npmignore, files and main properties.

Vsevolod Golovanov
  • 4,068
  • 3
  • 31
  • 65
Angad
  • 1,144
  • 3
  • 18
  • 39

1 Answers1

0

Your library must have this configuration in angular.json under architect > options > project:

"my-library": {
  "projectType": "library",
 ...
  "architect": {
    "build": {
      "builder": "@angular-devkit/build-ng-packagr:build",
      "options": {
 ...
        "project": "projects/my-library/ng-package.json"
      },
 ...

And your ng-package.json file must be:

{
  "$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
  "dest": "../../dist/my-library",
  "lib": {
    "entryFile": "src/public-api.ts",
    "umdModuleIds": {
    }
  }
}

Also you must have a public-api.ts file:

// public-api.ts
/*
 * Public API Surface of my-library
 */

export * from './lib/my-library.module';
...

Do you have all those files and configurations?

These are all configurations that should take place automatically when you create a library. For reference you can watch: https://angular.io/guide/creating-libraries

UPDATE:

"Currently I am copying my package.json into the dist folder and then running npm pack inside the dist folder."

With a gulp batch you can copy package.json from root to dist:

// /gulpfile.js 
var gulp = require('gulp');

gulp.task('default', [], function() {
  gulp.src(['../../my-library/package.json']) // pay attenction to your relative path where you run gulp.
      .pipe(gulp.dest('my-library/dist'));
});

To run it:

gulp default

In the gulp file directory.

And then try with npm pack in dist folder.

Sandman
  • 1,480
  • 7
  • 23
  • There files are all there. So once a build is run all files are generated into dist folder. I have a parent project with project.json file which is where npm pack is run. I copy the dist folder generated in the library to root path as dist/** but when npm run pack is initiated it takes dist folder also which is what i want to exclude. – Angad May 05 '20 at 13:29
  • Try with a gulp solution. I modified my answer to include an example of a gulp task. – Sandman May 05 '20 at 13:47
  • AS i said before this is running via CI Jenkins file and I can't changed the location i.e. cd dist to run the npm pack. I can change the parent level package.json file or add npmignore file – Angad May 05 '20 at 14:30