The core part of inline-js. Create an inliner with multiple resource loaders, transformers, and shortcuts.
npm install inline-js-coreinline-js-core
==============



The core part of inline-js. Create an inliner with multiple resource loaders, transformers, and shortcuts.
Installation
------------
``
`
npm install inline-js-core
createInliner
API
----
This module exports following members:
* : Create an inliner.
`
$3
js
`
const inliner = createInliner({
maxDepth?: Number
});
maxDepth
- the max recursion depth. Default: 10.
`
$3
js
`
const inlineResult = await inliner.inline({
target: {
name: String,
args: Array
},
source?: Object,
content?: String|Buffer
});
target
Read the resource from and process $inline directives recursively.
target
is a resource specifier. A simple file could be represented as:
`
js
`
{
name: "file",
args: ["D:\myfile.txt"]
}
source
is also a resource specifier like target. It is only used to resolve relative path to absolute path:
`
js
`
const source = {
name: "file",
args: ["D:\foo.txt"]
};
const target = {
name: "file",
args: ["bar.txt"]
};
inliner.resource.resolve(source, target);
console.log(target);
/*
{
name: "file",
args: ["D:\bar.txt"]
}
*/
inlineResult
has following properties:
content: Buffer|String
* - the output content.
target: Object
* - the target resource that is processed.
children: [...inlineResult: Object]
* - inlineResults of child dependencies (i.e. files being inlined by target).
`
$3
js
`
inliner.useConfig({
resources?: Array,
transforms?: Array,
shortcuts?: Array
});
`
A utility function to add resources, shortcuts, and transforms from a config object. If the argument is falsy, this function has no effect.
$3
js
`
inliner.resource.add({
name: String,
read: async (source: Object, target: Object) => String|Buffer,
resolve?: (source: Object, target: Object) => null,
hash?: (source: Object, target: Object) => String
});
name
Add a resource loader.
is the name of the resource loader.
read
should return the content of target file. source is the parent file that inlines target.
resolve
If is defined, inliner would call this function before read.
hash
should convert a (source, target) pair into a unique string that inliner would use it to cache the content. If hash is undefined then the content is never cached.
`
$3
js
`
inliner.resource.remove(name: String);
name
Remove a resource loader that the name is .
`
$3
js
`
const content = await inliner.resource.read(source: Object, target: Object);
target.name
Find the resource loader matching then call the read function of the resource loader. Inliner would try caching the result after read.
Map
$3
A object containing hash: String/pendingContent: Promise pairs.
`
$3
js
`
inliner.transformer.add({
name: String,
transform: async (context: Object, content: String|Buffer, ...args) => outputContent: String|Buffer
});
name
is the name of the transformer.
transform
function is used to transform content.
context
object provides some additional information about the source resource. It has following properties:
inlineTarget: Object
* - the target resource. The file that is being inlined and transformed.
inlineDirective
* - an object that represents an inline directive. It has following properties:
type: String
- - could be $inline, line, start, or open.
params: Array
- - arguments of the inline function.
start: Number
- - the start index of the replace range.
end: Number
- - the end index of the replace range.
source: Object
* - the source resource. The file containing the inline directive.
sourceContent: String
* - the content of source.
args
Other is specified by the $inline directive. For example:
`
js
`
$inline("myfile|transformA:foo,bar")
args
In this case, would be ["foo", "bar"].
`
$3
js
`
inliner.transformer.remove(name: String)
`
Remove the transformer.
$3
js
`
const outputContent = await inliner.transformer.transform(
context: Object,
content: String|Buffer,
transforms: Array
content
Transform through a list of transforms.
`
$3
js
`
inliner.globalShortcuts.add({
name: String,
expand: String|Function
})
name
Add a global shortcut expander.
is the name of the shortcut.
expand
is a pattern that would expand the original string. For example, with the following expander:
`
js
`
{
name: "foo",
expand: "foobar|t1:$1|t2:$2"
}
foo:a,b
It can expand into foobar|t1:a|t2:b. $n (n=1...9) would be replaced with the parameter at specified index. $& would be replaced with all parameters.
expand
can also be a function:
`
`
expand: (target: Object, ...args) => expandPattern: String
target
is the resource that is being processed.
args
are parameters of the shortcut.
`
$3
js
`
inliner.globalShortcuts.remove(name: String)
`
Remove a global shortcut expander.
$3
js
`
const outputString = inliner.globalShortcuts.expand(target: Object, pipes: [shortcut, ...otherPipes])
shortcut.name
Find the expander matching , expand shortcut, concat the result with otherPipes, then return the final string.
inliner.inline
Changelog
---------
* 0.5.0 (Aug 5, 2018)
- The module requires node 7.6 or higher.
- is changed. Now it accept an object.
inliner.resource.cache
- Add: expose .
Inliner.useConfig
* 0.4.1 (Jul 22, 2018)
- Add: utility function.
from
* 0.4.0 (Jun 23, 2018)
- Add: parameter of Inliner.inline.
InlineResult.target
- Add: , InlineResult.children to get detailed information about inlined files.
InlineResult.dependency
- Drop: .
getLineRange
* 0.3.1 (May 23, 2018)
- Fix: sub-dependency is broken.
* 0.3.0 (May 23, 2018)
- Add: export , getWhitespace utils in ./lib/parser.
$inline
- Add: The signature of is expanded to $inline(resource, startOffset = 0, endOffset = 0). Use startOffset, endOffset to extend replace range.
Transformer.transform
- Change: $inline.line now preserves indents.
* 0.2.0 (May 23, 2018)
- Change: the first argument of is changed to a TransformContext` object. This should help implement "endent" transformer.
* 0.1.1 (May 21, 2018)
- Fix: ignore parseDirective error when it is wrapped with other tags.
* 0.1.0 (May 21, 2018)
- Pull out core from inline-js.