Convert a JSON file to a class or constant during build process


#1

Hi,

I’m converting a Ionic 1 app to Ionic 3. In that app we had a build process similar to the Ionic 3 one, and we used gulp for that. One of the things this process did was read a config.json file and convert it to a constant. So we converted something like:

{
    "cache_expiration_time": 300000,
    "default_lang": "en",
    ...
}

to this:

angular.module('mm.core').constant('mmCoreConfigConstants', {
    "cache_expiration_time" : 300000,
    "default_lang" : "en",
    ...
});

The reason is that we wanted to be able to access these constants synchronously from anywhere, without having to create a service to read the file and parse it.

I’d like to do the same in Ionic 3, maybe creating a class that I can import where I want to use it. I’ve been reading a lot of documentation about the Ionic build process, and I don’t see a clear way to do it. I think the package.json config attribute doesn’t work for me, since my task doesn’t belong to any of the available options.

I thought that maybe I could create my own task (gulp or npm) to create this “constants” class in my src folder before the ionic build process starts, then run the build and, when it’s done, delete this new class. However, I don’t know how can I add new tasks to the Ionic build process without having to create my own “npm script” (I’d like to keep using ionic serve instead of “npm run whatever”).

Any ideas?

Thanks,
Dani