RequireJS and legacy application

I have a legacy application and I have reorganized parts of the application into separate backbone.marionette applications. I don’t have the time or budget to reorganize all this and I want my code to be easier to manage, which is why I was thinking about requirejs.

Most files are reduced and grouped together.

Can I use requirejs for this type of hybrid solution where I can work with individual backbone modules and still access existing javascript?

+6
source share
1 answer

As someone who has just recently started using Require.js in an old database using a base database, I feel your pain :-) I used a combination of approaches that I have listed here.

Say you have fileA.js and fileB.js and you want to convert fileB.js to use Require without changing fileA.js:

  • Violation of global space

    The requirement does not force you to import each variable through it; even in the Require-ified file, you can still access global variables in the same way as with code that does not require requirements. This means that if fileA creates all of its variables in the global / window namespace (which is very likely if you have not used the Requirement before), fileB can access them, regardless of whether it uses FileA Require.

    This turned out to be my solution for most of my old files; I just left them as they were, and put all the new required things under them. Thus, every global they create is ready and waiting for the required files to need them.

    Now it's great if fileB depends on file A, but what if it's the other way around? Well, the requirement also does not stop you from creating new global variables, which means that fileB can transfer anything with fileA if it wants to put it in global space.

  • Duplicate code

    Do not worry; I know how important β€œDRY” encoding methods are. However, in just a few files, what I did was duplicate Requ-ified. This turned out to be necessary because I use the Handlebars plugin to Require the compilation of the template, so if I wanted any file to use Handlebars, I needed to require it.

    To deal with the usual non-DRY issues, I added comments to the old files, effectively saying: "Do not add anything to this file, the required version is the" real "version." My plan is to slowly convert more of the site into Requirement over time, until I can permanently exclude the original, obsolete file. We have a small store, so it works for us, but in a larger company it may not fly.

  • Refactoring

    I know you said you want to avoid this, but sometimes a little refactoring can give you a lot of noise for your dollar. I personally almost did not reorganize anything, but there were only a couple of places that were small changes that greatly simplified the situation.

    In general, I see refactoring as something that you do after you switch to Requirement (over time, bring your code that does not require requirements to the fold).

  • Washers

    Chchrist is right in saying that gaskets are a good way to solve halfway demand problems. However, I personally did not use them, so I can’t say a lot about them, except to β€œlook” in them, they are likely to be useful. "

+8
source

Source: https://habr.com/ru/post/921209/


All Articles