Answer the question
In order to leave comments, you need to log in
How to correctly build js using gulp, taking into account environment variables?
There is a frontend on Angular1. There is a service with a method in which the URL of the API server is hardcoded. How to build JS using gulp so that if the variable ENV == 'develop', then the server address changes to another one.
Of course, I can stupidly replace the regex, but I want to do it right.
For example, to compile pug, I use gulp-data to pass json with variables to the compiler. How to do it with JS?
Answer the question
In order to leave comments, you need to log in
Maybe the stars didn't line up that way, but my envify didn't take off. Solved the problem like this:
// /gulpfile.babel.js
fs.writeFileSync('./.env.json', JSON.stringify(process.env, ["NODE_ENV"]));
// /src/js/modules/App.API/factoryes/API.factory.js
(function () {
'use strict';
window.process = {env: require('../../.env.json')};
if (process.env.NODE_ENV === "development") {
self.url = "https://api.domain.dev";
}
else {
self.url = "https://api.domain.ru";
}
})();
Watch what you collect. For webpack EnvironmentPlugin , for browserify -- envify , or even easier to get out with aliases.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question