1 | # static-module
|
2 |
|
3 | convert module usage to inline expressions
|
4 |
|
5 | # example
|
6 |
|
7 | Here's a simplified version of the [brfs](https://npmjs.org/package/brfs) module
|
8 | using static-module.
|
9 |
|
10 | brfs converts `fs.readFileSync(file)` calls to inline strings with the contents
|
11 | of `file` included in-place.
|
12 |
|
13 | ``` js
|
14 | var staticModule = require('static-module');
|
15 | var quote = require('quote-stream');
|
16 | var fs = require('fs');
|
17 |
|
18 | var sm = staticModule({
|
19 | fs: {
|
20 | readFileSync: function (file) {
|
21 | return fs.createReadStream(file).pipe(quote());
|
22 | }
|
23 | }
|
24 | }, { vars: { __dirname: __dirname + '/brfs' } });
|
25 | process.stdin.pipe(sm).pipe(process.stdout);
|
26 | ```
|
27 |
|
28 | input:
|
29 |
|
30 | ```
|
31 | $ cat brfs/source.js
|
32 | var fs = require('fs');
|
33 | var src = fs.readFileSync(__dirname + '/x.txt');
|
34 | console.log(src);
|
35 | ```
|
36 |
|
37 | output:
|
38 |
|
39 | ```
|
40 | $ node brfs.js < brfs/source.js
|
41 |
|
42 | var src = "beep boop\n";
|
43 | console.log(src);
|
44 | ```
|
45 |
|
46 | # methods
|
47 |
|
48 | ``` js
|
49 | var staticModule = require('static-module')
|
50 | ```
|
51 |
|
52 | ## var sm = staticModule(modules, opts={})
|
53 |
|
54 | Return a transform stream `sm` that transforms javascript source input to
|
55 | javascript source output with each property in the `modules` object expanded in
|
56 | inline form.
|
57 |
|
58 | Properties in the `modules` object can be ordinary values that will be included
|
59 | directly or functions that will be executed with the [statically
|
60 | evaluated](https://npmjs.org/package/static-eval) arguments from the source
|
61 | under an optional set of `opts.vars` variables.
|
62 |
|
63 | Property functions can return streams, in which case their contents will be
|
64 | piped directly into the source output.
|
65 |
|
66 | Otherwise, the return values of functions will be inlined into the source in
|
67 | place as strings.
|
68 |
|
69 | Use `opts.varModules` to map whitelisted module names to definitions that can be
|
70 | declared in client code with `var` and will appear in static expressions like
|
71 | `opts.vars`.
|
72 |
|
73 | For example, to make this code with `path.join()` work:
|
74 |
|
75 | ``` js
|
76 | var fs = require('fs');
|
77 | var path = require('path');
|
78 | var src = fs.readFileSync(path.join(__dirname, 'x.txt'), 'utf8');
|
79 | console.log(src);
|
80 | ```
|
81 |
|
82 | you can do:
|
83 |
|
84 | ``` js
|
85 | var staticModule = require('static-module');
|
86 | var quote = require('quote-stream');
|
87 | var fs = require('fs');
|
88 |
|
89 | var sm = staticModule({
|
90 | fs: {
|
91 | readFileSync: function (file) {
|
92 | return fs.createReadStream(file).pipe(quote());
|
93 | }
|
94 | },
|
95 | varMods: { path: require('path') }
|
96 | }, { vars: { __dirname: __dirname + '/brfs' } });
|
97 | process.stdin.pipe(sm).pipe(process.stdout);
|
98 | ```
|
99 |
|
100 | Use `opts.parserOpts` to set additional options for the
|
101 | [acorn](https://github.com/acornjs/acorn) parser.
|
102 |
|
103 | Set `opts.sourceMap` to `true` to generate a source map and add it as an inline
|
104 | comment. You can add `opts.inputFilename` to configure the original file name
|
105 | that will be listed in the source map.
|
106 |
|
107 | # install
|
108 |
|
109 | With [npm](https://npmjs.org) do:
|
110 |
|
111 | ```
|
112 | npm install static-module
|
113 | ```
|
114 |
|
115 | # license
|
116 |
|
117 | MIT
|