Require other requirejs apps - requirejs

Is it possible to require another requirejs app (with its own config) as a requirejs package? Say I have my main app and I want to require my standalone calendar widget. Is that possible?
Thanks in advance
edit
One way could be to require the optimized version of the widget?

RequireJS and CurlJS (the more commonly-used AMD loaders) allow you to re-configure local scope. See multiversion support docs
However, here is just a thought:
Rely as little as possible on config. It is very possible to set up only true generic, global things like jquery, underscore paths in global config and just roll named defines inside your widget for local packaging needs. For every config option James invented, there is a valid way to do it without a config.
Shim can be easily emulated locally with nested require calls. Putting all plain JS aliases inside paths is perhaps useless because you can just as easily use direct refs to the plain js files "path/to/file*.JS*"
In reality, you don't really need config that much. Get away from needing it and your problem is simplified.

Related

File specific TypeScript annotation for environment, like browser or node

I'm trying to write a universal React.js application using TypeScript and if possible it would like to somehow annotate certain TypeScript files in such a way that a file is understood to be running inside a browser context or Node.js context explicitly. So that any attempt to use browser APIs from within Node.js environment would fail, and vice versa. How can I do that?
Right now the files reside in the same directory and maybe that the problem because I cannot have multiple tsconfig files but if that is the only solution I guess I have to do it that way.
Each project described by a tsconfig.json file has a single set of visible declarations; there's no way to have different declarations visible in different files in the same project. You can put a <reference> directive in a specific file, but the directive will affect the entire project. So to enforce what you want using the regular type checker, you'll need to use multiple tsconfig.json files. There may be other approaches such as using the tslint "ban" rule to ban all APIs from one environment in a specific file, but I doubt they will be practical.

Run-time bundling of ES6 modules in ASP.NET MVC

Are there any existing solutions for run-time bundling of ES6 modules?
I'm looking to simplify JavaScript code development in a MVC5 web app. We're having issues with large, unwieldy JS files, so I'm hoping to get a module loader system in place. So far, I'm not finding any existing bundle transformers for ES6 or another module loader format. I'd be fine with using TypeScript or nodejs require style. I prefer not to use require.js style, though.
Perhaps there's a good reason this solution doesn't exist already. Maybe the dependency resolution processing is too much for a run-time bundling solution. But, I figure it's worth a shot to ask.
Solutions Considered
Prebuilt Web Client
Ultimately, this is where I want to be, but I need a stop-gap solution for now. I know how to put together a build system for an HTML client using grunt/gulp/webpack. But I don't want to have to tell developers to run webpack -w or something similar during development. Nor do I want to tell them to rebuild a solution for every JS change. They should be able to modify the file, refresh the browser, and see the change.
Directory Structure
This is the route I'll probably end up going with. Basically, this JS codebase consists of jQuery widgets and plain JS (helpers/common functions). So, if I structure the code in this directory structure and include the js dir, it should get me most of the way there:
js (DIR)
app-start.js
helpers (DIR)
widgets (DIR)
Widgets should be fine. Helpers, I can see issues where one function/class depends on another. Though, since a function call should never start with a helper (only a widget), this should work fine, assuming no globals are used (or maybe one global like 'App').

Browserify require clashes with node require

As the title suggests, i use browserify for my internal requires and on my node server that interferes with the require of global modules.
My solution now is to build the server script with browserify on the side, and then append a file that holds all my requires of global variables that are later gonna be used by the script. This renders browserify unable to try to put my global npm modules into my server script.
Is there a prettier solution to this? Because this way feels like a heap of dung.
-- EDIT --
The code runs only on backend, however - the problem is partially due to how i compile the code that is to be run on that backend. Initially the require keyword is used to get global node modules, e.g express or http. In my case, i need both that functionality as well as reference my own modules compiled with browserify.
My solution right now is to overwrite the global 'define' parameter with amdefine after i've saved references to the the global node modules that i will later use.
Code to bundle node modules into global parameters, require looks for global node modules
My Main, everything from this point forward, require looks for modules inside my own code
I guess I could make a duplicate of nodes require and make a new global reference to it, i.e, require becomes requireNodeModule, i feel as if that's an even worse solution to the one i have at present though...
It sounds like you have some code that runs both on the front-end and back-end, but at present this question is too broad for me to give you useful advice.
Can you narrow it down to a single, specific module that you want to run both in the browser and on your server, where the require clash is evident?

using requirejs without main file nor config()

I'm building an app that will contain many js (jquery) modules (files) using the following setup;
The build is run using Grunt task runner.
I use handlebars templates, then generate the html from *.hbs files.
During the build, I uglify all the js files into one minified file.
I call the minified file in my application <script src="assets/js/app.min.js"></script>
Now, I want to use requirejs to organize my code and adhere to the AMD specifications..
But I got 3 problems with this approach:
I need to have 1 single minified file for all the js; this keeps my code harder to "decode" thus to copy (since it is mixed with other dependencies; jquery, modernizer..) and also helps avoid extra http requests if scripts are called individually.. However, requirejs needs a main file to initialize the app and inside that main file It calls the other required files.. I don't have such configuration.
Many of the dependencies I'm using are in a bower package (that I don't include in the distribution); this makes it not possible to call those files using the suggested requirejs method.
I'm using jquery on this with some 3rd party plugins that don't call define(); to make them work I have to use shim config which again rises the problem #2!
So how am I supposed to take advantage of requirejs while keeping my code in modules and well organized?
Sorry for the long question and thanks for reading :)
ps: Feel free to change the title of the question as I couldn't find a better one!
I finally opted for AngularJS as it adheres to my setup above and allows me to split my app into manageable small modules.
I have also the possibility to use the ease of jQuery (even though it is not a best practice among angular community) and much more.

How to share common client-side javascript across NodeJS projects?

I'm a Node n00b starting a couple web app projects using Express, and I've got some common client-side libraries I'd like to share between the two projects. This seems like a very common problem, so there must be several solutions available already.
I come from a java background, and in java, I'd create a separate "common" project and "overlay" common WAR over my project during packaging. This would also allow for r.js optimization during the build process.
My best guess in Node is that I need to create a private NPM module, and map those common files into express via a use() middleware plugin. Is that right?
How, then, can I package both my common and project specific javascript into a minified file using r.js?
Or is source control the answer? Checking out my "common" repository inside each project?
Any help would be most appreciated. Thanks.
This seems like a very common problem, so there must be several solutions available already.
Good news: Yes, this is a common problem. Yes, there are several "solutions".
Bad News: All of the "solutions" are at least partially terrible.
Here's my advice:
1) Individual .js files should be coded as CommonJS modules
2) Groups of related .js files should be made into npm packages
3A) Use them in node via the regular node.js/CommonJS require function and use browserify to use them in the browser
3B) OR use a built tool like grunt to wrap commonjs into AMD format for use with requireJS in the browser
3C) OR consider something like component.io components

Resources