scalableminds / amd-optimize

An AMD (RequireJS) optimizer that's stream-friendly. Made for gulp.
MIT License
162 stars 29 forks source link

An error occurred when require text file #48

Open sarike opened 9 years ago

sarike commented 9 years ago

Hi, Thanks for the cool plugin first. I met a problem when I use amd-optimize. My code is as follows:

define(function (require) {
    var userInfoTemplate = require('text!./templates/userinfo.tpl');
}

After building code is as follows:

define('admin/dashboard/templates/userinfo.tpl', [], function () {
    return 'Hello <%=name %>';
});
define('admin/dashboard/index', [
    'require',
    'exports',
    'module',
    'admin/dashboard/templates/userinfo.tpl'
], function (require) {
    var userInfoTpl = require('text!./templates/userinfo.tpl');
}

But the code after building can not be executed correctly.

require.js:166 Uncaught Error: Module name "text!admin/dashboard/templates/userinfo.tpl_unnormalized2" has not been loaded yet for context: _

If this line:

var userInfoTpl = require('text!./templates/userinfo.tpl');

can be converted to:

var userInfoTpl = require('admin/dashboard/templates/userinfo.tpl');

Then code will be executed correctly!

This is a bug? Or did I do something wrong?

jameslai commented 9 years ago

+1

jameslai commented 9 years ago

After doing a little research, line 35 of trace.coffee seems to be the culprit. It removes the text! portion of the module name, but doesn't do the same to the references to the module. Removing this line seems to solve the problem.

As a note, I did a small test using r.js and a simple use case - r.js does not remove text! from any of the module names.

However, the tests in this repo fail once that chance is made, so someone felt this was an important distinction. Would love to get some insight into what the purpose of this was before making a PR.