How to exclude files from the Angular build using Angular-CLI. I've just added the path to exclude in the tsconfig.json
and tsconfig.app.json
files, but when I run ng serve
, Angular is still trying to compile the files.
Any ideas?
How to exclude files from the Angular build using Angular-CLI. I've just added the path to exclude in the tsconfig.json
and tsconfig.app.json
files, but when I run ng serve
, Angular is still trying to compile the files.
Any ideas?
Using exclude
property of tsconfig.json
(at root level), i.e.:
{
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
In your angular.json file,edit the build section as follows :
...
"assets": [
"src/assets",
"src/favicon.ico"
],
"styles": [
"src/styles.scss"
],
"scripts": []
},
"configurations": {
"production": {
"assets": [
{
"glob": "**/*",
"input": "src/assets/",
"ignore": [
"**/test.json",
"**/test"
],
"output": "/assets/"
},
{
"glob": "favicon.ico",
"input": "src/",
"output": "/"
}
],
...
This will still do the default imports required,but remove the specified files/directories(test.json & /test) when running ng build --prod,however they will still be available via ng serve,which is great for local development.
Remember to also exclude in your .gitignore
file if you have sensitive information in those files.
I could not make it work when editing tsconfig.json
,but the above worked like a charm!
Solution based on an example found here
I noticed if you put a .
in front of the path it will work:
{
"extends": "../tsconfig.json",
"compilerOptions": {
...
},
"exclude": [
...
"./yourPathToExclude/**/*"
]
}
I added the regex for the files to exclude from the build to src/tsconfig.app.json
.
Suppose I added the file ./src/app/components/my-component/my-component.stub.ts
to help in unit tests.
npm run build
and ng serve --prod=true
commands by default exclude only files ending with ...spec.ts
and include all other files.
In order to exclude the above file, add a regular expression to the src/tsconfig.app.json
as follows:
"exclude": [
"**/*.stub.ts"
]
Angular will exclude any file in the project that ends with .stub.ts
from the build commands.
Full src/tsconfig.app.json
example:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"module": "es2015",
"types": []
},
"exclude": [
"src/test.ts",
"**/*.spec.ts",
"**/*.stub.ts"
]
}
You can use ng eject
to get the webpack.config.js. You can modify this to your needs. However, you will not be able to use ng serve
or ng build
after that.
For using these commands again, you have to modify .angular-cli.json
. Remove the property 'projects.ejected' or set it to false. How to undo Angular 2 Cli ng-eject
Here's another solution that might work in some situations.
In angular.json (located in an Angular project's root directory), you will see this file replacement block in the build configurations for the production environment by default.
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
Just add your own file replacements there, and anything in the replaced file will not be compiled into the app.
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
},
{
"replace": "src/yourconfig.ts",
"with": "src/yourconfig.prod.ts"
}
],
Imports no longer used by the replaced file will also be ignored by the app, so, using the above example, if the ignored config file references a bunch of other environment specific files, they will also be ignored.
You can also do this for any number of environments: https://angular.io/guide/build
I don't found a way to remove a folder ONLY in production mode.
With tsconfig
either with angular.json
build config
The only way (not "clean") i found to do that is to remove the folder after the build.
For instance my package.json
:
...
"build": "ng build",
"build:dev": "npm run build -- -c dev",
"build:prod": "npm run build -- -c production && npm run removeMocks",
"removeMocks": "rimraf dist/myProject/mocks"
...
This Angular CLI thread is closed but not answered : https://github.com/angular/angular-cli/issues/6050#issuecomment-422288566