CHAPTER 16
Perhaps you are ready to take your components to the next level and allow other developers to use and reference them. You would accomplish this by creating a plugin so that any developer can use the package manager to install your components. As we have been using WebPack throughout this book, I will walk you through registering your plugin with Node Package Manager so that it can be installed on a developer’s box.
Plugins require a little more work with the third-party package managers, but once in place, using a plugin is a breeze. One thing to consider is that you should ensure the names of your components are unique and do not conflict with an Aurelia component or another third party. A common approach to solving this is to provide a prefix in front of all of your components to help keep their names unique. Let’s pretend that we are going to use the bs prefix and have the names bs-input and bs-attach-focus for our components.
We will want to have a separate project for our plugin repository. We will use GitHub for our repository. Let’s take a look at what the package.json would look like in our new project:
Code Listing 132
{ "name": "aurelia-succinctly", "version": "0.0.1", "description": "This is a set of components for the Aurelia Succinctly book.", "keywords": [ "aurelia", "plugin" ], "homepage": "http://mattduffield.wordpress.com", "bugs": { "url": "https://github.com/mattduffield/aurelia-succinctly/issues" }, "license": "CC0-1.0", "author": "Matt Duffield <[email protected]> (http://mattduffield.wordpress.com/)", "main": "dist/commonjs/index.js", "repository": { "type": "git", "url": "http://github.com/mattduffield/aurelia-succinctly" }, "aurelia": { "build": { "resources": [ "aurelia-succinctly/bs-input", "aurelia-succinctly/bs-attach-focus" ] } }, "devDependencies": { "aurelia-tools": "^0.2.4", "babel": "^6.5.2", "babel-eslint": "^6.1.2", "babel-plugin-syntax-flow": "^6.8.0", "babel-plugin-transform-decorators-legacy": "^1.3.4", "babel-plugin-transform-es2015-modules-amd": "^6.8.0", "babel-plugin-transform-es2015-modules-commonjs": "^6.11.5", "babel-plugin-transform-es2015-modules-systemjs": "^6.11.6", "babel-plugin-transform-flow-strip-types": "^6.8.0", "babel-preset-es2015": "^6.9.0", "babel-preset-es2015-loose": "^7.0.0", "babel-preset-stage-1": "^6.5.0", "conventional-changelog": "1.1.0", "del": "^2.2.1", "gulp": "^3.9.1", "gulp-babel": "^6.1.2", "gulp-bump": "^2.2.0", "gulp-eslint": "^3.0.1", "gulp-yuidoc": "^0.1.2", "isparta": "^4.0.0", "istanbul": "^1.0.0-alpha.2", "jasmine-core": "^2.4.1", "karma": "^1.1.2", "karma-babel-preprocessor": "^6.0.1", "karma-chrome-launcher": "^1.0.1", "karma-coverage": "^1.1.1", "karma-jasmine": "^1.0.2", "karma-jspm": "2.2.0", "object.assign": "^4.0.4", "require-dir": "^0.3.0", "run-sequence": "^1.2.2", "vinyl-paths": "^2.1.0", "yargs": "^4.8.1" }, "jspm": { "registry": "npm", "jspmPackage": true, "main": "index", "format": "amd", "directories": { "dist": "dist/amd" }, "peerDependencies": {}, "devDependencies": { "aurelia-polyfills": "^1.0.0-beta.1.1.0" } } } |
Please provide your own pertinent information from name down to repository. Under the section aurelia/build/resources, you will want to identify all of your components you wish to publish. Finally, provide whatever devDependencies and jspm entries you like.
The file structure for your project should look like the following:
Code Listing 133
aurelia-succinctly/ build/ dist/ doc/ CHANGELOG.md node_modules/ src/ bs-input.html bs-input.js attach-focus.js index.js test/ .gitignore config.js gulpfile.js karma.conf.js LICENSE package.json README.md |
This structure follows what you would see in the skeleton navigation example, with the exception that you only have your components in the src/ folder. The build/ folder handles all the building and bundling of the source code. The dist/ folder is what you get when you bundle your source for ready for deployment. The doc/ folder is where you would have your change log. The node_modules/ folder is what you get when you run npm install. The src/ folder is where all of your components reside. The test/ folder is where you would provide any tests for your components. The rest is pretty much standard.
A typical workflow is to have a separate project where you test your components in an existing application as a feature. This will allow you to rapidly correct any issues and then transfer any changes to this project. You will need to save your changes to your Git repository. Finally, you can publish your repository to the Node Package Manager with the following command:
Code Listing 134
npm publish ./ |
If you haven’t already set up your npm author info, you will need to have following before you can publish:
Code Listing 135
npm set init.author.name "Your name" npm set init.author.email "[email protected]" npm set init.author.url "http://yourblog.com" npm adduser |
You will be able to make changes to your Git repository and commit, but you will need to update the version in your package.json file before you try and publish again, as it will complain that you already have that version published.
The last step would be to consume the plugin from another project. You will first want to install the plugin using the following command:
Code Listing 136
npm install --save aurelia-succinctly |
Finally, you will want to be sure that your main.js file includes the plugin as follows:
Code Listing 137
import '../styles/styles.css'; import 'font-awesome/css/font-awesome.css'; import 'bootstrap/dist/css/bootstrap.css'; import 'bootstrap'; import * as Bluebird from 'bluebird'; Bluebird.config({ warnings: false }); export async function configure(aurelia) { aurelia.use .standardConfiguration() .developmentLogging() .plugin('aurelia-succinctly'); await aurelia.start(); aurelia.setRoot('app'); } |
This is a lot more complicated than just working with a feature, but once you have all the pieces in place, third-party developers will simply be able to install your plugin and update their main.js file, and they are ready to go.