angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Angular 6 CLI does not copy library's assets folder into dist folder.

Open 7bamboo opened this issue 8 years ago • 47 comments

Versions

Angular CLI: 6.0.7
Node: 9.3.0
OS: darwin x64
Angular: 6.0.3
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.6.7
@angular-devkit/build-angular      0.6.7
@angular-devkit/build-ng-packagr   0.6.7
@angular-devkit/build-optimizer    0.6.7
@angular-devkit/core               0.6.7
@angular-devkit/schematics         0.6.7
@angular/cdk                       6.2.0
@angular/cli                       6.0.7
@angular/material                  6.2.0
@ngtools/json-schema               1.1.0
@ngtools/webpack                   6.0.7
@schematics/angular                0.6.7
@schematics/update                 0.6.7
ng-packagr                         3.0.0
rxjs                               6.2.0
typescript                         2.7.2
webpack                            4.8.3

Repro steps

  • Create a library @acme/lib1 using ng generate library @acme/lib1
  • Create assets folder under projects/acme/lib1/assets and add images
  • Add a "projects" -> "@acme/lib1" -> "architect" -> "build" -> "options" -> "assets" key to the library's config within angular.json file.
  • Build library using ng build @acme/lib1
  • Fail error should show and none of the assets will be copied to dist folder.

Observed behavior

Upon building & packaging an Angular 6 library e.g. @acme/lib1 using ng build @acme/lib1, the Angular CLI does not copy the library's assets into the dist/acme/lib1/assets folder. This happens also when using the --prod flag.

The CLI seems to only support copying the root app's assets but not library specific assets. When trying to add "assets": ["src/assets"] to project @acme/lib1 within angular.json, the following error appears in the command line:

Schema validation failed with the following errors: Data path "" should NOT have additional properties(assets).

When creating the following custom rule to copying the files on ng build:

 "assets": [
    "src/favicon.ico",
    "src/assets",
     { "glob": "**/*", "input": "src/assets", "output": "../acme/lib1/assets/" }
],

I get the following error:

An asset cannot be written to a location outside of the output path.

While it is possible to work around this issue using other command line tools/scripts, it will be more consistent to add support for library's assets copy as well.

Desired behavior

Use Case Very often libraries include image files, icons, css files and other static files which are needed to be distrubted with the package.

What would like to see implemented? Add ability to define a library specific assets property within angular.json. Example -

"@acme/lib1": {
      "root": "projects/acme/lib1",
      "sourceRoot": "projects/acme/lib1/src",
      "projectType": "library",
      "prefix": "lib",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-ng-packagr:build",
          "options": {
            "tsConfig": "projects/acme/lib1/tsconfig.lib.json",
            "project": "projects/acme/lib1r/ng-package.json",
            "assets": [ // <--------- this is currently not supported
              "src/assets"
            ]
          },
  ...}

What did you expect to see? Project's specific assets should be copied from projects/acme/lib1/src/assets into dist/acme/lib1/assets.

Mention any other details that might be useful (optional)

7bamboo avatar May 30 '18 19:05 7bamboo

@bniedermeyer: the current workaround is to manually copy the assets

You can write a simple npm script to copy the assets after library is built using cpx:

    "build-lib": "ng build test-lib --prod && npm run copy-lib-assets",
    "copy-lib-assets": "cpx \"projects/test-lib/.npmrc\" \"dist/test-lib\""

But it would be really great if this is possible via angular.json

klofi avatar Aug 29 '18 12:08 klofi

This definitely should be available from angular.json. Library should be totally independent project and assets are quite important part of it.

Drag13 avatar Sep 02 '18 12:09 Drag13

I can't understand how, after almost 4 months, this issue is not at least in progress. Yes, there is a workaround, but it's like working against the angular.json configuration to do something outside of it when it should handle it.

JEDacreWright avatar Sep 21 '18 16:09 JEDacreWright

Thank you for your interest in this issue. This issue is currently on the project backlog. However, as there is a viable method to accomplishing the underlying use case and post-build staging will always be required for specific scenarios, other issues currently have a higher priority. If the issue is not being addressed at the priority you would prefer, please consider contributing. The project is open source and greatly appreciates and accepts contributions from the community. For further details please see the contributing guidance found here: https://github.com/angular/angular-cli/blob/master/CONTRIBUTING.md

