Comments (16)
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.
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.
BTW if I don't use relative paths, but absolute ones (for instance notif/alerts
), everything works great.
from almond.
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.
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.
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.
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.
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.
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.
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:
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.
Thanks.
I'll try to avoid namespace.
from almond.
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.
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.
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.
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.
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)
- 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.