如何在 requireJs 中使用多文件库

kra*_*dio 4 javascript requirejs js-amd

我用 requireJs 构建了一个项目,我的文件结构如下:

js/
   lib/
       noty/
           layouts/
               bottom.js
               top.js
               ...
           themes/
               default.js
           noty.jquery.js
       jquery.js
       jquery-ui.js
   user/
      user.js
   app.js
Run Code Online (Sandbox Code Playgroud)

而我的配置:

js/
   lib/
       noty/
           layouts/
               bottom.js
               top.js
               ...
           themes/
               default.js
           noty.jquery.js
       jquery.js
       jquery-ui.js
   user/
      user.js
   app.js
Run Code Online (Sandbox Code Playgroud)

我担心的是要集成 noty,这是一个我可以在任何模块中使用的通知插件。这个插件需要加载:

js/lib/noty/noty.jquery.js
js/lib/noty/layout/top.js
js/lib/noty/themes/bottom.js
Run Code Online (Sandbox Code Playgroud)

我不确定这样做的方法是什么?

  • 连接文件 ?

  • 将每个文件加载为依赖项?:

    requirejs(['jquery', 'jquery-ui', 'noty/noty.jquery.js', 'noty/layout/top.js'等]

  • 为 requireJs 创建某种插件/模块?

谢谢

som*_*eok 5

或者像这样:

paths: {
    'jquery': 'jquery/1.10.2/jquery',
    'noty': 'noty/2.0/jquery.noty',
    'noty.themes.default': 'noty/2.0/themes/default',
    'noty.layouts.topCenter': 'noty/2.0/layouts/topCenter',

    app: '../app',
    util: '../util'
},
shim: {
    'noty': ['jquery'],
    'noty.themes.default': {
        deps: ['jquery', 'noty'],
        exports: 'jquery'
    },
    'noty.layouts.topCenter': {
        deps: ['jquery', 'noty'],
        exports: 'jquery'
    }
}
Run Code Online (Sandbox Code Playgroud)