1

Suppose I've got a bunch of JavaScript files that build funcs something like:

me.add ([
panel1: [{
   url: 'https://localhost:8888/MyProj-0.0.1/resources/html/dir1/page1.html'
   }, {
   url: 'https://localhost:8888/MyProj-0.0.1/resources/html/dir2/page5.html'
   }]);

How can I use a property file to manage the front parts of all of these refs? ie, I'd like to change all 60+ of my JavaScript files that have functions like the above to be something like:

me.add ([
panel1: [{
   url: '($MagicGoesHere)/resources/html/dir1/page1.html'
   }, {
   url: '($MagicGoesHere)/resources/html/dir2/page5.html'
   }]);

so that I can read in a single property from a file in my project that defines the MagicGoesHere root for all of these references.

Note, I only want to do this as part of building my project; I do not need to read in a property and expose is on the webserver. (ie, when I deploy the WAR that gets built, all of the refs will have been expanded during the build)

My goal is to make it easy to update the refs when releasing MyProj-0.0.2

TIA,

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • What project infrastructure, which frameworks are you using? Doesn't sound like a typical js setup. – Bergi Jul 13 '17 at 04:00
  • It's actually a Java project goverened by maven, but I've got a pile of JS files that interact with ext.js for the UI. Yes, my above post is vague; was trying to simplify my question as much as possible :-) – KnowledgeSeeker Jul 13 '17 at 18:18

1 Answers1

0

You can easily just use a constants file.

my-constants.js

const myConstants = {
    baseUrl: 'http://localhost:8888/MyProj-0.0.1/'
};
module.exports = myConstants;

my-app.js

const myConstants = require('./my-constants');

me.add([
    {
        panel1: [
            { url : myConstants.baseUrl + 'resources/html/dir1/page1.html' }
        ]
    }
])

The above does not satisfy this constraint you placed upon the problem:

Note, I only want to do this as part of building my project; I do not need to read in a property and expose is on the webserver. (ie, when I deploy the WAR that gets built, all of the refs will have been expanded during the build)

However, I think it is worth asking if that is really necessary. There is no cost to the constants file. The amount of complexity you bring in with replacing source code during a build is non-trivial and it introduces new kinds of security concerns.

If you really want to do this as a build step, my favorite tool for the job is rollup-plugin-replace. It includes a few features you will probably want, like delimiters, without any cruft.

Seth Holladay
  • 8,951
  • 3
  • 34
  • 43
  • Thanks for your suggestion, Seth. What I was hoping to do was to pick-off the root string from the pom file; ie one stop shopping, so that when the project name or version changes, then the maven build process will automagically update the source files. Of course, I suppose I could write a simple shell script to run as a preprocessor to the build, but this seems likely to cause probs in the future. Thanks again for your reply, – KnowledgeSeeker Jul 13 '17 at 18:12
  • Yeah, I've done that exact thing in the past. Makes sense! Are you using a `package.json` file? Or only a pom file? If you have a `package.json`, then I would use [read-pkg-up](https://www.npmjs.com/package/read-pkg-up) and feed the `name` and `version` from it to `rollup-plugin-replace` as part of the build process. You would just make a simple `.js` script to do that for you. Or you could use something like [Gulp](http://gulpjs.com/). – Seth Holladay Jul 15 '17 at 08:06