I'm trying to move one of my custom elements into a plug-in so that I can re-use it across projects.
I had a look at the skeleton plugin and noticed that it has a src/index.js
that returns a config with all custom elements defined as globalResources
.
So I tried the same thing and I basically have:
src/index.js
export function configure (config) {
config.globalResources([
'./google-map',
'./google-map-location-picker',
'./google-map-autocomplete'
]);
}
And then I have each one of my custom elements next to index.js
, for example:
google-map.js
import {inject, bindable, bindingMode, inlineView} from 'aurelia-framework';
@inlineView(`
<template>
<div class="google-map"></div>
</template>
`)
@inject(Element)
export class GoogleMapCustomElement {
// All the Custom Element code here
}
I've also set up a basic npm script that runs babel on the code and sticks it in dist/
:
"main": "dist/index.js",
"babel": {
"sourceMap": true,
"moduleIds": false,
"comments": false,
"compact": false,
"code": true,
"presets": [ "es2015-loose", "stage-1"],
"plugins": [
"syntax-flow",
"transform-decorators-legacy",
"transform-flow-strip-types"
]
},
"scripts": {
"build": "babel src -d dist"
},
Tbh I'm not entirely sure this is all correct but I took some of it from the skeleton plugin and it seems to run fine.
Anyway, the problem I'm having is that after I install the plugin (npm install --save-dev powerbuoy/AureliaGoogleMaps
), add it to my aurelia.json
in build.bundles[vendor-bundle.js].dependencies
and tell aurelia to use it in main.js
(.use.plugin('aurelia-google-maps')
) I get:
GET http://localhost:9000/node_modules/aurelia-google-maps/dist/index/google-map.js
(404)
So my question is, where does it get the dist/index/
part from?? I'm configuring my globalResources
in index.js
but nowhere does it say that I have an index
folder.
What am I doing wrong?
Bonus question: What is the bare minimum required to transpile my ES6 plug-in code so that others can use it? Does my babel configuration look correct?
What about referencing your plugin within aurelia.json, like this:
Update:
Edit: thanks for clarifying why you don't want to use the whole skeleton-plugin package.
Focusing on your original question:
aurelia-cli
uses RequireJS (AMD format) to load dependencies. Probably, your current output has a different format.Add
transform-es2015-modules-amd
to babel.plugins to ensure AMD-style output, so it will be compatible with RequireJS and therefore with aurelia-cli.Original:
There are several blog post about plugin creation, I started with this: http://patrickwalters.net/making-out-first-plugin/ .
Of course, there have been many changes since then, but it's a useful piece of information and most of it still applies.
I'd recommend using plugin-skeleton as project structure. It provides you with a working set of gulp, babel, multiple output formats out-of-the-box. With this approach, your plugin's availability wouldn't be limited to JSPM or CLI only but everyone would have the possibility to install it regardless of their build systems.
Migration is fairly easy in your case:
index.js
intosrc/
npm install
gulp build
dist/
folderHere are some details based on my observations/experience.
1. Main
index.js/plugin-name.js
:In general, a main/entry point is required, where the plugin's configure() method is placed. It serves as a starting point when using it within an Aurelia application. This file could have any name, usually it's
index.js
orplugin-name.js
(e.g. aurelia-google-maps.js) to make it clear for other developers what should be included for bundling. Set that same entry point in package.json as well.In addition to globalResources, you can implement a callback function to allow configuration overrides. That can be called in the application, which will use the plugin. Example solution
Plugin's
index.js
Your app's
main.js
2. HTML and CSS resources:
globalResources
.3. Using the plugin with aurelia-cli: Documentation
While we are looking forward to that above moment, let's edit
aurelia.json
:Now, your plugin is ready to include it in
main.js
as showed in Section 1..I hope you didn't get sick of reading the word 'plugin' so many (21!) times. :D
I have absolutely no idea why, but in order to solve this problem I actually had to move my custom elements inside an
index/
folder.So now I have this:
And my
index.js
still looks like this:Where it gets
index/
from I guess I will never know, but this works at least.I did need the babel plug-in Marton mentioned too, but that alone did not solve the mystery of the made up path.
Edit: To elaborate a bit further, if I name my main entry point something other than
index.js
the folder too needs that name. For example, if I were to renameindex.js
main.js
I would need to put myglobalResources
inside a folder calledmain/
.