'How to exclude the folder but include all of its files and sub-folder while npm publish in angular library

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1