1 | Code published for jspm can fall into three main scenarios:
|
2 |
|
3 | * [Writing a library for consumption in jspm and other environments](#writing-a-library-for-consumption-in-jspm-and-other-environments)
|
4 | * [Publishing jspm-style packages to npm](#publishing-es6-to-npm)
|
5 | * [Writing a library or application for usage just with jspm](#writing-a-library-or-application-for-usage-just-with-jspm)
|
6 |
|
7 | Each of these scenarios has pro's and con's and the one that we feel you should focus most on is the third one.
|
8 |
|
9 | ## Writing a library for consumption in jspm and other environments
|
10 |
|
11 | This is the common case if you're writing a piece of utility code that you want to be available to as many users as possible.
|
12 |
|
13 | Currently the best option for this scenario is to write a package in the npm-style with npm-style dependencies and **publish that package to npm**. You can then test against the jspm environment and if necessary add any custom [jspm config properties](https://github.com/jspm/registry/wiki/Configuring-Packages-for-jspm) that may be needed to make it work in jspm, but the primary environment and publishing target is npm.
|
14 |
|
15 | ## Publishing jspm-style packages to npm
|
16 |
|
17 | > This approach is not recommended, use the [Writing a library or application for usage just with jspm](#writing-a-library-or-application-for-usage-just-with-jspm) scenario instead.
|
18 |
|
19 | If you really want to use npm for jspm-style code, set the `jspmNodeConversion` property to false in the package.json. This will disable the usual operations that attempt to convert from Node and npm style code into jspm-compatible code:
|
20 |
|
21 | ```json
|
22 | {
|
23 | "jspmNodeConversion": false,
|
24 | "jspm": {
|
25 | "format": "es6",
|
26 | "dependencies": {
|
27 | }
|
28 | }
|
29 | }
|
30 | ```
|
31 |
|
32 | Then you can publish ES6 (or use other formats too) and jspm-style dependencies in your package.json fine on npm. You can choose to wrap the above with a `jspm` property as you wish.
|
33 |
|
34 | ## Writing a library or application for usage just with jspm
|
35 |
|
36 | In this scenario, you're writing a library or application that is specifically taking advantage of jspm features such as:
|
37 |
|
38 | * Using jspm plugins
|
39 | * Supporting multi-format module loading
|
40 | * Supporting dependencies from multiple registries
|
41 | * Lazy-loading with `System.import`
|
42 | * Building on top of jspm API features
|
43 |
|
44 | For this workflow, it is advisable to publish your code to GitHub, and tagging semver versions for install:
|
45 |
|
46 | ```
|
47 | git tag v0.1.0
|
48 | git push origin v0.1.0
|
49 | ```
|
50 |
|
51 | With the above, you can publish ES6 directly (not recommended until uglify supports ES6), or any other module format, and take advantage of all jspm features.
|
52 |
|
53 | That is all you need to do to make your package installable by anyone with `jspm install github:user/repo@0.1.0`.
|
54 |
|
55 | > Note that the `v` prefix in the version tag is optional.
|
56 |
|
57 | It is advisable to ensure the following package.json properties are set when publishing jspm packages to GitHub:
|
58 |
|
59 | * `format`: The module format you've written your package in - `esm`, `amd`, `cjs` or `global`.
|
60 | * `directories.lib`: A subdirectory to install your package from, all other directories and files are then ignored and paths are relative to this folder.
|
61 | * `main`: The main entry point for your package. If using a `directories.lib` the main is relative to this folder.
|
62 | * `registry`: Typically set this to `jspm` as the registry you have written your package for.
|
63 | * `jspm.dependencies`: jspm-style dependencies for your project.
|
64 |
|
65 | Further information about package.json configuration can be [read at the configuration guide](https://github.com/jspm/registry/wiki/Configuring-Packages-for-jspm).
|
66 |
|
67 | #### Example Publishing Workflow
|
68 |
|
69 | Say I want to create an ES6 package that uses the CSS and JSX plugins to export a React component:
|
70 |
|
71 | ```
|
72 | jspm init -y
|
73 | jspm install css jsx react@0.13.0-beta.1
|
74 | ```
|
75 |
|
76 | By default the `lib` folder is the folder that is set as `directories.lib` and is what users will be requiring from.
|
77 |
|
78 | So edit `lib/index.jsx`:
|
79 |
|
80 | ```javascript
|
81 | import './style.css!';
|
82 | import React from 'react';
|
83 | export default class Component extends React.component {
|
84 | render() {
|
85 | return <div>Hello {this.props.name}</div>;
|
86 | }
|
87 | }
|
88 | ```
|
89 |
|
90 | We can test this package locally in the browser with `System.import('app/index.jsx!')`.
|
91 |
|
92 | Then to publish set the package.json `main` to `index.jsx!`, and the package.json `format` to `es6`.
|
93 |
|
94 | Because this package will only be available for jspm users, we can also write all the properties at the base-level and add a `registry: jspm` property instead. This gives us:
|
95 |
|
96 | ```json
|
97 | {
|
98 | "directories": {
|
99 | "lib": "lib"
|
100 | },
|
101 | "main": "index.jsx!",
|
102 | "format": "es6",
|
103 | "registry": "jspm",
|
104 | "dependencies": {
|
105 | "css": "^0.1.0",
|
106 | "jsx": "^0.1.1",
|
107 | "react": "0.13.0-beta.1"
|
108 | }
|
109 | }
|
110 | ```
|
111 |
|
112 | Publishing this package under a `v0.1.0` version tag to GitHub then allows users to easily load this component with:
|
113 |
|
114 | ```
|
115 | jspm install react-component=github:my/react-component@0.1.0
|
116 |
|
117 | ok Installed react-component as github:my/react-component@0.1.0 (0.1.0)
|
118 | ```
|
119 |
|
120 | ```javascript
|
121 | import reactComponent from 'react-component';
|
122 | ```
|
123 |
|
124 | Sub-requires can also be made from inside the package if we wanted other modules in the `lib` folder to be available to users:
|
125 |
|
126 | ```javascript
|
127 | import AnotherComponent from 'react-component/another-component.jsx!';
|
128 | ```
|