Giter Site home page Giter Site logo

Missing dependency about almond HOT 16 CLOSED

requirejs avatar requirejs commented on August 16, 2024
Missing dependency

from almond.

Comments (16)

jrburke avatar jrburke commented on August 16, 2024

Relative IDs are resolved relative to a reference ID's parent segment and not their paths. So, if 'foo' asks for 'bar', that is resolved to a module ID of 'bar'. If the above works fine before a build, but does not work after a build, it sounds like there is config info that is used before a build but not used as part of a build (maybe a paths config with 'bar': 'foo/bar'?).

If so, more info on how it is configured before a build (any requirejs.config calls, what the file system looks like) and the build config used would be helpful.

from almond.

apleshkov avatar apleshkov commented on August 16, 2024

Well, I generate my config via script:

({
    baseUrl: 'src',
    dir: 'out',
    optimize: 'none',
    namespace: 'mm',
    paths: { // core modules, one can use them in his service modules
        "app": "empty:",
        "client-server": "empty:",
        "jquery": "empty:",
        "ui": "empty:",
        "util": "empty:"
            ...
    },
    packages: [ // service modules as packages
        { name: 'home' },
        { name: 'notif' }
    ],
    modules: [
        { name: 'home' },
        { name: 'notif' }
    ]
})

Service modules are located in different places. Our script finds them and copy to a special folder:

data/ru/images/js/myjs/mm/tmp
└── src
    ├── home
    │   ├── history.js
    │   └── main.js
    └── notif
        ├── alerts.js
        └── main.js

I have a problem with notif module. Here's the output (built) code:

mm.define('notif/alerts',['app/comet'], function () { ... });

mm.define('notif',['jquery', './alerts'], function () { ... });

I get an error Error: notif missing alerts on defining notif module, thus it doesn't define at all.

But home module defines well:

mm.define('home/history',[],function () { ... });

mm.define('home',['./history'], function () { ... });

from almond.

apleshkov avatar apleshkov commented on August 16, 2024

BTW if I don't use relative paths, but absolute ones (for instance notif/alerts), everything works great.

from almond.

jrburke avatar jrburke commented on August 16, 2024

Ah, 'notif' is a package config. I think this is just an error in how packages are written out. I thought I fixed something for that in the requirejs 2.x series. If you use r.js 2.0.4 and almond 0.1.1 does that work better?

from almond.

apleshkov avatar apleshkov commented on August 16, 2024

Updated.

Now I get Cannot optimize network URL, skipping: empty:.js on every build. Seems everything is still build-able though.

The origin problem is still here.

from almond.

apleshkov avatar apleshkov commented on August 16, 2024

Seems I get this error only if I use dots in main files:

// Output code
mm.define('welcome/form',['jquery'], function ($) {
    ...
});

mm.define('welcome/school',['jquery', './form', 'client-server'], function ($, Form, ClientServer) {
//                                     ^ dot
    ...
});

mm.define('welcome/main',[],function (require) {

    var School = mm.require('./school'); // <- var School = require('./school');

    ...

});

But If I just change var School = require('./school'); to var School = require('welcome/school'); in my main.js - everything works fine.

from almond.

jrburke avatar jrburke commented on August 16, 2024

Ugh, lost this in the email notification flow. Looking at that code now, is the only problem with './school', or does it happen with './form' also?

