'Angular 12 breaks absolute paths for templateUrl and styleUrls
I am using Angular 13, upgraded from Angular 11 but after upgrading to Angular 12, the absolute paths stopped working for templateUrl and styleUrls.
When I was with Angular 11, I had the AOT complier on and it was working.
I'm needing to use absolute paths because I want to avoid having long relative paths and a lot of my components are using shared .scss
and .html
files for showing delete/restore confirm dialogs etc.
Here's an example of what I currently have in the components for templateUrl and styleUrls which is not working in Angular 12+ but still working in Angular 11.
@Component({
selector: 'something',
templateUrl: '/src/app/directory/directory/example.component.html',
styleUrls: ['/src/app/directory/directory/example.component.scss']
})
If I change the above to use relative path like below then it works but it doesn't look nice to have so many ../../../
@Component({
selector: 'something',
templateUrl: '../../../../example.component.html',
styleUrls: ['../../../../example.component.scss']
})
This is the templateUrl error I'm getting:
Error: src/app/directory/directory/something.component.ts:9:18 - error NG2008: Could not find template file '/src/app/directory/directory/example.component.html'.
templateUrl: '/src/app/directory/directory/example.component.html',
This is the styleUrls error I'm getting:
Error: Module not found: Error: Can't resolve 'C:/Users/.../frontend/src/app/directory/directory/something/src/app/directory/directory/example.component.scss?ngResource' in 'C:\Users\...\frontend'
Error: The loader "C:/Users/.../frontend/src/app/directory/directory/something/src/app/directory/directory/example.component.scss" didn't return a string.
I've also tried adding paths like below to my tsconfig but it doesn't affect anything.
"paths": {
"@env": [
"environments/environment"
],
"@shared/*": [
"app/shared/*"
],
"@core/*": [
"app/core/*"
],
"~*": [
"src/*"
]
}
I've seen answers suggesting to use raw-loader but I'm not quite sure how it works and if that's even the right solution here. I think it might be a complier config problem but haven't figured anything out.
Here's what my angular.json looks like
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"frontend": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
},
"@schematics/angular:application": {
"strict": true
}
},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/frontend",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": true,
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"node_modules/animate.css/animate.min.css",
"src/styles.scss"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/popper.js/dist/umd/popper.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"
]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "1mb",
"maximumError": "10mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "1mb",
"maximumError": "10mb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "frontend:build"
},
"configurations": {
"production": {
"browserTarget": "frontend:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "frontend:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"node_modules/animate.css/animate.min.css",
"src/styles.scss"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/popper.js/dist/umd/popper.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"tsconfig.app.json",
"tsconfig.spec.json",
"e2e/tsconfig.json"
],
"exclude": [
"**/node_modules/**"
]
}
},
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "frontend:serve"
},
"configurations": {
"production": {
"devServerTarget": "frontend:serve:production"
}
}
}
}
}
},
"defaultProject": "frontend"
}
Here's my tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./src",
"outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"module": "es2020",
"lib": [
"es2018",
"dom"
]
},
"angularCompilerOptions": {
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}
And here's my tsconfig.app.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
},
"files": [
"src/main.ts",
"src/polyfills.ts"
],
"include": [
"src/**/*.d.ts"
]
}
Solution 1:[1]
I had this exact issue when moving from version 11 to 12. When I dropped the leading "/", it then was interpreted as an absolute path, relative to the workspace.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Barry |