Angular-cli build with base-href also return programs folder when using git bash

后端 未结 4 1176
南方客
南方客 2020-12-31 07:32

I\'m trying to build my angular project with the angular-cli in a MINGW64 docker box on a Windows 7 Pro machine.

In git bash, the command I am using is:



        
4条回答
  •  误落风尘
    2020-12-31 08:19

    TLDR: Replace first / with /.


    The problem:

    I also encountered this issue when using git-bash on Windows. The git-bash sees the last argument of your command

    ng build --prod --base-href /directory/
    

    as a path relative to its binary folder, and prepends the argument with that (it converts the "seems-to-be" relative path into an absolute path).

    Official solution (which does not work but can be fixed):

    I tried the suggested method of using a double slash at the beginning:

    ng build --prod --base-href //directory/
    

    This will not be replaced by git-bash, however, the resulting base tag effectively makes the browser to not only replace the base path of URLs, but also the hostname, which is the first part of that string. So if you serve your app on host hostname, you want the URL to be

    http://hostname/directory/
    

    but it just became

    http://directory/
    

    You can fix this by also hard-coding the hostname into the base argument:

    ng build --prod --base-href //hostname/directory/
    

    The problem here is that your build now assumes a fixed hostname. As I did not like that solution, I experimented a little bit further and found a nice alternative.

    My solution (which currently works but can break in future versions):

    I found a clever "hack": Replace the first slash by an HTML entity. The CLI tool puts that string untouched into the href="...", and does not (double-)escape the ampersand (which it should, IMHO). A possible entity for the slash symbol is /, so your command looks like:

    ng build --prod --base-href /directory/
    

    This looks strange, but works:

    
    

    That is exactly "equivalent" (in the HTML/XML sense) to

    
    

    Note that when the Angular developers decide to "fix" this non-escaped argument in the CLI tool, this hack may no longer work, as that would result in

    
    

    which ends up making your URLs look like

    http://hostname//directory/
    

提交回复
热议问题