`ng add igniteui-angular` should add `hammerjs` to main.ts.
Description
ng add igniteui-angular should add hammerjs to main.ts instead of adding it to scripts option in angular.json.
- igniteui-angular version: 7.0.2
- browser: N/A
Steps to reproduce
ng add igniteui-angular
Result
hammerjs is added to scripts option in angular.json.
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"test-app": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {
"@schematics/angular:component": {
"styleext": "scss"
}
},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/test-app",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": [
"./node_modules/hammerjs/hammer.min.js" <--- here
]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "test-app:build"
},
"configurations": {
"production": {
"browserTarget": "test-app:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "test-app:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"src/styles.scss"
],
"scripts": [
"./node_modules/hammerjs/hammer.min.js" <--- here
],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"test-app-e2e": {
"root": "e2e/",
"projectType": "application",
"prefix": "",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "test-app:serve"
},
"configurations": {
"production": {
"devServerTarget": "test-app:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "test-app"
}
Expected result
ng add igniteui-angular should add hammerjs to main.ts like Angular Material does.
https://material.angular.io/guide/getting-started
Currently, adding hammerjs to main.ts seems most common way.
At least, even igniteui-angular demo doesn't add it to angular.json.
https://github.com/IgniteUI/igniteui-angular/blob/master/angular.json#L32
Attachments
None.
@tkiryu Have you encountered any issues with apps configured this way?
Adding Hammer to the scripts config was part of our Getting Started guide and that's also how we configured it in our own CLI templates. Per the global scripts Ng CLI story the scripts config is intended for legacy/global libraries and HammerJS kind of matches that description. That side-effects only import has no interaction with the typescript code - that comes from the typings package (which we also install). Either way, even when imported Hammer still attaches itself to window.
As far as I can tell, the major difference is that the import bundles the lib in the main.js during build, rather than creating scripts.js, and that's assuming the project doesn't use other global scripts already (supposedly that's were analytic snippets go as well).
I don't mind moving it for consistency, though :)
@damyanpetev , I'm so sorry for getting back to you late.
Have you encountered any issues with apps configured this way?
Yes, I have.
For example, We add igniteui-angular to an existing app using @angular/material, then a problem comes.
Angular Material's getting started says as below.
After installing, import it on your app's entry point (e.g. src/main.ts). import 'hammerjs';
I guess everyone who is using @angular/material has imported hammerjs like the above.
Unfortunately, ng add igniteui-angular doesn't care whether hammerjs is already imported or not and adds hammerjs script to scripts option in angular.json.
This is the case with What Using global libraries inside your app says
Once you import a library via the scripts array, you should not import it via a import statement in your TypeScript code (e.g. import * as $ from 'jquery';). If you do that you'll end up with two different copies of the library: one imported as a global library, and one imported as a module.
@damyanpetev , I came up with an idea.
ng add @angular/material asks whether user wants to import hammerjs or not.
ng add @angular/material
...
? Set up HammerJS for gesture recognition? (Y/n)
I think ng add igniteui-angular should do same thing.
What do you think about it?
@tkiryu The guide mentions Hammer JS can be added via npm, CDN (which would be in the index.html or angular.json) or served directly(?). I suppose still the import is the most popular version, so avoiding the conflict would be nice.
Will definitely look into this after the holidays.
@damyanpetev , thank you for your comment. I agree with you!