Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writeProfile in prebuild.js strips regex #36

Open
lancegosby opened this issue Aug 24, 2017 · 9 comments
Open

writeProfile in prebuild.js strips regex #36

lancegosby opened this issue Aug 24, 2017 · 9 comments

Comments

@lancegosby
Copy link

when writing the app.profile.js file, the prebuild.js script uses JSON.stringify(). Unfortunately this strips out any regex from the profile and replaces it with an empty object.

In my _app.profile.js file, I am trying to fix the build errors thrown by moment by excluding some files from the package using the trees property. For example:

packages: [
    { 
        //other packages
    },
    {
      name: "moment",
      location: "./moment",
      trees: [
        [".", ".", /(\/\.)|(~$)|(test|txt|src|min|templates)/]
      ]
    },
    {
        // more packages
    }
]
@Biboba
Copy link

Biboba commented Apr 13, 2018

Hi @lancegosby,

Did you find a workaround to this issue ?
I can't make a build work because of this.

Thanks

@lancegosby
Copy link
Author

I did this a while ago so not sure if I changed anything else.
In prebuild.js, I updated the writeProfile function as follows:

function writeProfile() {

  function replacer(key, value) {
    // wrap RegExp so we can find later
    if (value instanceof RegExp) {
      return ("__REGEXP " + value.toString() +  " REGEXP__");
    }
    else
      return value;
  }

  // use replacer function to wrap any RegEx so we can find it
  var tmpProfileStr = "profile = " + JSON.stringify(profile, replacer, 2) + ";";
  // convert back to RegEx before writing to file
  var profileStr = tmpProfileStr.replace(/"__REGEXP (.*) REGEXP__"/g, function(match, p1) {
    // unescape backslashes since this was stringified
    return p1.replace(/\\\\/g,"\\");
  });
  fs.writeFileSync(wProfileFile, profileStr, "utf-8");
}

@Biboba
Copy link

Biboba commented Apr 19, 2018

Many thanks @lancegosby !
It worked : from 193 errors, I am now at 24

build always "fails"

@gbochenek
Copy link
Contributor

gbochenek commented Apr 19, 2018

Good stuff @lancegosby ! Would it be less confusing to swap out RegExp.toJSON over this text replace?

@lancegosby
Copy link
Author

@gbochenek I'm not sure exactly what you mean. I remember trying a few different things at the time I was working through this. Not sure why I stuck with this, but it works for us. If you can improve on it, or make it less confusing, I would interested to see it.

@gbochenek
Copy link
Contributor

gbochenek commented Apr 20, 2018 via email

@lancegosby
Copy link
Author

You don't need to put "__REGEXP" in your _app.profile.js file at all. You can see a snippet of my file at the top of this issue.

I think the problem with RegExp.prototype.toJSON = RegExp.prototype.toString; is that the toString function escapes the backslashes and puts the whole regex into a string. The Dojo build trees property doesn't work with a string. It needs to be a RegExp object.

The replacer function wraps the RegExp string so we can undo those unwanted side effects.

Unfortunately, I don't have time to create a pull request at the moment.

@gbochenek
Copy link
Contributor

gbochenek commented Apr 24, 2018

Makes sense now. I need to read more carefully next time!

If anyone has time, I would be happy to accept a pull request for this solution.

@rsjones
Copy link
Collaborator

rsjones commented May 15, 2020

@lancegosby Do you know if this work around is still needed with newer version of WAB?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants