37

Is there a way to make angular-cli's ng buil --prod command to also copy a file/directory to the dist folder?

My project includes a "server" folder, and a "config.js" file at root, and I need them to be copied to "dist" such that when building, I don't need to remember to copy those directories

Amit
  • 5,924
  • 7
  • 46
  • 94

4 Answers4

62

For those wanting to copy files outside the src folder:

In example below I am copying all files from myfolder to the root dist folder.

In the angular-cli.json file (3rd line):

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

Angular-cli.json Documentation here

Stephane
  • 11,056
  • 9
  • 41
  • 51
34

On your angular-cli.json on assets object add the folders you want to include like:

Note : For Angular 9, this should be placed on angular.json file

"assets": [
    "assets",
    "favicon.ico",
    "META-INF",
    "WEB-INF"
  ]
saber tabatabaee yazdi
  • 4,404
  • 3
  • 42
  • 58
Lenin Lambis
  • 405
  • 4
  • 7
  • 2
    i am trying to copy one directory but it is not get copied the directory is outside the `src` folder so i gave the path as `../my_dir` and i run the command `ng build --prod` and only `assets` and `favicon.ico` were copied to the `dist` folder `my_dir` folder was not copied. – HirenParekh Apr 07 '17 at 06:56
  • I have the same problem as @HirenParekh. Any solution for that? – Ihor Nov 20 '17 at 15:29
  • 1
    For Angular 9, this should be placed on `angular.json` file – Sergio May 25 '20 at 10:46
  • The server build part in `angular.json` does not allow to copy assets because they assume that only the browser build (the SPA) should have assets. If you are building an Angular Universal App you will face this problem, still looking for a good solution. – Sparker73 May 27 '20 at 22:33
3

Source Link

For ng build use the "postbuild" command to copy folder/files

Update the package.json file

From

"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
....

},

To

"scripts": {
   "ng": "ng",
   "start": "ng serve",
   "build": "ng build && npm run postbuild",
   "postbuild":"xcopy /s \".\/dist\\angular-material-bottom-sheet-app\" \".\/mydistapp\\dist\" \/Y",
   "test": "ng test",
   "lint": "ng lint",
   "e2e": "ng e2e"
},

Check complete details here

Code Spy
  • 9,626
  • 4
  • 66
  • 46
  • good solution with the postbuild step. remember that xcopy is deprecated. use robocopy https://ss64.com/nt/robocopy.html – norca Jun 05 '23 at 11:44
0

When you put it in src/assets/.htaccess, it will be copied into dist/assets/.htaccess.

If you want it to be in dist/.htaccess, then you should be in src/.htaccess AND you need to add it to angular-cli.json in the assets array (like favicon.ico).

JEuvin
  • 866
  • 1
  • 12
  • 31