amdclean icon indicating copy to clipboard operation
amdclean copied to clipboard

require.toUrl support?

Open sprat opened this issue 9 years ago • 2 comments

I actually use the require.toUrl function to get images files URLs relatively to my modules files, with the root location of images specified in my require config.

// images.js
define(["require"], function (require) {
    return {
        logo: require.toUrl("images/logo.png");
    };
});

However, I don't know how to properly translate these require.toUrl calls when using AMDClean to build my project: the require.toUrl calls are kept in the optimized file, but obviously they don't work because there's no require available in the built file, and for good reason!

Any ideas how I can solve this problem? Can you please provide an example in the documentation?

sprat avatar Oct 05 '15 12:10 sprat

The require.toUrl function is not supported in amdclean, since the purpose of amdclean is to remove the module system and emit pure javascript.

I would suggest to provide a require mock with a toUrl function which for example returns the argument + a CDN prefix:

define("require", function() {
    return {
        toUrl: function(path) {
            return "https://mycdn.com/"+ path;
        }
    };
});

ooxi avatar Oct 05 '15 12:10 ooxi

Thanks for the feedback. I've experimented a little bit but I finally don't think it's a good idea to try to emulate the require.toUrl as the paths rules are fairly complicated in general... So I decided to create a "settings" module exporting some urls and I replace this module by another "settings" module in the build, which is not calling require.toUrl but use current script relative urls.

sprat avatar Oct 07 '15 08:10 sprat