The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / npm / test / packages / npm-test-platform / node_modules / browserify / doc / methods.markdown @ 54a4c639

History | View | Annotate | Download (4.67 KB)

1
methods
2
=======
3

    
4
This section documents the browserify api.
5

    
6
````javascript
7
var browserify = require('browserify');
8
````
9

    
10
var b = browserify(opts={})
11
---------------------------
12

    
13
Return a new bundle object.
14

    
15
`opts` may also contain these fields:
16

    
17
* watch - set watches on files, see below
18
* cache - turn on caching for AST traversals, see below
19
* debug - turn on source mapping for debugging with `//@ sourceURL=...`
20
in browsers that support it
21
* exports - an array of the core items to export to the namespace. Available
22
items: 'require', 'process'
23

    
24
If `opts` is a string, it is interpreted as a file to call `.addEntry()` with.
25

    
26
### watch :: Boolean or Object
27

    
28
Set watches on files and automatically rebundle when a file changes.
29

    
30
This option defaults to false. If `opts.watch` is set to true, default watch
31
arguments are assumed or you can pass in an object to pass along as the second
32
parameter to `fs.watchFile()`.
33

    
34
### cache :: Boolean or String
35

    
36
If `cache` is a boolean, turn on caching at
37
`$HOME/.config/browserify/cache.json`.
38

    
39
If `cache` is a string, turn on caching at the filename specified by `cache`.
40

    
41
### bundle events
42

    
43
`b` bundles will also emit events.
44

    
45
#### 'syntaxError', err
46

    
47
This event gets emitted when there is a syntax error somewhere in the build
48
process. If you don't listen for this event, the error will be printed to
49
stderr.
50

    
51
#### 'bundle'
52

    
53
In watch mode, this event is emitted when a new bundle has been generated.
54

    
55
b.bundle()
56
----------
57

    
58
Return the bundled source as a string.
59

    
60
By default, `require` is not exported to the environment if there are entry
61
files in the bundle but you can override that with `opts.exports`.
62

    
63
`process` is only exported to the environment when `opts.exports` contains the
64
string `'process'`.
65

    
66
b.require(file)
67
---------------
68

    
69
Require a file or files for inclusion in the bundle.
70

    
71
If `file` is an array, require each element in it.
72

    
73
If `file` is a non-array object, map an alias to a package name.
74
For instance to be able to map `require('jquery')` to the jquery-browserify
75
package, you can do:
76

    
77
````javascript
78
b.require({ jquery : 'jquery-browserify' })
79
````
80

    
81
and the same thing in middleware-form:
82

    
83
````javascript
84
browserify({ require : { jquery : 'jquery-browserify' } })
85
````
86

    
87
To mix alias objects with regular requires you could do:
88

    
89
````javascript
90
browserify({ require : [ 'seq', { jquery : 'jquery-browserify' }, 'traverse' ])
91
````
92

    
93
In practice you won't need to `b.require()` very many files since all the
94
`require()`s are read from each file that you require and automatically
95
included.
96

    
97
b.ignore(file)
98
--------------
99

    
100
Omit a file or files from being included by the AST walk to hunt down
101
`require()` statements.
102

    
103
b.addEntry(file)
104
----------------
105

    
106
Append a file to the end of the bundle and execute it without having to
107
`require()` it.
108

    
109
Specifying an entry point will let you `require()` other modules without having
110
to load the entry point in a `<script>` tag yourself.
111

    
112
If entry is an Array, concatenate these files together and append to the end of
113
the bundle.
114

    
115
b.filter(fn)
116
------------
117

    
118
Transform the source using the filter function `fn(src)`. The return value of
119
`fn` should be the new source.
120

    
121
b.register(ext, fn)
122
-------------------
123

    
124
Register a handler to wrap extensions.
125

    
126
Wrap every file matching the extension `ext` with the function `fn`.
127

    
128
For every `file` included into the bundle `fn` gets called for matching file
129
types as `fn.call(b, body, file)` for the bundle instance `b` and the file
130
content string `body`. `fn` should return the new wrapped contents.
131

    
132
If `ext` is unspecified, execute the wrapper for every file.
133

    
134
If `ext` is 'post', execute the wrapper on the entire bundle.
135

    
136
If `ext` is 'pre', call the wrapper function with the bundle object before the
137
source is generated.
138

    
139
If `ext` is an object, pull the extension from `ext.extension` and the wrapper
140
function `fn` from `ext.wrapper`. This makes it easy to write plugins like
141
[fileify](https://github.com/substack/node-fileify).
142

    
143
Coffee script support is just implemented internally as a `.register()`
144
extension:
145

    
146
````javascript
147
b.register('.coffee', function (body) {
148
    return coffee.compile(body);
149
});
150
````
151

    
152
b.use(fn)
153
---------
154

    
155
Use a middleware plugin, `fn`. `fn` is called with the instance object `b`.
156

    
157
b.prepend(content)
158
------------------
159

    
160
Prepend unwrapped content to the beginning of the bundle.
161

    
162
b.append(content)
163
-----------------
164

    
165
Append unwrapped content to the end of the bundle.
166

    
167
b.alias(to, from)
168
-----------------
169

    
170
Alias a package name from another package name.
171

    
172
b.modified
173
----------
174

    
175
Contains a Date object with the time the bundle was last modified. This field is
176
useful in conjunction with the `watch` field described in the `browserify()` to
177
generate unique `<script>` `src` values to force script reloading.