I have this example code that uses RequireJS:
require(['jquery', 'SunCalc'], function ($, SunCalc) {
var position = SunCalc.getMoonPosition(new Date(), 51.5, -0.1);
});
I don't want to use RequireJS but I want to use the SunCalc library. I've installed it via npm and created a gulp task to minify the library file and include it but how could I do the above without RequireJS?
According to their github source, it loads as a global, if you are not using a dependency loader like requireJS.
So, SunCalc.getMoonPosition() should work after you've included the source. Much the same as jquery loading as a global.
Related
I'm using require.js successfully with many separate files:
require(['app/login/Login'], function (app) {
new app.Login();
});
This all works exactly as expected, with each module loading as required.
I've now run my code through the Optimizer and have one combined .js file "everything.js" - which is just what I want.
But how do I actually load this?
require(['everything'], function (app) {
new app.Login();
});
Returns me undefined for app.
Turns out the answer is in the optimizer docs:
If you want to include require.js with the main.js source, you can use
this kind of command:
node ../../r.js -o baseUrl=. paths.requireLib=../../require name=main include=requireLib out=main-built.js
Since "require" is a reserved dependency name, you
create a "requireLib" dependency and map it to the require.js file.
Once that optimization is done, you can change the script tag to
reference "main-built.js" instead of "require.js", and your optimized
project will only need to make one script request.
i.e. you can change the script tag to reference "main-built.js" instead of "require.js"
I am trying to use gulp to load jquery, but only the modules I am actually using, but something allways break in the process. My gulp task ATM is:
gulp.task('jqRJ', function() {
rjs({
generateSourceMaps: true,
baseUrl: './static/src/js-modules/vendor/jquery',
name: 'core',
out: 'jquery-3.1.1.js',
})
.pipe(srcmap.init({loadMaps: true}))
.pipe(srcmap.write(path.srcmaps))
.pipe(gulp.dest('./static/src/js-modules/vendor/jquery'));
});
I am using the folder/file structure for the jquery as it came from npm install jquery --save-dev. requirejs load the main jquery file correctly (the one that define the dependencies), but it does not load the other dependencies (like the core or the css modules).
Any Idea how could I accomplish that?
Thanks!
I m trying to use a js library easy-fit (https://github.com/pierremtb/easy-fit) in an angularjs application (https://github.com/khertan/forrunners).
Easy-fit use gulp, babel. And i would like to transpil and compile to a unique javascript file.
How can i use gulp-babel to transpile without require call ?
Thank you,
After reading documentation
There is no requirejs dependancy at all.
But as there is some require() call in the lib which is a node method, browserify the lib is required...
How i ve made and used the lib
$ browserify easy-fit.js --standalone easy-fit > easy-fit.bundle.js
Include it in your index.html :
<script src="lib/easy-fit.bundle.js"></script>
And now call it in your javascript :
var EasyFit = window.easyFit.default;
So i've come across an interesting use case where i'm using Browserify to bundle all of my assets together in a project, but a large external (external to the project) module needs to be loaded in when a certain in-app window is accessed. (It's a video player module made up of three scripts that get pulled in asynchronously when required).
At the moment i'm getting all kinds of errors from uncalled object errors if the requireJS module is loaded in before the Browserified app.js file, to cannot find module errors if loaded in after the Browserified code.
Is there anyway i can get Browserify and RequireJS to play nicely on the same page? I'm losing my mind!
TL;DR - use window.require in your browserified script.
Maybe it would still help someone.
I happen to use an 'external' dojo-based library totally based on requireJS-style AMD, which is absolutely un-"browserifyeble" and un-convertable to CommonJS or anything sane. My own code is totally Browserified. It's working OK like this:
Load the AMD loader (which defines the global require function) before the browserified script:
<script src="dojo/dojo.js"></script> <!-- RequireJS/AMD loader, defining a global 'require'-->
<script src="app/main.js"></script> <!-- The Browserify bundle -->
In your own js, call the require function on window object ('cause you'll have a local browserify-require shadowing the global one)
window.require(['dojo/dojo'], function (dojo) { ... });
The 'external' app or library, which calls require on its own to load submodules etc., works just fine 'cause that code is out of browserify context and the global require is not shadowed there.
Maybe if you have some pure nice standard RequireJS modules, you could somehow convert them to be Browserifiable, but in my case that wasn't an option.
There is a tool called browserify-derequire that resolves this issue by renaming browserify's require statmenets to avoid the naming collision.
It can be installed with npm using:
npm install -g browserify-derequire
Use it as a browserify plugin by changin your build command to:
browserify src/*.js -p browserify-derequire > module.js
There is more discussion on this issue at: https://github.com/substack/node-browserify/issues/790
For a gulp friendly solution (similar to what #Cride5 proposed) you can use gulp-derequire plugin.
Basic example from docs:
var derequire = require('gulp-derequire');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
gulp.task('build', function() {
var bundleStream = browserify({entries: './index.js', standalone: 'yourModule'}).bundle();
return bundleStream
.pipe(source('yourModule.js'))
.pipe(derequire())
.pipe(gulp.dest('./build'));
});
Plugin is also based on derequire module so all options are supported.
I'm implementing an AMD module oriented js framework that will be used on third party sites.
With this line, framework users will configure necessary modules
<script data-main="/main.js" src="/require.js"></script>
The problem is that data-main reference is loaded asynchronously so any js logic depending on modules loaded by main.js would fail unless I can be sure that it finished loading.
I'm pretty new to requirejs so not sure what's the good practices to create a framework that will be used by other people.
How could resolve this very simple problem?
EDIT
An example to explain my point
main.js
requirejs.config({
paths: {
jquery: '//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min',
}
});
main js reference + extra code
<script data-main="/main.js" src="/require.js"></script>
<script>
require(['jquery'], function($) {
// since main.js is not loaded yet, it will assume that there is a jquery.js
// file on the same folder that this file
console.log('myModule was also loaded and can use jQuery');
});
</script>
If you want to depend on other libraries and are specifically targeting being in a Require pipeline, all you need to do is to declare some dependencies with
define(
'myModule', // module name
['foo'], // module dependencies
function(foo){ // module construction
var myModule = {};
.... code to set up the module ....
return myModule;
});
and Require will take care of things. This will register your module with Require and won't attempt to build your module until all of your dependencies are available. This functionality is discussed here.
Update with example
Require JS is designed to work both with and without a prebuilt configuration. The paths property of the Require config object only provides Require with information on how to attempt to find libraries which have not yet been registered. However, the registration and then dependency resolution is handled by Require regardless of how/where the module was registered. Please see this JSFiddle for a working example of how you can register and use dependencies.
Update 2 regarding config
Since RequireJS loads everything asynchronously, you are correct, your code example will not work. However, you're making an incorrect assumption about how it is "supposed" to work. You have an incorrect example of what your library's clients' Require configuration will look like. If someone else is building an application using RequireJS and they want to use your library, they should declare the path to your library in their require.config:
require.config({
paths: {
// this tells require how to load jQuery (a library maintained neither by you nor your clients).
'jquery': '//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min',
// this tells require how to load myModule (the library you are building for your clients).
'myModule': '//example.com/js/my-module.min',
// this tells require how to load foo (a library built and hosted by your clients).
'foo': 'scripts/foo'
}
});
On the other hand, if your clients can't update their Require config to include your library in the declarations, then you're out of luck. All you can do is take all of your dependencies and bundle them up in your distribution file and then declare no dependencies:
define(
'myModule',
[], // can't declare any dependencies
function() {
// these dependencies are inside of the definition function to keep the global namespace clean
// since we need jQuery, we have to inline it:
var jQuery = ....
// same goes for d3.js
var d3 = ....
// now we can set up the module itself
var myModule = {};
.... code to set up the module ....
return myModule;
}
);
Obviously, this option means that you can't use the libraries which are being used by your clients. This means your library will be a lot heavier and include effectively duplicate code and data.
Hope that helps you understand how Require works and how other people will use your library.
I've finally used this approach
<script src="http://mydomain/js/require.js"></script>
<script>
requirejs.config({
baseUrl: 'http://mydomain/js'
});
require(['main'],function(){
// here users can do anything they want as all required libraries are loaded
});
</script>
main.js is loaded with a require instruction instead of using data-main attribute from script tag , this provides a callback where users can put their code.