Include assets when building library using ng-packagr
Asked Answered
V

5

28

Could anyone give a hint where to start to include images and css files into Angular library using ng-packagr?

Veteran answered 27/9, 2017 at 8:27 Comment(0)
S
19

There's an official issue for that question here. It's actually quite simple to include images and the like to your library: You can just copy them to the dist folder manually after ng-packagr has done its thing.

However, it's more difficult to automate the extraction of those files in projects that'll use your library. In the issue referenced above BenjaminDobler suggests to add the following mapping to the .angular-cli.json file of any consumer project:

{ "glob": "**/*", "input": "../node_modules/my-lib/assets", "output": "./assets/my-lib/" }

I feel this is more of an NPM issue, though, and there also are bare NPM solutions for it like pkg-assets and npm-assets.

Sclerotic answered 27/10, 2017 at 19:58 Comment(0)
T
33

It's an old thread but still updating with a latest options in Feb-2020

With the ng-packagr ^9.0.1 version, You can do this using inbuilt "copy assets"

{
  "ngPackage": {
    "assets": [
      "CHANGELOG.md",
      "./styles/**/*.theme.scss"
    ],
    "lib": {
      ...
    }
  }
}

https://github.com/ng-packagr/ng-packagr/blob/master/docs/copy-assets.md

This has helped me remove the postpackage cp scripts

Adding my actual ng-package.json for benefit of others. I wanted to copy the assets folder and all its contents to the library and publish it along with it.

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

The assets folder is in the library root directory. this helps to copy the entire assets folder and the contents inside it and add it to the library, so that you can use it as @include "@node_modules/your-library/assets/styles/main.scss"

Thielen answered 23/2, 2020 at 17:12 Comment(6)
Please, a complete exampleOrchestral
Added the sample of ng-package for copying the assets folder. unfortunately I dont have a complete library as an example.Thielen
Doesn't work. I'm Angular v8.3.20 and on a projectType: library I got: ERROR: Configuration doesn't match the required schema. Data path "" should NOT have additional properties (assets).Belabor
@Belabor You need to define this in the ng-package.json of the library.Kenya
@Kenya I did it in the ng-package.json, but weirdly with the last version of angular, no files are copied... And still have then error at bundling.Blanco
@Kenya ok then.. I am trying to copy a wasm-glue.js file.. but assets are copied AFTER the bundling.. So I'm completely stuck because I don't find "hook" that allows me to copy wasm-glue.js before FESM2015 bundling happens.Blanco
S
19

There's an official issue for that question here. It's actually quite simple to include images and the like to your library: You can just copy them to the dist folder manually after ng-packagr has done its thing.

However, it's more difficult to automate the extraction of those files in projects that'll use your library. In the issue referenced above BenjaminDobler suggests to add the following mapping to the .angular-cli.json file of any consumer project:

{ "glob": "**/*", "input": "../node_modules/my-lib/assets", "output": "./assets/my-lib/" }

I feel this is more of an NPM issue, though, and there also are bare NPM solutions for it like pkg-assets and npm-assets.

Sclerotic answered 27/10, 2017 at 19:58 Comment(0)
I
9

It can be automated on Linux with post* script

"scripts": {
    "package": "ng-packagr -p ng-package.json",
    "postpackage": "cp -R YOUR_ASSETS_FOLDER dist/YOUR_ASSETS_FOLDER && tar -cvzf dist.tgz -C dist .",
    "release": "npm publish dist"
}
Imperial answered 15/4, 2018 at 5:1 Comment(0)
H
3

I also got same problem when I packaging using ngPackagr. So I wrote my own small node script to copy them to the dist folder manually.

npm install wrench

create new js file in the root assets-copy.js

var wrench = require("wrench"),
  util = require("util");

var source = "./src/assets";
var target = "./dist/src/assets";

wrench.copyDirSyncRecursive(source, target, {
  forceDelete: true
});

console.log("Asset files successfully copied!");

add build script to package.json like below,I called it as manual:assets-copy

  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "packagr": "ng-packagr -p ng-package.json",
    "assets-copy": "node assets-copy.js"
  }

After you run

npm run packagr

Also run our script

npm run manual:assets-copy

It'll copy them to the dist folder manually.

Hancock answered 3/10, 2018 at 3:57 Comment(0)
B
1

Try to use "postbuild". It works for me.

on package.json

...
"scripts": {
    ...
    "build": "ng build",
    "postbuild": "cp -R projects/mylib/src/assets dist/mylib/assets",
...

IMPORTANT... when to execute the build uses:

/> npm run build

Then the "postbuild" will execute automatically. It will not execute if you uses only "ng build".

Belabor answered 7/4, 2020 at 11:56 Comment(1)
'cp' is not recognized as an internal or external command, operable program or batch file.Orgasm

© 2022 - 2024 — McMap. All rights reserved.