ng build --prod copies .json file to assets folder, but contents not available at run time
Please provide us with the following information:
OS?
windows 10, deployed to windows server 2008 r2
Versions.
angular-cli: 1.0.0-beta.24 node: 6.9.1 os: win32 x64 @angular/common: 2.4.4 @angular/compiler: 2.4.4 @angular/core: 2.4.4 @angular/forms: 2.4.4 @angular/http: 2.4.4 @angular/platform-browser: 2.4.4 @angular/platform-browser-dynamic: 2.4.4 @angular/router: 3.4.4 @angular/compiler-cli: 2.4.4
Repro steps.
Create an app ... ng new AssetSample add file globals.json to assets folder in globals.json, enter text: { "apiRoot":"#{TheRoot}", "versionString": "#{TheUnevaluatedVersionString}" } In app.component.ts, add the consructor:
constructor(
) {
console.log (globals.apiRoot);
}
Add file to 'apps/assets' array in angular-cli.json build the app... ng build --prod -bh /SampleApp point an iis application root to the dist folder created by ng build run the app through IIS observe the message in the console... '#{The Root}' change the globals file api Root to ... '#{NewRoot}' run the app through IIS
Observed: console shows '#{The Root}"
Expected console should show '#{NewRoot}'
The log given by the failure.
Normally this include a stack trace and some more information.
Mention any other details that might be useful.
why would i want to do such a thing?
The problem is when we have to deploy to a number of different servers, the base url has to be changed for each back end:
test.foo.com/theWebUI --> test.foo.com/theBackEndURL staging.foo.com/theWebUI --> staging.foo.com/theBackEndURL demo.foo.com/theWebUI --> deme.foo.com/theBackEndURL production.foo.com/theWebUI --> production.foo.com/theBackEndURL
I don't care for the pattern of different environment files, for testing purposes, would rather do a single build and deploy that same build to all targets...
This is similar to how asp.net configures deployments through it's web.config.
Octopus Deploy will do this; the #{foo} pattern signals Octopus to replace foo with the value appropriate to the target environment.... so, being able to edit the .json file post build is a pretty necessary to deployment of any decently sized app.
So the fact that the asset is copied to the asset folder but ignored (maybe it gets packed up in the gz file?) is a pretty serious issue for us.
Thanks! We'll be in touch soon.
There are 2 things being discussed here. JSON file and site URL.
I'll only talk about JSON file in this comment, since it's the title of the issue.
Here's what I think is happening:
- You are
importing the JSON file - Since
importing JSON only works in Node, and also Webpack is the one that takes care ofimports, the JSON file is being bundled with the app - The JSON file is also copied as part of
assets, but that's not used, because what's used is the bundled copy
I'd say that this is working by design as per now.
For using JSON files in runtime, use Angular's Http service, or any other way that makes AJAX calls. This should just work and update from the JSON file in runtime.
You are importing the JSON file
actually, i'm using 'var'... as in...
var globals = require('../assets/globals.json')
but from the rest of your message, i'm thinking this is a moot point.
For using JSON files in runtime, use Angular's Http service, or any other way that makes AJAX calls. This should just work and update from the JSON file in runtime.
would you please elaborate on this solution? you mean serve the .json file from the back end and grab it with the http service?
First off, the purpose of one of the variables is to define where the back end is; you wouldn't be able to use the http service, because you don't know where to go until you get the .json file.
Also, the second variable is a version string that i'd like to embed into the web ui; so the file really wants to be kept with the UI package, and not gotten from somewhere else.
Or am I mis understanding your solution?
I guess if it's working as designed, maybe that's a problem? or perhaps you can explain to me how to accomplish post-build, per deployment environment configuration?
thanks
There are 2 things being discussed here. JSON file and site URL.
I'll only talk about JSON file in this comment, since it's the title of the issue.
Oh! then; should i open another issue?
I mean doing something like:
@Injectable()
public class SettingsService {
constructor(private http: Http) {}
static settings = null;
getSettings() {
if(SettingsService.settings) {
return Observable.of(SettingsService.settings);
}
return this.http.get('./settings.json')
.map(response => response.json())
.do(settings => SettingsService.settings = settings);
}
}
To expand on @Meligy 's answer, use something similar to http://stackoverflow.com/a/36417240 to cache the settings. Otherwise you may end up downloading the json multiple times.
The stackoverflow answer above saved me quite some headache, so vote it up :-)
thanks guys; works like gangbusters. it hadn't occurred to me to use the service to back to the web ui site.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.