UNPKG

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