2

Back-end developer is asking about front-end.

I've got two functions which are totally different. One of them is for a production, another - for a local development. For example, when I'm in a development mode, the situation is as follows:

/*
function f() {
    // production logic
}
*/
function f() { 
    // development logic
}

As you may see, I always comment one of the functions. Before making a build for a production I should remove comments for the production function, and add them to the development one.

I really don't want to make the if statement where I'll check out some variable (if the value equals "DEV" then ... else ...). The functions are too large and contain many non-contiguous parts.

I googled a lot, but nothing has found. Is something like? Something like profiles with their variables that can be integrated into JS files? Or marking a function with a comment which excludes/includes a function from/in a build as that's possible with HTML.

The project is being gathered by Grunt.

Community
  • 1
  • 1
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • JS isn't "built" so there aren't compile time flags. If you're using grunt it probably wouldn't be hard to make a grunt script that does the equivalent though. By the way, normally the only difference between dev and prod will be console logs. I'm curious why you have entire functions that are different. – dlsso Sep 01 '16 at 19:02
  • @dlsso, This module is only a small part of the large project, the production version of the function operates data which appears in the run-time, for the development version, I have to generate this data artificially to see a full picture. – Andrew Tobilko Sep 01 '16 at 19:23
  • @dlsso, I'd be grateful if you suggest the way I should follow to achieve this purpose. Can Grunt modify JS files during their merging / uglifying? – Andrew Tobilko Sep 01 '16 at 19:29
  • Got it. So the ideal way to handle it is to set up something that more closely resembles your prod environment, which will make the rest of your life easier as well. Failing that, I think what you want is a grunt plugin that comments code between two tags/keywords. Then the minifier will clean them out when it runs. The problem is I don't know if such a plugin exists, in which case you would have to make it yourself. – dlsso Sep 01 '16 at 19:50
  • To clarify, the answer to your second comment is yes grunt can modify JS, but you'll need to find a plugin that does what you want. https://www.npmjs.com/package/grunt-dev-prod-switch might work, for example. – dlsso Sep 01 '16 at 19:55
  • @dlsso, thank you, I'll try and show my attempts. – Andrew Tobilko Sep 01 '16 at 20:16

1 Answers1

1

As @dlsso mentioned in the comments, there is an effective npm plugin grunt-dev-prod-switch to modify source files depending on the mode you are in.

Use to switch between previously defined block comment blocks in project files to change the environment from development to production and back.

After the configuration, it allows me to do the following:

/* env:prod */
function f() { ... }
/* env:prod:end */

/* env:dev */
function f() { ... }
/* env:dev:end */

The plugin determines which mode/environment is turn on (dev_prod_switch.options.environment), analyses comments of the files (are defined in the dev_prod_switch.all.files property) and spoils (comment) all blocks don't relate to the current mode.

For example, the production mode is turned on:

/* env:dev *#/
function f() { ... }
/* env:dev:end */
Community
  • 1
  • 1
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142