UNPKG

4.28 kBMarkdownView Raw
1jspm can be installed and used as an API dependency in a Node project:
2
3```
4 npm install jspm
5```
6
7```javascript
8 var jspm = require('jspm');
9 jspm.install('jquery'); // etc
10```
11
12The API is currently unstable and subject to change. The API is also lacking most jspm functions. New API suggestions or adjustments are always welcome, and PRs would be taken gladly.
13
14## jspm API version 0.2
15
16#### setPackagePath(packagePath)
17
18Sets the directory where the jspm `package.json` file for the package being acted on is to be found.
19
20Must be run before any other API calls.
21
22### Loader API
23
24#### import
25
26Loads a module in the jspm project in Node:
27
28```javascript
29var jspm = require('jspm');
30jspm.setPackagePath('.');
31
32jspm.import('fs').then(function(fs) {
33 console.log(fs.readFileSync('./package.json'));
34});
35```
36
37jspm supports all Node libraries on the server and uses their Browserify equivalents in the browser.
38
39[Read more about NodeJS usage of jspm](nodejs-usage.md).
40
41#### normalize(name, parentName) -> Promise(normalized)
42
43Normalize a module name within the current jspm project.
44
45#### class Loader
46
47For more loader flexibility within the API, a new custom SystemJS loader instance can be created
48based on the current jspm environment:
49
50```javascript
51var jspm = require('jspm');
52jspm.setPackagePath('.'); // optional
53
54var mySystem = new jspm.Loader();
55
56// can be used as any other System instance
57mySystem.normalize('moduleName').then(function(normalized) {
58
59});
60mySystem.import('moduleName').then(function(module) {
61
62});
63```
64
65[Read more on the SystemJS API](https://github.com/systemjs/systemjs/blob/master/docs/system-api.md)
66
67### Bundle API
68
69#### bundle(expression, fileName, options) -> Promise()
70
71```javascript
72// jspm bundle app/main build.js --no-mangle
73var jspm = require('jspm');
74jspm.setPackagePath('.');
75jspm.bundle('app/main', 'build.js', { mangle: false }).then(function() {
76});
77```
78
79Set the `injectConfig` option to inject the bundle tree into the configuration file.
80
81### unbundle() -> Promise()
82
83Removes any existing `depCache` or `bundle` configuration from the configuration file.
84
85#### bundleSFX(moduleName, fileName, options) -> Promise()
86
87Creates a single self-executing bundle for a module.
88
89##### Bundle Options
90
91Both `bundle` and `bundleSFX` support the following options:
92
93* `minify`: Use minification, defaults to false.
94* `mangle`: Use mangling with minification, defaults to true.
95* `lowResSourceMaps`: Use faster low-resolution source maps, defaults to true.
96* `sourceMaps`: Use source maps, defaults to true.
97
98#### class Builder
99
100When more build flexibility is needed, create a custom SystemJS Builder instance for the current jspm environment via:
101
102```javascript
103var jspm = require('jspm');
104jspm.setPackagePath('.'); // optional
105
106var builder = new jspm.Builder();
107
108builder.config({ custom: 'options' });
109
110// or builder.buildStatic
111builder.bundle('app/main.js', {
112 minify: true
113})
114.then(function(output) {
115 // output is now an in-memory build
116 // output.source
117
118 // get the depCache configuration for the tree
119 var depCache = builder.getDepCache(output.tree);
120});
121```
122
123The builder will be automatically configured to have the correct jspm configuration and baseURL for the environment.
124
125[Read more on the builder API at the SystemJS builder project page](https://github.com/systemjs/builder)
126
127### Package Manager API
128
129#### dlLoader -> Promise()
130
131Downloads the loader files if needed.
132
133#### install(name [,target] [, options]) -> Promise()
134#### install(targets [, options]) -> Promise()
135#### install(true [, options]) -> Promise()
136
137Installs the given package or packages.
138
139```javascript
140// jspm install jquery
141jspm.install('jquery')
142
143// jspm install jquery=github:components/jquery@^2.0.0
144jspm.install('jquery', 'github:components/jquery@^2.0.0')
145
146// jspm install jquery=2
147// jspm install jquery@2
148jspm.install('jquery', '2')
149
150// jspm install jquery --force
151jspm.install('jquery', { force: true })
152
153// jspm install jquery@1.2.3 bootstrap --link
154jspm.install({ jquery: '1.2.3', 'bootstrap': true }, { link: true })
155
156// jspm install
157// reproducible install from package.json
158jspm.install(true, { lock: true })
159```
160
161#### uninstall(name) -> Promise()
162#### uninstall(names) -> Promise()
163
164Can take a single module or array of modules to uninstall.
165