This approach can cause problems for the build tool.
Update:
After further research, I found that the configuration settings in your main JS file are not considered optimizer by default. So, a cleaner solution would be to use a different configuration for the client and server.
Original:
A safer approach would be to define a module that adapts to the environment, while preserving all conditional codes in the definitions of your module and leaving all the dependency lists in the most reliable format.
// dependent module define(["libraryAB"], function (library) { //do some stuff }); // libraryAB.js dependency module define([], function () { return window ? defineLibraryA() : defineLibraryB(); });
Alternatively, you can leave the libraryA and libraryB separate by specifying libraryAB this way.
// libraryAB.js dependency module define(["libraryA", "libraryB"], function (libraryA, libraryB) { return window ? libraryA : libraryB; }); //define libraryA.js and libraryB.js as usual
If you want to avoid running libraryA on the server or libraryB on the client, you can force these modules to return functions and, if necessary, remember the result.
The moral is that itβs safer to store all your custom code inside module definitions, while keeping dependency lists well and predictably.
source share