Uncaught TypeError creating an Aurelia Plugin: plugin.load is not a function
Asked Answered
H

1

6

I created a new Aurelia plugin, which is named Aurelia-Slickgrid and got it all working locally with .feature('resources'). I was then ready for the next step, making it available to everyone as a plugin and created a Github project and made it available under this GitHub link - Aurelia-Slickgrid. I used Aurelia Skeleton Plugin to create my plugin, also ran gulp build to create the transpiled output. I then published it on NPM and successfully installed it with npm i --save aurelia-slickgrid. I am currently using Aurelia-CLI to bundle and everything goes well, until I open the browser and see that an error got thrown in the console (now I'm left with a white page in the browser):

Uncaught TypeError: plugin.load is not a function
    at Module.<anonymous> (vendor-bundle.js:5308)
    ...

For the moment, I am trying to load it as a plugin with Aurelia-CLI. See below for the complete configuration used:

index.js of the plugin

import {AureliaSlickgrid} from './aurelia-slickgrid';
import {SlickPager} from './slick-pager';
import {SlickWindowResizer} from './slick-window-resizer';

export function configure(config) {
  config.globalResources('./aurelia-slickgrid');
  config.globalResources('./slick-pager');
  config.globalResources('./slick-window-resizer');
}

export {
  AureliaSlickgrid,
  SlickPager,
  SlickWindowResizer
}

main.js (of my local project)

aurelia.use
    .standardConfiguration()
    .feature('resources')
    .plugin('aurelia-slickgrid');

aurelia.json (of my local project)

{
     "name": "aurelia-slickgrid",
     "path": "../node_modules/aurelia-slickgrid/dist/amd",
     "main": "index"
},

If you're testing with an empty Aurelia CLI project you need to add:

      {
        "name": "aurelia-slickgrid",
        "path": "../node_modules/aurelia-slickgrid/dist/amd",
        "main": "index"
      },
      "slickgrid-es6",
      "jquery"

Finally import to use it in code via:

import {AureliaSlickgrid} from 'aurelia-slickgrid';

The documentation on how to create an Aurelia Plugin is almost non existing, so I based myself on another plugin, namely Aurelia-Bootstrap made by a great Aurelia Contributor. One of the file that greatly influenced my plugin code was his index.js and I used his coding styling to code mine.

Any idea where could be my problem?


I also tried to add resources to the aurelia.json, however it had no effects.

{
  "name": "aurelia-slickgrid",
  "path": "../node_modules/aurelia-slickgrid/dist/amd",
  "main": "index",
  "resources": [
    "**/*.html"
  ]
},

Got a bit further, after searching for a few other Aurelia plugins. It seems that globalResources should only be View/ViewModel pair (html/js), in my case I only have 1 of those pair which is SlickPager and now only that one is being called by globalResources(). Another possible problem, still to confirm though, I had a file named aurelia-slickgrid.js which is the same naming as the plugin name, could that cause a conflict? Possibly...who knows. So anyway, I renamed it to slick-service.js and renamed the class to SlickService. I am now able to call 2/3 of my objects in the WebPack Skeleton, however I still can't import SlickService.

updated index.js of the plugin

import { SlickService } from './slick-service';
import { SlickPager } from './slick-pager';
import { SlickWindowResizer } from './slick-window-resizer';

export function configure(aurelia) {
  aurelia.globalResources('./slick-pager');
}

export { SlickService, SlickPager, SlickWindowResizer };

I can now call this in WebPack without issues

import {SlickPager, SlickWindowResizer} from 'aurelia-slickgrid';
@inject(SlickPager, SlickWindowResizer)

export class Test {
  constructor(slickPager, slickWindowResizer) {
    console.log(slickPager, slickWindowResizer);
  }
}

However if I import SlickService as well, it throws an error

import {SlickPager, SlickWindowResizer, SlickService} from 'aurelia-slickgrid';
@inject(SlickPager, SlickWindowResizer, SlickService)

export class Test {
   constructor(slickPager, slickWindowResizer, slickService) {
      console.log(slickPager, slickWindowResizer, slickService);
   }
}

previous code with SlickService now throws this error

ERROR [app-router] Error: Error invoking SlickService. Check the inner error for details.
------------------------------------------------
Inner Error:
Message: key/value cannot be null or undefined. Are you trying to inject/register something that doesn't exist with DI?
Hatten answered 8/1, 2017 at 23:46 Comment(7)
Perhaps you are missing to load the HTML files? "resources": ["**/*.html"] after your "main": "index" entry.Clinic
One minor issue with the code: this should be jquery instead of bootstrap. I wonder if this is a bug with the aurelia-cli. Have you tried using the plugin in one of the skeleton projects?Goraud
@MarcScheib I forgot to mention that I already tried to add 'resources' with **/*.html and no differences. @JeremyDanyow I changed the jquery reference and re-published a version and that doesn't fix the issue (also note that it was all working locally). I only tried Aurelia-CLI because I don't know how to load SASS in Aurelia-Webpack which my plugin usesHatten
I've had difficulty creating my first Aurelia plugin as well. I posted on stack overflow and didn't get the issue resolved. There really needs to be documentation and a step-by-step guide. Plugins are big since they allow the community to easily share. Making plugin development easier should be a top priority.Annikaanniken
Got a bit further, but still not out of the wood. Can someone confirm that in globalResources() call, it should only be View/ViewModel pair? Also, does it matter if a js file is named exactly like the plugin name (in my case aurelia-slickgrid.js)?Hatten
in slick-window-resizer.js you're exporting SlickWindowResizer but importing SlickResizer in slick-service.js, hence the null or undefined inject errorCellulitis
Thanks @noj, that was it. Your answer and RabahG answer help me fix it. I can finally sleep :)Hatten
S
5
  1. It doesn't matter if you call your file the same as your plugin.
  2. globalResources accepts not only view/vm pairs but (.html for html-only components and single file resources like custom attributes binding behaviors and value converters).
  3. you have a typo in slick-service.js you should be importing SlickWindowResizer not SlickResizer.
  4. in aurelia.json you need to add the resources array, like: "resources": ["**/*.{css,html}"]
Saline answered 10/1, 2017 at 16:7 Comment(2)
Yes this is it, I finally got it working thanks to @Cellulitis and yourself. Your point 4 also helped me get going with Aurelia-CLI as well, and I also didn't know you could write it on 1 line to get all css and html which is awesome. This is awesome :)Hatten
This kind of information should really be part of Aurelia Plugin documentation... the problem is that the doc doesn't exist :(Hatten

© 2022 - 2024 — McMap. All rights reserved.