Comments (6)
Is this for a top-level require() call (not a require call inside a define())? If so, then this should work:
var a = require('a');
However, in general, top level require([], function () {}) calls should stay async to keep similar execution flows as the source-loaded code via require.js.
from almond.
I believe I was making a "top-level require" call?
app.js:
(function () {
... almond.js here ...
... all my modules here ... define(........) x 1000
// oh, nos - this guy is async (which is okay, generally)
require(["wapo/app/main"]);
}());
The file is produced by running r.js with a profile more or less like this:
{
name: 'almond',
wrap: true,
include: [
'wapo/app/main'
],
insertRequire: ['wapo/app/main'], <------------- want this to be sync :(
paths: {
'almond': 'lib/almond/almond',
'wapo/app/config' : 'wapo/app/config/prod-config'
}
}
What I noticed stepping through the debugger was that I was going through that async path. Unfortunately, I could not find an option in the optimizer to force the synchronous version of the require call so I had to hack it to pieces.
Is there something I'm missing?
from almond.
The require([]) form is async. Try require('') instead at the top level. To do that with an almond build, use wrap, but as startFile and wrap.endFile. The startFile can just be:
(function () {
and then the end file can be
require('wapo/app/main');
}());
then remove the insertRequire from the build config since the wrap.endFile will do the require. Also be sure to use the latest almond.js.
from almond.
startFile / endFile are sweet! i converted the build to use the array versions for concat'ing external libraries for the "-with-dependencies.js" version of the build.
It would be cool if start / end could take a file name or the actual string - mixed into one (maybe a {content: 'foo'} if you are using mixed mode).
For example, I build several distributions of the project, with the difference in runtime behavior determined by the configuration used at compile time (so I can get handle the various deployments without nasty if statements all over the place):
var layers = [ {
layer: 'foo', <------ determines file name; a build {name}.min.js and {name}.full.js for each
config: { <----- requirejs optimizer config
},
... <----- other stuff i want to set for the layer
},
...
];
For some versions, I want to use: insertRequire: ['one', 'two', 'three']
For yet other versions, I use only: insertRequire: ['one']
If I use the wrap: { ... } config, I would need to put the require('one') in a separate file (because I am also the wrap to concatenate other libraries (I guess I could have a file pass to do that, but then I would need to optimize the entire file again).
startFile: [ 'some/lib1.js', 'some/lib2.js', 'var bar = lib2.noConflict(true);', '(function(){' ],
endFile: ['require(['one','two','three']); }());', 'maybe/another/file/here/why/i/know/not.js']
I guess I could just use require('one');require('two');require('three');... to get three sync versions.
Now that I think about it, it's probably not worth it. I can work around it.
from almond.
+1 for insertSyncRequire
as an an option on r.js.
Last night I faced the exact same problem as les2, on uBerscore (an experimental facade over underscore).
uBescore has 'lodash' as a dependency on AMD, but internally all modules use _.
I basically want to have a 'standalone' .js library (along AMD) that can be <script/> loaded after some global '_' is globalized, so I used almond.
Unfortunately the async require([])
breaks it unless I setTimout.
So I intuitively used a 'modified' almond where just line ~354 if (forceSync)
becomes if (true)
, which does the trick!
To day found this issue and tryed the trick with wrap.endFile
, it worked.
But why not have an insertSyncRequire
or something by default ? Unless somebody read this post, s/he is in trouble...
from almond.
@anodynos using the latest almond.js, if you do a wrap.endFile, you can have that file added to the end of the build, and that endFile can do the sync require supported by almond (not requirejs) at the top level:
window.globalName = require('moduleName');
Going to close this since this is possible now.
from almond.
Related Issues (20)
- normalize fails for relative imports with prefixes HOT 7
- Return almond.js as string when required from node.js HOT 2
- How to handle anonymous modules HOT 2
- unsafe String split HOT 3
- can't get requirejs, angularjs and almond to work together HOT 2
- Uncaught Error: No fs HOT 2
- Build problem HOT 2
- AlmondJS + CDN module with UMD HOT 4
- Any way to listen the DOMContentLoaded event HOT 1
- Your js script is crashing Firefox HOT 3
- Issue with relative path when they match such path: "../../vendor/some-lib/./some-file" HOT 1
- Issue with relative path when they match such path: "../../vendor/some-lib/./some-file" HOT 1
- Creating reusable libraries with RequireJS & AlmondJS HOT 1
- Almond + Electron HOT 5
- Loading CSS files HOT 3
- Can almond be forced to require synchronously?
- require.config(...) is being executed too late
- Calander Start With Monday rather than Sunday HOT 1
- Location of almond.js is critical to the build process?
- TypeError: Cannot read property 'splice' of undefined HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from almond.