teux / ng-cache-loader

Webpack loader to put HTML partials in the Angular's $templateCache.
83 stars 15 forks source link

How to keep the relative path? #11

Closed AlexVangelov closed 9 years ago

AlexVangelov commented 9 years ago

How to keep the relative path folders with different depth? e.g.: require("path/to/file.html") -> $templateCache.put("path/to/file.html"... require("path/to/subfolder/file.html") -> $templateCache.put("path/to/subfolder/file.html"...

I didn't found a way to do it for webpack config and available prefix options. { test: /.html$/, loader: 'ng-cache?module=app,prefix=???' }, Thank you.

teux commented 9 years ago

You should specify root directory.

/home/user/Projects/packman/
  ├─ path/to/file.html
  └─ path/to/subfolder/file.html

Prefix prefix=/home/user/Projects/packman:[dirs] will produce what you expect. Or shorter version with same result:

prefix=packman:**
AlexVangelov commented 9 years ago

Yes, but using my proprietary project path in the config is not acceptable.

I found a temporary solution: (templates are living in <project>/src/) { test: /\.html$/, loader: 'ng-cache?module=app,prefix=src:**' } and it do the trick for me, but it if somebody deploy the project into /home/user/src/<project> the template /home/user/src/<project>/src/path/to/template.html will not be parsed correctly.

AlexVangelov commented 9 years ago

If there is an option to apply wildcard on required path, not on resolved, it will be helpful. Actually after reading the documentation, I was expecting something like this to work: -url,prefix=** or -url,prefix=[dirs]

teux commented 9 years ago

It's not a problem because prefix takes the rightmost root /home/user/src/<project>/src/path/to/template.html with prefix prefix=src:** produce path/to/template.html

-url,prefix=**

In my opinion this is dangerous. One day you can move script to another directory that will changes path in requires and in Angular cache, and breaks directives. Therefore, the path is formed relative to the specified root, and not relative to the script.

AlexVangelov commented 9 years ago

Oh, if the prefix takes rightmost occurrence of root folder, it's OK. There will be no other src folder in the project. I agree that it's dangerous, but I have to Webpaket-ize existing project with Material Design folder structure. Thank you, can close the issue.

ssidorchik commented 8 years ago

@teux I really see the use case of having prefix which takes the relative path in require. When group resources by feature, templates are located in the same or nested folders alongside with scripts. If move a component to another folder or change parent folder name the templateUrl should reflect the changes. Having full path becomes annoying to maintain template urls when application has couple thousand files. So this issue can be avoided with a relative path in template id.