No publicPath option in configuration file
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
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...
Also, with this integration we can use the standard way of Lazy Loading modules with the router.
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.
You can use the --base-href flag for this: https://github.com/angular/angular-cli#base-tag-handling-in-indexhtml
@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.
@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.
Is there any update on this?
any update on this?
No one has tried to do a PR yet, so no, no update yet.
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.
@filipesilva PR created for publicPath support including unit test #3285
How soon to have the PR merged? Need publicPath
I need it as well. We can't store our javascript assets in the root of our server.
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.
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.
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.
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 ?
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.