How to import packages within my Aurelia application

ぃ、小莉子 提交于 2019-12-06 10:09:05

What happens is that aurelia skeleton-esnext uses SystemJS, and Aurelia-CLI uses RequireJS (SystemJS and WebPack will be supported on CLI in a near future). SystemJS and RequireJS are module loaders, and they behave a little different. That's why you have 2 different ways to configure the modules.

This link shows how to configure a lib that has a css file: http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/contact-manager-tutorial/9

"dependencies": [
  ...
  {
    "name": "nprogress",
    "path": "../node_modules/nprogress",
    "main": "nprogress",
    "resources": [
      "nprogress.css"
    ]
  }
  ...
]

The above code tells RequireJS to put nprogress.css in the bundled file. However, it doesn't tell where or when to import/require the css file, so you must import/require it in your app. Either using <require from="nprogress/nprogress.css"></require> in your view, or using import 'nprogress/nprogress.css'; in your view-model.

When you were using SystemJS you didn't have to import some css files because JSPM can detect some dependencies and generate a special syntax that tells SystemJS to load CSSs files. For example, when you install nprogress using JSPM, it generates the following file:

/* */ 
"format global";
"deps ./nprogress.css!";
/* NProgress, (c) 2013, 2014 Rico Sta. Cruz - http://ricostacruz.com/nprogress
 * @license MIT */

;(function(root, factory) {
///....

The line "deps ./nprogress.css!"; tells SystemJS to automatically import/require nprogress.css when you load the module. Therefore, you don't have to import/require the css file in your view or view-model.

Hope this helps!

Check out the contact-manager tutorial by Rob Eisenberg

It shows how to add Jquery, Bootstrap to aurelia.json:

"dependencies": [
...
"jquery",
{
"name": "bootstrap",
"path": "../node_modules/bootstrap/dist",
"main": "js/bootstrap.min",
"deps": ["jquery"],
"exports": "$",
"resources": [
  "css/bootstrap.css"
]
},
...
]

And how to require it in app.html:

<require from="bootstrap/css/bootstrap.css"></require>

I was able to successfully do the same for D3:

{
"name": "d3",
"path": "../node_modules/d3/build",
"main": "d3.js"
},

Then I was able to use it in my components .js file

import * as d3 from d3

[Edit] Just noticed tutorial link was included in above accepted answer, but keeping this as it shows the actual code for adding jquery/bootstrap, which has some elements missing in the OP examples, including after the edit.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!