42

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

0

4 Answers 4

67

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

Sign up to request clarification or add additional context in comments.

2 Comments

copy project.json file from root to dist { "glob": "project.json", "input": "./", "output": "/" }
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"
  ]

4 Comments

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.
I have the same problem as @HirenParekh. Any solution for that?
For Angular 9, this should be placed on angular.json file
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.
6

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

2 Comments

good solution with the postbuild step. remember that xcopy is deprecated. use robocopy ss64.com/nt/robocopy.html
its probably best to not use any native bash commands since it will break depending on the OS. I would recommend looking for a good npm package that copies files like this: npmjs.com/package/copyfiles
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.