clydin avatar Sep 21 '18 16:09 clydin

Since libraries are exported in the Angular Package Format, wouldn't that first have to be updated to contain guidance for asset output? I see a note about preferring "styles" and "template" over "styleUrls" and "templateUrls" but I don't see anything about assets.

dambrosiomike avatar Oct 12 '18 19:10 dambrosiomike

It seems there are only two workarounds for this problem if angular-cli doesn't provide the capability of copying the assets folder across.

  1. Forget about ng and write some hack in npm to emulate it and copy the assets folder across. With the npm postbuild hook or something.
  2. Or just do it manually.

Either of those slow down quite a lot the development process. If someone has any other better idea, please shout!

sergiojoker11 avatar Nov 22 '18 13:11 sergiojoker11

@ortichon this is not related to the issue which is about bundling assets into an angular library.

n00dl3 avatar Nov 22 '18 21:11 n00dl3

hai any updates i also end up with the same issue "assets": [ { "glob": "**/*", "input": "src/assets", "output": "/assets" } ], not working with

Angular CLI: 7.0.7 Node: 8.11.3 OS: win32 x64 Angular: 7.1.1 ... animations, common, compiler, core, forms, http ... platform-browser, platform-browser-dynamic, router

Package Version

@angular-devkit/architect 0.10.7 @angular-devkit/build-angular 0.10.7 @angular-devkit/build-optimizer 0.10.7 @angular-devkit/build-webpack 0.10.7 @angular-devkit/core 7.0.7 @angular-devkit/schematics 7.0.7 @angular/cli 7.0.7 @angular/compiler-cli 7.0.4 @angular/language-service 7.0.4 @ngtools/webpack 7.0.7 @schematics/angular 7.0.7 @schematics/update 0.10.7 rxjs 6.3.3 typescript 3.1.6 webpack 4.19.1

dhanarajknp avatar Dec 21 '18 07:12 dhanarajknp

Heya, this isn't the first time an issue has been opened on this topic. https://github.com/angular/angular-cli/issues/13285, https://github.com/angular/angular-cli/issues/10927 and https://github.com/angular/angular-cli/issues/10869 are essentially the same thing as this one.

The short answer is that libraries are built with ng-packagr, and ng-packagr does not have an assets functionality. The feature request in its issue tracker is https://github.com/ng-packagr/ng-packagr/issues/1092.

But it's a frequent issue that gets a lot of attention so I think it's worth it to go more into detail about this topic. We'll also leave it open and label it as a FAQ to increase visibility.

Building your browser app and building are library are similar insofar as they are both built. There are a bunch of source files that get transpiled and bundled and whatnot and put somewhere.

That's about how similar they are though. Building an browser app is actually a completely different build pipeline than building a library. And that means that the pipeline has different configuration options.

For building browser apps we use webpack and a customized set of configurations in the @angular-devkit/build-angular package, whose sources are in this repository. For building libraries we use ng-packagr, whose sources are in https://github.com/ng-packagr/ng-packagr.

Some options you don't really expect to be there when building a library, like the path to the index.html file. It just doesn't make a lot of sense. Other options, like assets, you do expect to be there. It does make sense for a library to have assets, and many libraries do.

But since ng-packagr doesn't have an assets like feature, you can't set that option in angular.json. You get the Schema validation failed error, which means that the option doesn't exist.

I agree this is a desirable feature and that many libraries, like @angular/material, have assets and need to build them using custom code. It is reasonable to expect the feature to exist.

But the right place to request it is in https://github.com/ng-packagr/ng-packagr/issues/1092 because that is where the code for building libraries is. If you are interested in contributing to this feature, that is where you should discuss possible approaches and collaborate with people working on this feature.

Although it sounds like an easy thing, it's not necessarily as easy as just copying some files over. Especially if we're talking about sass files, or other files that are meant to be consumed in specific ways. Often you'll find special instructions in libraries about how the assets must be used.

I hope this sheds some light on the topic.

filipesilva avatar Jan 03 '19 20:01 filipesilva

