1 | inline-js
|
2 | =========
|
3 |
|
4 | A static assets inliner, like PHP's `include`, with transformer!
|
5 |
|
6 | Installation
|
7 | ------------
|
8 | ```
|
9 | npm install -g inline-js
|
10 | ```
|
11 |
|
12 | Quick start
|
13 | -----------
|
14 | You have two files, `a.txt` and `b.txt`.
|
15 |
|
16 | ```
|
17 | // a.txt
|
18 | $inline("./b.txt");
|
19 |
|
20 | // b.txt
|
21 | Hello world!
|
22 | ```
|
23 | Run inline-js:
|
24 | ```
|
25 | inlinejs a.txt
|
26 | ```
|
27 | Result:
|
28 | ```
|
29 | Hello world!
|
30 | ```
|
31 |
|
32 | Syntax
|
33 | ------
|
34 | ### Inline a file
|
35 | The `$inline()` statement will be replaced with the content of the file.
|
36 | ```
|
37 | $inline("path/to/file")
|
38 | ```
|
39 | ### Use the transformer
|
40 | ```
|
41 | $inline("path/to/file|transform1|transform2")
|
42 | ```
|
43 | ### Pass arguments to the transformer
|
44 | ```
|
45 | $inline("path/to/file|transform1:arg|transform2:arg1,arg2")
|
46 | ```
|
47 | ### Replace current line
|
48 | Entire line will be replaced.
|
49 | ```
|
50 | // this line will be replaced $inline.line("path/to/file") with the content of the file
|
51 | ```
|
52 | ### Use .start and .end
|
53 | The lines between .start and .end will be replaced.
|
54 | ```
|
55 | // $inline.start("path/to/file") This line preserve
|
56 | These lines
|
57 | will
|
58 | be
|
59 | replaced
|
60 | // $inline.end but not this line
|
61 | ```
|
62 | ### Use .skipStart and .skipEnd
|
63 | Skip the content beween .skipStart and .skipEnd.
|
64 | ```
|
65 | // $inline.skipStart
|
66 | $inline('path/to/file') this line won't be inlined
|
67 | // $inline.skipeEnd
|
68 | ```
|
69 | ### Use .open and .close
|
70 | The content between .open and .close will be replaced. The additional argument is how many characters to skip.
|
71 | ```
|
72 | <!--$inline.open("path/to/file", 3)-->Replace me<!--$inline.close(4)-->
|
73 | ```
|
74 | ### Use .shortcut
|
75 | Use .shortcut to deal with repeated patterns. Shortcut is composed by a name and a expanding pattern. You can use $1...$9 to referece the params.
|
76 | ```
|
77 | // $inline.shortcut("pkg", "../package.json|parse:$1")
|
78 | var version = $inline("pkg:version"),
|
79 | author = $inline("pkg:author");
|
80 | ```
|
81 |
|
82 | CLI
|
83 | ----
|
84 |
|
85 |
|
86 | ```
|
87 | inlinejs
|
88 |
|
89 | Usage:
|
90 | inlinejs [options] <entry_file>
|
91 |
|
92 | Options:
|
93 | -o --out FILE Output file. Print to stdout if omitted.
|
94 | -d --max-depth COUNT Max depth of the dependency tree. [default: 10]
|
95 | -n --dry-run Print the file name instead of writing to disk.
|
96 | -h --help Show this.
|
97 | -v --version Show version.
|
98 | ```
|
99 |
|
100 |
|
101 |
|
102 | Builtin transformers
|
103 | --------------------
|
104 |
|
105 | ### cssmin
|
106 | Minify css content.
|
107 |
|
108 | ### dataurl
|
109 | Convert the content into dataurl.
|
110 |
|
111 | The transformer would determine the mimetype from filename:
|
112 | ```
|
113 | $inline("mystyle.css|dataurl")
|
114 | ->
|
115 | data:text/css;charset=utf8;base64,...
|
116 | ```
|
117 | Or you can pass the mimetype manually:
|
118 | ```
|
119 | $inline("somefile.txt|dataurl:text/css")
|
120 | ```
|
121 | Specify charset (default to `utf8`):
|
122 | ```
|
123 | $inline("somefile.txt|dataurl:text/css,utf8")
|
124 | ```
|
125 |
|
126 | ### docstring
|
127 | Extract docstring (i.e. the first template literal) from the js file.
|
128 |
|
129 | ### eval
|
130 | Eval JavaScript expression. You can access the content with `$0`.
|
131 | ```
|
132 | var version = $inline("./package.json|eval:JSON.parse($0).version|stringify");
|
133 | ```
|
134 |
|
135 | ### markdown
|
136 | Wrap content with markdown codeblock, code, or quote.
|
137 | <pre><code>// a.txt
|
138 | some text
|
139 |
|
140 | // $inline("a.txt|markdown:codeblock")
|
141 | ```
|
142 | some text
|
143 | ```
|
144 |
|
145 | // $inline("a.txt|markdown:code")
|
146 | `some text`
|
147 |
|
148 | // $inline("a.txt|markdown:quote")
|
149 | > sometext</code></pre>
|
150 |
|
151 | ### parse
|
152 | `JSON.parse` the content. You can access property by specify property name.
|
153 | ```
|
154 | var version = $inline("./package.json|parse:version"),
|
155 | nestedProp = $inline("./package.json|parse:nested,prop");
|
156 | ```
|
157 |
|
158 | ### stringify
|
159 | `JSON.stringify` the content. Useful to include text content into .js:
|
160 | ```
|
161 | var myCssString = $inline("./style.css|cssmin|stringify");
|
162 | ```
|
163 |
|
164 | ### trim
|
165 | `String.trim` the content.
|
166 |
|
167 | Use `.inline.js`
|
168 | ----------------
|
169 | You can create your transformer and shortcut with this file.
|
170 |
|
171 | Create a `.inline.js` file in your package root:
|
172 | ```
|
173 | module.exports = {
|
174 | shortcuts: [{
|
175 | name: "myshortcut",
|
176 | expand: "pattern-to-expand",
|
177 | // or use a function
|
178 | expand: function (file, arg1, arg2, ...args) {
|
179 | // create expand pattern
|
180 | return pattern;
|
181 | }
|
182 | }, ...],
|
183 | transforms: [{
|
184 | name: "mytransform",
|
185 | transform: function (content, arg1, arg2, ...args) {
|
186 | // do something to the content
|
187 | return content;
|
188 | }
|
189 | }, ...]
|
190 | };
|
191 | ```
|
192 |
|
193 | Changelog
|
194 | ---------
|
195 |
|
196 | * 0.4.0 (Sep 22, 2017)
|
197 |
|
198 | - Fix: dataurl is unable to handle binary file.
|
199 | - **Change: now transformer would recieve a `file` argument.**
|
200 | - Add: make `dataurl` determine mimetype by filename.
|
201 |
|
202 | * 0.3.1 (Sep 19, 2017)
|
203 |
|
204 | - Fix crlf error. [#3](https://github.com/eight04/inline-js/issues/3)
|
205 |
|
206 | * 0.3.0 (Feb 4, 2017)
|
207 |
|
208 | - Add $inline.shortcut.
|
209 |
|
210 | * 0.2.0 (Jan 21, 2017)
|
211 |
|
212 | - Add $inline.open, close, skipStart, skipEnd, start, end, line.
|
213 | - Add transformer docstring, markdown, parse.
|
214 | - Change eval transformer.
|
215 | - Improve logging.
|
216 | - Add --max-depth option.
|
217 | - Other bugfixes.
|
218 |
|
219 | * 0.1.0 (Jan 21, 2017)
|
220 |
|
221 | - First release.
|