angular / angular-cli

CLI tool for Angular
https://cli.angular.io
MIT License
26.68k stars 11.98k forks source link

How to exclude some files from ng build #6050

Closed vimalprakashts closed 7 years ago

vimalprakashts commented 7 years ago

Versions.

@angular/cli: 1.0.0-rc.0 node: 7.6.0 os: darwin x64 @angular/common: 2.4.8 @angular/compiler: 2.4.8 @angular/core: 2.4.8 @angular/forms: 2.4.8 @angular/http: 2.4.8 @angular/platform-browser: 2.4.8 @angular/platform-browser-dynamic: 2.4.8 @angular/router: 3.4.8 @angular/cli: 1.0.0-rc.0 @angular/compiler-cli: 2.4.8

How to exclude some files from ng build ? My case is I will replace some js file dynamically in docker volume. For that , I just want to ignore some files while building. Is that possible ?

ctrl-brk commented 7 years ago

Try to add them to the "exclude" section of your tsconfig

  "exclude": [
    "myexclude.ts"
  ]
filipesilva commented 7 years ago

@vimalprakashts can you elaborate a bit more please?

wnabil commented 7 years ago

i added them to tsconfig but that didn't work :\

filipesilva commented 7 years ago

Closing as original author never got back.

playground commented 6 years ago

@filipesilva for me I want to exclude 'core-js/es6/reflect' and 'core-js/es7/reflect' from ng build -prod but need them for ng test and ng build (without aot).

filipesilva commented 6 years ago

@playground you can try using the environment files for that. I should mention that in the future they won't be needed for dev/test even.

ralberts commented 6 years ago

@filipesilva @playground I am curious - how do you exclude files in the environment files... is there an example? I know I probably missed it on the documentation or elsewhere :-).

playground commented 6 years ago

@ralberts I simply have a polyfills-dev.ts and a polyfills-prod.ts, and in build script just copy the corresponding environment file into polyfills.ts

Viktor-Bredihin commented 6 years ago

any solution here? exclude doesn't work. I need to exclude folder from ng build

hpawe01 commented 6 years ago

@Viktor-Bredihin: As described by @ctrl-brk, when I add the relative path of my folder in the exclude section of ./src/tsconfig.app.json, the CLI build command excludes this folder.

Does this work for your?

stweedie commented 6 years ago

While adding it there does exclude the files from the build, ng serve will still watch changes (even to those excluded files) and recompile.

Any way to avoid this?

Viktor-Bredihin commented 6 years ago

@hpawe01 In my project I have 2 areas (admin, user). It's using the same shared module, so actually I have 2 apps in the same project. Then I want to build 2 different apps, so admin area should be on one server and user area on another. But I can't do that, cause while ng build - builds only needed modules (where u have routes), ng build --prod - builds everything in the project

Viktor-Bredihin commented 6 years ago

any workaround here ?

Viktor-Bredihin commented 6 years ago

maybe it's possible to exclude files something for specific environment at least?

ManojlovicMilos commented 6 years ago

This should not be closed. I have same problem, and here I find no solution and no workaround. I really think that "Closing as original author never got back." is not a reason to close an issue, as it is evident that other people experience same problems. Also issue is easily understandable and I don't see any reason for elaboration. When people need support for using Angular, and encounter this problem, they will come to this issue. Here they will see no problem solved, no workaround and many question with no answer. And it stays like that for months. You can at least give some feedback. This is not only example of prematurely closed issues for Angular I encountered, and it really bugs me. I really hope that team takes this in consideration, as it might happen that many people "never come back".

Viktor-Bredihin commented 6 years ago

@ManojlovicMilos I have found it on my own. Here is the best solution - https://yakovfain.com/2017/04/06/angular-cli-multiple-apps-in-the-same-project/

Wim-van-der-Veen commented 6 years ago

