Giter Site home page Giter Site logo

Comments (6)

jrburke avatar jrburke commented on July 17, 2024

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.

les2 avatar les2 commented on July 17, 2024

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.

jrburke avatar jrburke commented on July 17, 2024

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.

les2 avatar les2 commented on July 17, 2024

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.

anodynos avatar anodynos commented on July 17, 2024

+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.

jrburke avatar jrburke commented on July 17, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.