If just school, then I could see it not working out since it is using mm.require which is probably a global require, so it does not have enough info to resolve the dot correctly, where if the require passed in to 'welcome/main' should have the right knowledge to do it. Although, this has an empty dependency array, mm.define('welcome/main', []

so that will not work out. It should look something like:

mm.define('welcome/main', ['require'], function (require) {
    var School = require('./school');
});

What is doing the renaming of define to mm.define? Is that the r.js namespace option? In that case it should probably not rename the internal require to be mm.require.

from almond.

apleshkov avatar apleshkov commented on August 16, 2024

mm is just a namespace (set via r.js build config).

I always use define/require in my code, but optimizer prefixes them with namespace automatically.

Thus if I write something like var School = require('./school'), it transforms to var School = mm.require('./school') by r.js.

If I write for instance define(function (require) { ..., it transforms to mm.define('welcome/main',[],function (require) { ....

Probably it's a real cause of my problems, but I can't control optimizer output code. Also I guess require and mm.require should point to the same function, shouldn't they?

from almond.

apleshkov avatar apleshkov commented on August 16, 2024

BTW If I write define(['./school'], function (School) { ..., it transforms to mm.define('welcome/main', ['./school'], function (School) { ... and it doesn't solve my problem.

Also this variant

define(['require'], function (require) {
    var School = require('./school');
});

could help, but I guess it's a kind of misconception, isn't it?

from almond.

jrburke avatar jrburke commented on August 16, 2024

So I'm thinking the optimizer should not rewrite the internal, local require calls inside a define factory, since it needs the local require to properly resolve relative IDs. I filed this bug to track that issue in r.js:

requirejs/r.js#245

Fortunately, you can avoid the namespace problem by just not using the namespace config and instead, do the build with almond, and then wrap the code via the wrap config in a closure. If you want to use the require and define from within that closure outside of the built file, you can create the global in the wrap.end part:

window.mm = {
  define: define,
  require: require
};

Also, doing this is will not work:

define(['require'], function (require) {
    var School = require('./school');
});

If the dependency array is used, it needs to specify all the dependencies used in the module, this would be correct:

define(['require', './school'], function (require) {
    var School = require('./school');
});

but then it is just easier in that case to just switch to dependency array-only approach:

define(['./school'], function (School) {
});

But if you mention that even this form does not work, so I'll need to set up a test to debug what is going on. If you have a git repo I could check out with the problem, that would be helpful.

I am unlikely to get to making a test though for a few days, as I am trying to push out a 2.0.5 requirejs release today to fix some issues that I think should be out there sooner.

from almond.

apleshkov avatar apleshkov commented on August 16, 2024

Thanks.

I'll try to avoid namespace.

from almond.

jrburke avatar jrburke commented on August 16, 2024

Did avoiding namespace fix this, or is there still the './school' problem?

If so, then I would like more info on the 'welcome' module ID space -- it does not appear to be configured as a "package", in the quoted config above. I want to try to construct a test case if there is still a problem.

from almond.

apleshkov avatar apleshkov commented on August 16, 2024

Sorry for so long response.

No, it didn't fix the ./school problem.

welcome is configured as a package, but it appeared after the config had posted here.

from almond.

jrburke avatar jrburke commented on August 16, 2024

Hmm, so I have a test for the optimizer here, that uses almond to test a build of a package with a package-relative dependency, and it works for me with requirejs/r.js 2.0.6 (or what will be 2.0.6 when released later today) and the latest almond release, 0.1.2:

https://github.com/jrburke/r.js/tree/master/build/tests/lib/packages

Where build.js is the build profile, and then almond.html is used to test the built file with almond. Maybe if you can modify that example to break or match the case you see failing, or provide a standalone case, that would be helpful.

from almond.

apleshkov avatar apleshkov commented on August 16, 2024

Well, I've created a little test and did some experiments:

  • old almond + old r.js = fail
  • new almond + new r.js = win
  • old almond + new r.js = win
  • new almond + old r.js = fail

old almond - 0.1.1
old r.js - 1.0.8

new almond - https://github.com/jrburke/almond/blob/c27e6e22f5ad5aed38a32b35d2e4674b8ed761c9/almond.js
new r.js - https://github.com/jrburke/r.js/blob/aeec5f0e78dded6c3f6714a42e563f753806bf38/dist/r.js

I can provide this test if you want.

from almond.

jrburke avatar jrburke commented on August 16, 2024

OK great, so at least we know what to recommend if someone comes across this again: be sure to use r.js 2.0.6+ and almond 0.1.3+. Thanks for working out the matrix combinations.

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.