what's mean “!” in require.js

2019-02-19 14:02发布

问题:

What's mean "!" in require.js when I included module ? What's syntax ? I what includ dinamic stylesheet in my project and I found https://github.com/martinsb/require-css plugin It's works fine .

require(['css!css/sample.css'], function() {
                        alert('Stylesheet has been loaded');
                    }); 

Why ['css!css/sample.css'] include css.js and css/sample.css ?

Edit:

I know alternative :

    define(["controller", "RequireCSS","marionette"], 
 function (Controller, css,  Marionette) {

        css.load("default.css");

RequireCSS.js

define(function () {
    return {
        load: function (url) {
            var link = document.createElement("link");
            link.type = "text/css";
            link.rel = "stylesheet";
            link.href = url;
            document.getElementsByTagName("head")[0].appendChild(link);
        }
    }

});

回答1:

By default requirejs has support for downloading and loading javascript files only. Any other file which need to be downloaded and parsed accordingly is done by means of plugins. These plugins are separate javascript files which know how to download and parse the respected file.

To tell requirejs that any particular file need to be handled by separate plugin and not default loader. It's plugin is appended with ! between plugin name and file path.



回答2:

! means that css plugin will be used to parse sample.css.

Syntax is <plugin-name>!<file-path>.

css.js file contains css plugin's code.

css/sample.css is file, that should be loaded using that plugin.

See https://github.com/millermedeiros/requirejs-plugins



标签: requirejs