I got it working, after some experimenting (since I as well couldn't find documentation). Assuming you've got a structure src/app/... , and the file src/tsconfig.json, then in tsconfig.json the syntax that works for me is: { "compilerOptions": { "baseUrl": ".", "mapRoot": "./", etc bla bla .... }, "exclude": [ "./app/system/services/data.original-service.ts", "./app/backend/system/services/http-queue.service.ts", "./app/backend/system/services/auth.service.ts", "./app/backend/system/services/auth-local.service.ts" ] }

TimWarp commented 6 years ago

Wim-van-der-Veen 's solution worked for me... my tsconfig.app.json =

{ "extends": "../tsconfig.json", "compilerOptions": { "outDir": "../out-tsc/app", "module": "es2015", "baseUrl": ".", "types": [] }, "exclude": [ "test.ts", "*/.spec.ts", "registration", "./app/registration/**" ] }

...adding the dot in baseUrl did the job ... phew. the above excludes the whole 'registration' folder

gabrielalan commented 6 years ago

Didn't work for me :( I'm trying to exclude some mock files that I use only on *.spec.ts, but AOT build keeps saying they are not included on any NgModule

FirstVertex commented 6 years ago

When I tried using exclude for my .spec.ts files, my Jasmine tests stopped working because the tests weren't part of the compilation! Need to include the .spec.ts files in JIT build and exclude them for AOT build! How to exclude in AOT build only?

Ldnz commented 6 years ago

tsconfig-schema.json say:

Specifies a list of files to be excluded from compilation. The 'exclude' property only affects the files included via the 'include' property and not the 'files' property. Glob patterns require TypeScript version 2.0 or later.

working workaround for me:

tsconfig.json

  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ],
    "baseUrl": "",
    "paths": {
      "app/*": ["./src/app/*"]
    }
  },
  "include": [
    "src/*"
  ],
  "exclude": [
    "src/app/directory_1/*",
    "src/app/directory_2/*"
  ]
}
rajeshwar-cloud commented 6 years ago

This won’t work. In our case we want to put the TS file on the intermediate server and use the angular to build those files

Ldnz commented 6 years ago

@raorajesh what is "intermediate server" in your case? could you explain with more details? i also use the angular to build those files and have three tsconfig files (common, browser and for universal server). only server config contains include/exclude section.

mistrykaran91 commented 6 years ago

I'm not able to replicate this in my application, Currently, our application has a structure like src->app->modules->user-management, I want to exclude user-management folder in prod build, how can I do this.

Below is the tsConfig. { "extends": "../tsconfig.json", "compilerOptions": { "outDir": "../out-tsc/app", "module": "es2015", "baseUrl": "", "types": [ "jquery" ] }, "exclude": [ "test.ts", "*/.spec.ts", "app/modules/user-managment/*/" ] }

rmsmq commented 5 years ago

Specifying the exact path of the file was not working for me, I had to use a recursive match on the root of the file location. e.g. this is what SHOULD have worked

"exclude": [
    "src/test.ts",
    "**/*.spec.ts",
    "src/other-main.ts",
    "src/tsconfig.other.ts",
    "src/app/other-app.module.ts"
]

This is what DID work;

"exclude": [
    "src/test.ts",
    "**/*.spec.ts",
    "**/other-main.ts",
    "**/tsconfig.other.ts",
    "**/other-app.module.ts"
]
profanis commented 5 years ago

It worked for me by adding the directory of the module in the exlude array of tsconfig.app AND removing the module reference from app.module

ddehghan commented 5 years ago

For anyone that get to here: The key is that the path is relative to the location of the "tsconfig.app.json"

kriskanya commented 5 years ago

Just leaving this comment here in case it helps someone.

I had to create a a RouterLinkDirectiveStub to stub the [routerLink] directive and wanted to ignore the file from linting.

To do this, I created a relative path to my folder containing the stub, in tsconfig.app.json:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "types": []
  },
  "exclude": [
    "**/*.spec.ts",
    "./app/shared/testing/*.ts"
  ]
}
neorclynn commented 5 years ago

Is there any way that I can exclude some files ONLY when "ng build --production"? I still need those files when "ng serve".

tiingo commented 5 years ago

Leaving this here for others who may experience "exclude" not working. You must also exclude files that import anything that is being excluded for it work.

For example if you exclude "exclude-me.component.ts" but it in a "routing.module.ts" file, then "exclude-me.component.ts" will not be excluded. You must also exclude the "routing.module.ts" file in addition to "exclude-me.component.ts"

cdarken commented 5 years ago

@neorclynn if you didn't find a solution until now: you can specify another tsconfig file when running build.

angular-automatic-lock-bot[bot] commented 4 years ago

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.