You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fast, extensible, statically typed, and light Javascript transformer. (pronounced `nee-boo`)
4
8
5
9
**Why bother?** Nebu saves developers from the slow and heavy [Babel][1] compiler. Nebu skips AST-to-code generation, preferring simple string mutations, while keeping sourcemap support. This improves performance, preserves coding style, and makes plugins less clunky.
6
10
7
-
For ES6 support, use [Bublé][2]*after* using Nebu.
11
+
If you need to transpile ES6+ to ES5, use [Bublé][2]*after* using Nebu.
8
12
9
13
If you believe in Nebu's mission, consider building a Nebu plugin. The ecosystem is practically non-existent. It needs your help! 🤓
10
14
@@ -13,6 +17,25 @@ If you believe in Nebu's mission, consider building a Nebu plugin. The ecosystem
13
17
[1]: https://github.com/babel/babel
14
18
[2]: https://github.com/Rich-Harris/buble
15
19
20
+
21
+
22
+
## Examples
23
+
24
+
See the `examples` folder for plugin examples.
25
+
26
+
You can test these examples like so:
27
+
28
+
```sh
29
+
git clone https://github.com/alloc/nebu
30
+
cd nebu && pnpm i
31
+
cd examples && pnpm i
32
+
./try nebu-strip-dev
33
+
```
34
+
35
+
36
+
37
+
## Usage
38
+
16
39
```js
17
40
constnebu=require('nebu');
18
41
@@ -32,37 +55,38 @@ The `process` function traverses the AST depth-first, which means children are
32
55
visited before neighbors, and parents are visited before children.
33
56
34
57
The `process` function has the following options:
35
-
-`ast: ?object` pre-existing ESTree object
36
-
-`state: ?object` state passed to each visitor
58
+
-`ast?: object` pre-existing ESTree object
59
+
-`state?: object` state passed to each visitor
37
60
-`plugins: object[]` array of visitor maps
38
-
-`filename: ?string` path to the source code
39
-
-`sourceMaps: ?string|true` sourcemap type
40
-
-`generatedFile: ?string` path to the generated code
41
-
-`includeContent: ?boolean` include source content in sourcemap
42
-
-`parser: ?object` options for the parser
61
+
-`filename?: string` path to the source code
62
+
-`sourceMap?: boolean | "inline"` sourcemap type
63
+
-`sourceMapTarget?: string` sourcemap path (relative to `filename`)
64
+
-`generatedFile?: string` path to the generated code
65
+
-`includeContent?: boolean` include source content in sourcemap
66
+
-`jsx?: boolean` enable JSX parsing
43
67
44
-
The `plugins` array is required, and must contain at least one plugin. It may contain nested arrays of plugins.
68
+
The `plugins` array is required. Plugins are objects whose keys are ESTree node types and each value is a function that receives the node and shared state. The `plugins` array supports a plugin being wrapped in `{default: plugin}` for ESM interop.
45
69
46
70
The `state` object is useful when a plugin analyzes the structure of your code and needs to communicate this information back to you. Another use case is inter-visitor communication.
47
71
48
-
The `sourceMaps` option defaults to falsy, which means no sourcemap is generated. Setting `sourceMaps` to `true`or `"both"`will generate a `SourceMap` object and return it as the `map` property of the result object. Setting `sourceMaps` to `"inline"`or `"both"`will append a `//# sourceMappingURL` comment to the generated code. When `sourceMaps` equals `"inline"` or falsy, the `process` function returns a string (the generated code) instead of an object.
72
+
The `sourceMap` option defaults to false, so no sourcemap is generated. Setting `sourceMap` to `true` will generate a `SourceMap` object and return it as the `map` property of the result object. Setting `sourceMap` to `"inline"` will append a `//# sourceMappingURL` comment to the generated code.
49
73
50
-
The `includeContent` option defaults to true, which means you must explicitly specify `false` to exclude source content from the sourcemap.
74
+
The `includeContent` option defaults to true. You must explicitly specify `false` to exclude source content from the sourcemap.
51
75
52
-
The `parser` options object is passed to `acorn.parse`, whose valid options are listed [here](https://github.com/acornjs/acorn#main-parser). The `ecmaVersion` option is always set to `9` (to stay compatible with Bublé). The `sourceType` option is always set to `"module"`.
76
+
### Utilities
53
77
54
-
### nebu.acorn
78
+
The `nebu/utils` module exports a few utility functions you may find useful when developing a plugin.
55
79
56
-
To override the `acorn` module that nebu uses, you can set `nebu.acorn` before calling `nebu.process` for the first time.
80
+
```js
81
+
import { findParent } from'nebu/utils'
82
+
```
57
83
58
84
## Node API
59
85
60
86
Every node (except the root node) has these properties:
61
87
-`parent: Node` the nearest container node
62
88
-`ref: string` the parent property that contains us
63
89
64
-
The `acorn.Node` prototype is temporarily extended with the following methods.
65
-
66
90
NOTE: Methods that take a `code` argument do *not* validate it for syntax errors. So be careful!
0 commit comments