UNPKG

4.96 kBMarkdownView Raw
1inline-js
2=========
3
4A static assets inliner, like PHP's `include`, with transformer!
5
6Installation
7------------
8```
9npm install -g inline-js
10```
11
12Quick start
13-----------
14You have two files, `a.txt` and `b.txt`.
15<!-- $inline.skipStart -->
16```
17// a.txt
18$inline("./b.txt");
19
20// b.txt
21Hello world!
22```
23Run inline-js:
24```
25inlinejs a.txt
26```
27Result:
28```
29Hello world!
30```
31
32Syntax
33------
34### Inline a file
35The `$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
48Entire 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
53The lines between .start and .end will be replaced.
54```
55// $inline.start("path/to/file") This line preserve
56These lines
57will
58be
59replaced
60// $inline.end but not this line
61```
62### Use .skipStart and .skipEnd
63Skip 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
70The 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
75Use .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")
78var version = $inline("pkg:version"),
79 author = $inline("pkg:author");
80```
81
82CLI
83----
84<!-- $inline.skipEnd -->
85<!-- $inline.start("./cli.js|docstring|markdown:codeblock") -->
86```
87inlinejs
88
89Usage:
90 inlinejs [options] <entry_file>
91
92Options:
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<!-- $inline.end -->
100<!-- $inline.skipStart -->
101
102Builtin transformers
103--------------------
104
105### cssmin
106Minify css content.
107
108### dataurl
109Convert the content into dataurl.
110
111The transformer would determine the mimetype from filename:
112```
113$inline("mystyle.css|dataurl")
114->
115data:text/css;charset=utf8;base64,...
116```
117Or you can pass the mimetype manually:
118```
119$inline("somefile.txt|dataurl:text/css")
120```
121Specify charset (default to `utf8`):
122```
123$inline("somefile.txt|dataurl:text/css,utf8")
124```
125
126### docstring
127Extract docstring (i.e. the first template literal) from the js file.
128
129### eval
130Eval JavaScript expression. You can access the content with `$0`.
131```
132var version = $inline("./package.json|eval:JSON.parse($0).version|stringify");
133```
134
135### markdown
136Wrap content with markdown codeblock, code, or quote.
137<pre><code>// a.txt
138some text
139
140// $inline("a.txt|markdown:codeblock")
141```
142some 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```
154var 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```
161var myCssString = $inline("./style.css|cssmin|stringify");
162```
163
164### trim
165`String.trim` the content.
166
167Use `.inline.js`
168----------------
169You can create your transformer and shortcut with this file.
170
171Create a `.inline.js` file in your package root:
172```
173module.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
193Changelog
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.