Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c243e35
WIP: pull out core
eight04 May 21, 2018
bea1b3c
WIP: update dependencies
eight04 May 21, 2018
4b93eaa
WIP: pull out inline-js-core
eight04 May 21, 2018
88068fa
WIP: test
eight04 May 21, 2018
658aebd
Fix: cli API
eight04 May 21, 2018
52fc1f1
Update dependencies
eight04 May 21, 2018
259cabd
Fix: cmd resource is slow
eight04 May 22, 2018
3cadd75
Add: async config locater
eight04 May 22, 2018
3f36a15
Test: config cache
eight04 May 22, 2018
0af0a5a
Test: cached findConfig result
eight04 May 22, 2018
594826c
Test: resolve
eight04 May 22, 2018
7e92814
Test: unknown markdown
eight04 May 22, 2018
f5fd7c0
Test: dataurl no extension
eight04 May 22, 2018
919d5d7
Test: trim
eight04 May 22, 2018
11f73c0
Add: mocha-context
eight04 May 22, 2018
e25882f
Fix: unescape backtick in docstring transform
eight04 May 23, 2018
e948dba
Test: stringify
eight04 May 23, 2018
5cae599
Test: string transform
eight04 May 23, 2018
1f69322
Fix: charset shouldn't be used as the buffer encoding
eight04 May 23, 2018
3d8f9b2
Update dependency
eight04 May 23, 2018
437000f
Update inline-js-core
eight04 May 23, 2018
cd42a48
Add: indent transform
eight04 May 23, 2018
0ba4b89
fixup! Add: indent transform
eight04 May 23, 2018
ee05727
Update inline-js-core
eight04 May 23, 2018
2972f44
Add: functional test
eight04 May 23, 2018
43f25a1
Fix: config is not loaded
eight04 May 23, 2018
bec6341
Test: make funtional test nest deeper
eight04 May 23, 2018
eaca18e
Fix: use getLineRange and getWhitespace
eight04 May 23, 2018
36fa347
Update core
eight04 May 23, 2018
a059702
Test: output
eight04 May 23, 2018
d0686a1
Test: empty config/no config
eight04 May 23, 2018
3c2218a
Fix: print welcome message at start
eight04 May 23, 2018
763a595
Test: use DI to test io
eight04 May 23, 2018
4c71033
Fix: outputFileSync -> outputFile
eight04 May 23, 2018
0a1c0b7
Fix: sinon is dev-dependency
eight04 May 23, 2018
d92a5d1
Fix: badge branch
eight04 May 23, 2018
c769048
Update changelog
eight04 May 23, 2018
087e7e1
Update readme
eight04 May 23, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"node": true
},
"rules": {
"no-use-before-define": ["error", "nofunc"],
"semi": ["error", "always"]
"max-statements-per-line": 2,
"semi": [2, "always"]
},
"extends": [
"eslint:recommended"
Expand Down
130 changes: 97 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
inline-js
=========

[![Build Status](https://travis-ci.org/eight04/inline-js.svg?branch=dev-async)](https://travis-ci.org/eight04/inline-js)
[![Coverage Status](https://coveralls.io/repos/github/eight04/inline-js/badge.svg?branch=dev-async)](https://coveralls.io/github/eight04/inline-js?branch=dev-async)
[![Build Status](https://travis-ci.org/eight04/inline-js.svg?branch=master)](https://travis-ci.org/eight04/inline-js)
[![Coverage Status](https://coveralls.io/repos/github/eight04/inline-js/badge.svg?branch=master)](https://coveralls.io/github/eight04/inline-js?branch=master)
[![install size](https://packagephobia.now.sh/badge?p=inline-js)](https://packagephobia.now.sh/result?p=inline-js)

A static assets inliner, like PHP's `include`, with transformer!

Expand All @@ -16,11 +17,13 @@ Quick start
-----------
You have two files, `a.txt` and `b.txt`.
<!-- $inline.skipStart("toUsage") -->
*a.txt*
```js
// a.txt
$inline("./b.txt");
```

// b.txt
*b.txt*
```
Hello world!
```
Run inline-js:
Expand Down Expand Up @@ -54,15 +57,29 @@ An $inline directive is composed by:
```js
const a = "the content of the resource";
```

If you want to expand the replace range, pass offsets to the function:

```js
const a = /* $inline(resource, 3, 3) */;
```
Which would be converted to:
```js
const a = the content of the resource;
```

* `$inline.line(resource)`: Entire line will be replaced.
* `$inline.line(resource)`: The entire line, excluding indent, will be replaced.

```js
/* $inline.line(resource) */
function test() {
/* $inline.line(resource) */
}
```
Which would be converted to:
```js
the content of the resource
function test() {
the content of the resource
}
```

### Replace the text wrapped by a pair of directives
Expand Down Expand Up @@ -94,18 +111,20 @@ An $inline directive is composed by:

### Define shortcuts

Shortcut is composed by a name and a expanding pattern. You can use `$1`, `$2`, ...`$9`, or `$&` to referece the parameters.
A shortcut is composed by a name and an expand pattern. You can use `$1`, `$2`, ...`$9`, or `$&` to referece the parameters.

```js
// $inline.shortcut("pkg", "../package.json|parse:$1")
var version = $inline("pkg:version"),
author = $inline("pkg:author");
// $inline.shortcut("pkg", "../package.json|parse:$&")
const version = $inline("pkg:version");
const author = $inline("pkg:author");
const other = $inline("pkg:other,property");
```
Which would be processed as:
```js
// $inline.shortcut("pkg", "../package.json|parse:$1")
var version = $inline("../package.json|parse:version"),
author = $inline("../package.json|parse:author");
// $inline.shortcut("pkg", "../package.json|parse:$&")
const version = $inline("../package.json|parse:version");
const author = $inline("../package.json|parse:author");
const other = $inline("../package.json|parse:other,property");
```

### Ignore `$inline` directives
Expand All @@ -129,10 +148,12 @@ $inline('path/to/file') // won't be processed
$inline.skipEnd("skipThisSection")
```

If `$inline.skipEnd` isn't presented, it would ignore the entire file.

Resource
--------

Resource is a JavaScript string so some characters (`'`, `"`) needs to be escaped. It uses [pipe expression](https://www.npmjs.com/package/haye#pipe-expression). If written in regular expression:
Resource is a JavaScript string so some characters (`'`, `"`) needs to be escaped. It uses pipe expression. If written in regular expression:

```
(resourceType:)? resourceParam (| transform (: param (,param)* )? )*
Expand All @@ -152,11 +173,11 @@ $inline("path/to/file|transform1:param1,param2|transform2")
Different resource type
-----------------------

inline-js can read content from different resources, which result in different type of the content (`string` or `Buffer`). The type of the content may also affect how transformers work (e.g. `dataurl` transformer).
inline-js can read content from different resources, which results in different types of the content (`string` or `Buffer`). The type of the content may also affect how transformers work (e.g. `dataurl` transformer).

* `file`: Default type. It reads the content from a file path, which may be relative to the file which requires the resource.

The result could be a utf8 string or a `Buffer`, depending on the extension of the file. (See [is-binary-path](https://www.npmjs.com/package/is-binary-path))
The result could be a utf8 string or a `Buffer`, depending on the extension of the file (see [is-binary-path](https://www.npmjs.com/package/is-binary-path)).

* `text`: Like `file`, but the result is always a utf8 string.
* `raw`: Like `file`, but the result is always a `Buffer`.
Expand All @@ -170,31 +191,35 @@ File-like resources would be cached after loaded, so inlining the same file with

Command resources are also cached, but it depends on cwd. For example:

* The command `cat myfile` is executed once, with `cwd = "."`.
* In this example, the command `cat myfile` is executed once, with `cwd = "."`.

*entry.txt*
```js
// entry.txt
$inline("a.txt")
$inline("b.txt")

// a.txt
```
*a.txt*
```js
$inline("cmd:cat myfile")

// b.txt
```
*b.txt*
```js
$inline("cmd:cat myfile")
```

* The command is executed twice. The first with `cwd = "."` and the second with `cwd = "./dir"`.
* In this example, the command is executed twice. The first with `cwd = "."` and the second with `cwd = "./dir"`.

*entry.txt*
```js
// entry.txt
$inline("a.txt")
$inline("dir/b.txt")

// a.txt
```
*a.txt*
```js
$inline("cmd:cat myfile")

// dir/b.txt
```
*dir/b.txt*
```js
$inline("cmd:cat myfile")
```

Expand Down Expand Up @@ -239,20 +264,42 @@ Or you can pass the mimetype manually:
```js
$inline("somefile.txt|dataurl:text/css")
```
Specify charset (default to `utf8` for text file):
Specify charset (default to `utf8` for text files):
```js
$inline("somefile.txt|dataurl:text/css,utf8")
```

### docstring
Extract docstring (i.e. the first template literal) from the content.
Extract docstring (i.e. the top-most template literal) from the content.

### eval
Eval JavaScript expression. You can access the content with `$0`.
Evaluate JavaScript expression. You can access the content with `$0`.
```js
var version = $inline("./package.json|eval:JSON.parse($0).version|stringify");
```

### indent
Indent the string according to the indent of the current line.

*entry.js*
```js
function test() {
$inline("foo.js|indent");
}
```
*foo.js*
```js
console.log("foo");
console.log("bar");
```
`inlinejs entry.js` result:
```js
function test() {
console.log("foo");
console.log("bar");
}
```

### markdown
Wrap content with markdown codeblock, code, or quote.
````js
Expand All @@ -272,7 +319,7 @@ some text
````

### parse
`JSON.parse` the content. You can access property by specify property name.
`JSON.parse` the content. You can access properties by specifying key name.
```js
var version = $inline("./package.json|parse:version"),
nestedProp = $inline("./package.json|parse:nested,prop");
Expand Down Expand Up @@ -330,6 +377,23 @@ module.exports = {
Changelog
---------

* 0.7.0 (May 23, 2018)

- The core inliner logic had been splitted out as [inline-js-core](https://github.com/eight04/inline-js-core). This repository now only contains:

- Config locator.
- Builtin resource loader.
- Builtin transformers.

- More tests.
- Add: `indent` transformer. It would indent inlined file according to the indent of the current line.
- Add: config locator has a cache now.
- Add: `$inline()` now accepts up to 3 arguments.
- Fix: Escaped characters are correctly handled in `docstring` transformer.
- **Change: In `dataurl` transformer, `charset` is set to `utf8` if the content is a string. It makes sense since we actually always use `utf8` encoding to convert string to Buffer.**
- **Change: The first argument of `transform()` function is changed to a `transformContext` object. To access the resource, visit `transformContext.inlineTarget`.**
- **Change: `$inline.line()` now preserves indent. It doesn't replace the entire line anymore.**

* 0.6.1 (Mar 16, 2018)

- Fix: throw when cmd resource return non-zero exit code.
Expand Down
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Options:
laxPlacement: true
});

require("./index").init({args})
require("./index").init(args)
.catch(err => {
console.error(err); // eslint-disable-line no-console
process.exit(1);
Expand Down
Loading