angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

No publicPath option in configuration file

Open manuelcua opened this issue 10 years ago • 36 comments

I´ve searched in the source but there´s no way of setting the "publicPath" of my application

It should be inside the webpack output configurations but looking at angualr-cli source it´s not available.

For me it is crucial because I prepend /static/dist/ to load javascript and assets on my app.

I´m using NG version 1.0.0-beta.15

I´m using Django as Server and only doing watch on ng build and Django puts it´s static files in /static/ folder and maps the base URL to a template index.html file so the hrefs in the index files are not relative to the static folder and gets a 404 error.

All of this gets solved if I could add the publicPath configuration in angular-cli.json or environment files

manuelcua avatar Sep 21 '16 16:09 manuelcua

This would be great! My build isn't located on the server's base folder, but in the public folder. So after every build, i have to change the strings in my index.html manually again...

markus-heer avatar Sep 25 '16 13:09 markus-heer

Also, with this integration we can use the standard way of Lazy Loading modules with the router.

manuelcua avatar Sep 28 '16 13:09 manuelcua

Definitely +1 for this config option; our test and production servers use a subdirectory so having publicPath support is necessary for us but we'd only want it to be used when using the --prod flag during build or serve.

theDustRoom avatar Sep 29 '16 07:09 theDustRoom

You can use the --base-href flag for this: https://github.com/angular/angular-cli#base-tag-handling-in-indexhtml

filipesilva avatar Oct 05 '16 15:10 filipesilva

@filipesilva : This does not solve de problem, the --base-href is only to provide root point for the Angular Route Path, not for the root of static files.

manuelcua avatar Oct 05 '16 15:10 manuelcua

@filipesilva I agree with @manuelcua as this is a separate config option for Webpack.

The publicPath specifies the public URL address of the output files when referenced in a browser. For loaders that embed it take the file from there. While IE would make "apollo/apollo/ang/main.bundle.js" The workaround is to make the index conditional.

The solution could be a templating where the index template is written by the developper and the %bundle% is replaced by angular-cli.

erhard avatar Oct 11 '16 13:10 erhard

Is there any update on this?

naveedahmed1 avatar Oct 15 '16 10:10 naveedahmed1

any update on this?

stuartwk avatar Oct 21 '16 04:10 stuartwk

No one has tried to do a PR yet, so no, no update yet.

filipesilva avatar Oct 24 '16 14:10 filipesilva

I'm not experiencing any issues, but I did find this issue looking for an easy setting to separate the bundled files from distribution root folder, a custom task will do for now, fingers crossed for future.

mozqt avatar Nov 05 '16 01:11 mozqt

@filipesilva PR created for publicPath support including unit test #3285

changLiuUNSW avatar Nov 25 '16 13:11 changLiuUNSW

How soon to have the PR merged? Need publicPath

mwaa avatar Nov 27 '16 17:11 mwaa

I need it as well. We can't store our javascript assets in the root of our server.

rsnj avatar Nov 27 '16 17:11 rsnj

Since changeLiuUNSW's PR is still pending, I found a temporary solution in the webpack docs for outputPath. It suggests that you can add __webpack_public_path__ in your app's entry file, which is main.ts for anyone using the cli. So then my entire main.ts looks something like this:

import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app';

declare let __webpack_public_path__: any;
__webpack_public_path__ = "https://cdn.somewhe.re/";

if (environment.production) {
    enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);

I realize this workaround should (hopefully) only be valid until a PR that enables publicPath is merge.

EDIT: Added variable declaration per below comment.

filoxo avatar Dec 02 '16 16:12 filoxo

Hi @filoxo , I tried to do the same as above, but still the script tags and link tags in dist/index.html are not prefixed with the __webpack_public_path__ . Also, noticed that it outputs a warning of somekind while we do ng build --prod. Hence, I decalred the __webpack_public_path__ variable like declare var __webpack_public_path__: any; . Would be good to have your feedback on this.

piyushchauhan2011 avatar Dec 04 '16 04:12 piyushchauhan2011

Not sure why you'd need the public path in index.html if you could serve that from the same origin as your assets (eg. CDN). My use case is that my backend is Java/Spring, but the js/CSS files are served via a CDN. Therefore the initial main.js loads but the additional chunks weren't. Setting the publicPath simply tells webpack's require which origin to request those additional chunks from.

And yes, I did notice the unresolved variable error during the build; probably should have made note of that in my comment too. I chose to ignore it because it still built successfully, but if declaring it resolved the problem with the TypeScript compiler that'd be good you'd solve the error. Again, though this works the real solution is to build the config into the cli. Hence why I noted that it's a temporary solution until #3285 is merged.

filoxo avatar Dec 04 '16 04:12 filoxo

Hmm, my use case is because we are trying to serve the dist/ folder using nginx but with a sub-url , the script, link and assets are not prefixed with that sub-url and fails to load.

I checked the the index.html but it doesn't prepend the sub-url even when we do __webpack_public_path__, don't know why.

@filoxo Is your script and link are prefixed with the cdn path correctly ?

piyushchauhan2011 avatar Dec 04 '16 05:12 piyushchauhan2011

I don't use the generated index.html because my backend is Java based. Setting the publicPath would only be used after the index file has been served because it's included within the bundle. That said, I don't think that what you're attempting to accomplish would be achieved by setting the publicPath but I'm no expert with webpack so perhaps I'm wrong.

filoxo avatar Dec 04 '16 05:12 filoxo