UNPKG

5.49 kBMarkdownView Raw
1### 1. Install jspm CLI:
2
3 ```
4 npm install jspm -g
5 ```
6
7### 2. Create a project:
8
9Optionally lock down jspm for the project:
10
11 ```
12 cd my-project
13 npm install jspm --save-dev
14 ```
15
16> It is advisable to locally install to lock the version of jspm.
17This will ensure upgrades to the global jspm will not alter the behavior of your application.
18Use `jspm -v` to confirm the local version.
19
20Create a new project configuration:
21
22 ```
23 jspm init
24
25Package.json file does not exist, create it? [yes]:
26Would you like jspm to prefix the jspm package.json properties under jspm? [yes]:
27Enter server baseURL (public folder path) [.]:
28Enter jspm packages folder [./jspm_packages]:
29Enter config file path [./config.js]:
30Configuration file config.js doesn't exist, create it? [yes]:
31Enter client baseURL (public folder URL) [/]:
32Which ES6 transpiler would you like to use, Traceur or Babel? [traceur]:
33```
34
35 Sets up the package.json and configuration file.
36 Note `jspm init` happens automatically if you try installing into an empty project too.
37
38* **baseURL**: This should be set to the public folder where your server will serve from, relative to the package.json file. _Defaults to the package.json folder itself._
39* **jspm packages folder**: The folder where jspm will install external dependencies.
40* **Config file path**: The jspm config file for your application. Should be within the baseURL and checked in to version control.
41* **Client baseURL**: The URL from the browser where the public folder is hosted.
42* **Transpiler**: Change this option at any time with `jspm dl-loader babel`. Custom transpilation options can also be set through `babelOptions` or `traceurOptions` in the jspm config file.
43
44If you ever need to reset any of these properties, you can modify the package.json, which will update the configuration when you next run `jspm install` or `jspm init` to refresh.
45
46It is possible to run through the above prompts again at any time with `jspm init -p`.
47
48### 3. Install any packages from the jspm Registry, GitHub or npm:
49
50 ```
51 jspm install npm:lodash-node
52 jspm install github:components/jquery
53 jspm install jquery
54 jspm install myname=npm:underscore
55 ```
56
57 Multiple installs can also be combined into a single line separated by spaces.
58
59 Any npm or Github package can be installed in this way.
60
61 Most npm packages will install without any configuration necessary.
62 This is because the npm registry endpoint applies conversion operations based on the assumption of
63 Node require resolution making the Node and npm-style code compatible with jspm.
64
65Github packages may need to be configured for jspm first.
66[Read the guide here on configuring packages for jspm](https://github.com/jspm/registry/wiki/Configuring-Packages-for-jspm).
67
68 All installs are saved into the package.json, so that the jspm_packages folder and
69 configuration file can be entirely recreated with a single `jspm install` call with no arguments.
70 This is ideal for version-controlled projects where third party packages aren't saved in the repo itself.
71
72 The config.js file is updated with the version information and the version is locked down,
73 this configuration file itself forming the lock, which should be added to version control.
74
75### 4. Write application code
76
77We can now write into our `lib` folder code that loads our dependencies, in any module format (including ES6):
78
79 lib/bootstrap.js
80 ```javascript
81 import _ from 'lodash-node/modern/lang/isEqual.js';
82 import $ from 'jquery';
83 import underscore from 'myname';
84
85 export function bootstrap() {
86 // bootstrap code here
87 }
88 ```
89
90 lib/main.js
91 ```javascript
92 import {bootstrap} from './bootstrap.js';
93 bootstrap();
94 ```
95
96> Note modules are only detected as ES6 when module syntax is present. This is because the module loader transpilation is linked to the ES module format currently, althought this transpilation layer will be separated in future. Read more about SystemJS module formats at https://github.com/systemjs/systemjs/blob/master/docs/module-formats.md.
97
98### 5. Run the code
99
100In an HTML page include the automatically downloaded SystemJS loader along with the config file, then import our application main entry point:
101
102 ```html
103 <!doctype html>
104 <script src="jspm_packages/system.js"></script>
105 <script src="config.js"></script>
106 <script>
107 System.import('lib/main.js');
108 </script>
109 ```
110
111Run a local server to view the page.
112
113> jspm makes requests for many files. For best performance, ideally try to use a performant HTTP/2-enabled development server
114 and set up `jspm_packages` to be served with far-future expires from the local server so that it is cached in the browser
115 and separate requests don't need to be made.
116
117### 6. Bundle for production
118
119```
120 jspm bundle lib/main --inject
121```
122
123Refresh the browser, and see the entire app loaded from a single bundle file.
124
125Alternatively, use `jspm build lib/main` to create a bundle script that can be used on its own with a `<script>` tag independent of `config.js` and `system.js`.
126
127### Next Steps
128
129[Read more about the production workflows](production-workflows.md)
130
131[Read the guide on configuring packages for jspm here](https://github.com/jspm/registry/wiki/Configuring-Packages-for-jspm).
132
133_If you are having any trouble configuring a package for jspm, please just [post an issue to the registry](https://github.com/jspm/registry/) and we'll help get it configured._
134
\No newline at end of file