Hi there, earlier this week i was starting to migrate our company's library build to the Angular CLI way.

First i have to say, that it was a bit of frustrating me because i assumed that it would be easy going since the Angular tools are so great.

When I realized that there is no copy functionality for library assets, I did consider for not migrating to the cli tools. But Angular schematics and architect builders are so a great tools. That's why I decided to continue.

I have extended the @angular-devkit/build-ng-packagr:build and created an own one with asset copy functionality.

Here you will find how to use it: https://github.com/linnenschmidt/build-ng-packagr#how-to-use-it

npm install @linnenschmidt/build-ng-packagr --save-dev

Please give it a try. Maybe this approach is enough for you.

linnenschmidt avatar Jan 17 '19 08:01 linnenschmidt

@filipesilva I understand it would be job for ng-packagr but since Angular CLI runs ng-packagr and we have no option to change the command to something like ng-packagr && node copyOurStuff.js — could you at least add some sort of hooks to Angular CLI so this could be done with Angular CLI in a single step? After all, it looks like this would be the idea behind angular.json and ng that I would create a library and build it using ng run my-library:build. But now I also have to sorta leave ng realm and run some irrelevant npm run my-library:postbuild task that I have to configure in a separate place of package.json that would run the script and only the name of it would be a sign that it is a necessary step in the build process.

EDIT: Found there's an issue for that already: https://github.com/angular/angular-cli/issues/11787

waterplea avatar Apr 10 '19 12:04 waterplea

i have solved this by changing the file path to another location which folder doesn't contain special character

dhanarajknp avatar Apr 16 '19 12:04 dhanarajknp

@waterplea Angular version 8 brings the a stable Architect API which will allow for the extension and/or customization of builders like @angular-devkit/build-ng-packagr.

Splaktar avatar Apr 17 '19 00:04 Splaktar

Re the last comment by @Splaktar, this blog post was just published which explains the Architect API: https://blog.angular.io/introducing-cli-builders-d012d4489f1b

michaelbromley avatar Apr 24 '19 07:04 michaelbromley

After trying different approches. My solution was to create angular build as a subfolder of where my assets are. This way I can access assets as ../

Example:

(build)---> myApp/myAngularBuild (assets)--> myApp/

this way I can even build with ng build --base-href ./ --watch

it's not genius, but it's functional.

Some times ( ) cause these type of issue in build path

On Tue 25 Jun, 2019, 11:50 PM tecsolucionesinformaticas, < [email protected]> wrote:

After trying different approches. My solution was to create angular build as a subfolder of where my assets are. This way I can access assets as ../

Example:

(build)---> myApp/myAngularBuild (assets)--> myApp/

this way I can even build with ng build --base-href ./ --watch

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/11071?email_source=notifications&email_token=AJWMR64JGMFOFUMTJ5YCQHTP4JOXJA5CNFSM4FCPTQF2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYREMHQ#issuecomment-505562654, or mute the thread https://github.com/notifications/unsubscribe-auth/AJWMR6YX4NNXV73AS6FGRC3P4JOXJANCNFSM4FCPTQFQ .

dhanarajknp avatar Jun 26 '19 02:06 dhanarajknp

i have solved this by changing the file path to another location which folder doesn't contain special character

Oh, so thank you, dhanarajknp! I used [] in the root folder name.

Cossack13 avatar Aug 23 '19 05:08 Cossack13

Something ? Anything ??

gauri-kumar avatar Aug 23 '19 11:08 gauri-kumar

I am using nodemon in my project. I have been able to copy the images in dist folder of library. https://github.com/Sunbird-Ed/sunbird-ui-components/blob/master/nodemon.json. This works

However, Now I am facing issues in using these images in the lib components.

lakhanmandloi avatar Sep 13 '19 13:09 lakhanmandloi

Well in the meantime I developed my own builder, that executes @angular-devkit/build-ng-packagr and then follows with additional functionality like copying assets etc. That way I do not need to leave the realm of ng commands and even logs fit perfectly.

There is however one more thing...I had to configure application assets in angular.json to copy the library assets so they are accessible during the runtime of the application.

klemenoslaj avatar Sep 14 '19 10:09 klemenoslaj