Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rasshofer committed Mar 18, 2018
0 parents commit 130fcdd
Show file tree
Hide file tree
Showing 21 changed files with 7,438 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = spaces
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
2 changes: 2 additions & 0 deletions .eslintignore
@@ -0,0 +1,2 @@
node_modules
coverage
11 changes: 11 additions & 0 deletions .eslintrc.yml
@@ -0,0 +1,11 @@
---
env:
es6: true
node: true
browser: true
extends: airbnb-base
rules:
comma-dangle: 0
max-len: 0
arrow-parens: 0
newline-per-chained-call: 0
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
node_modules
coverage
9 changes: 9 additions & 0 deletions .travis.yml
@@ -0,0 +1,9 @@
sudo: false
language: node_js
node_js:
- 6
- 8
- 9
script:
- npm test
- npm run coverage
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2018 Thomas Rasshofer <hello@thomasrasshofer.com>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
96 changes: 96 additions & 0 deletions README.md
@@ -0,0 +1,96 @@
# Packback

> A simple tool to bundle files (= pack back multiple files into a single file)
[![Build Status](https://travis-ci.org/rasshofer/packback.svg)](https://travis-ci.org/rasshofer/packback)
[![Coverage Status](https://coveralls.io/repos/github/rasshofer/packback/badge.svg)](https://coveralls.io/github/rasshofer/packback)
[![Dependency Status](https://david-dm.org/rasshofer/packback/status.svg)](https://david-dm.org/rasshofer/packback)
[![Dependency Status](https://david-dm.org/rasshofer/packback/dev-status.svg)](https://david-dm.org/rasshofer/packback)

## Usage

```shell
npm install --save-dev packback
```

```js
const packback = require('packback');

const packed = packback.packFile('./src/input.html');

fs.writeFileSync('./build/packed.html', packed);
```

```js
const packback = require('packback');

const packed = packback.pack(`
Lorem ipsum
include test.pug
Lorem ipsum
`, {
file: 'dummy.txt',
context: __dirname
});

fs.writeFileSync('./build/packed.html', packed);
```

## Handlers

Packback enables the use of handlers to process files. You can easily write your own handlers by providing the following options/properties. In addition, you can find pre-built handlers on [npm](https://www.npmjs.com/browse/keyword/packback).

### Options

#### `test`

A callback function that returns whether the handler is appropriate for a fetched file or not.

Example: `(filename) => /\.txt$/.test(filename)`

#### `pattern`

A regular expression to find matches within the file content.

Example: `/include\s*(\()?(.*?)\.txt(\)?)/g`

#### `matcher`

A callback function that is called for each match and returns the real file name for further processing. This may be useful in case the import statement conceals some parts of the real filename (like Node.js does with the optional `.js` file extension for `require` calls).

Example: ``(matches) => `${matches[2]}.txt` ``

#### `decorator` (optional)

A callback function that is called for each match just before inserting the content into the overall bundle. This may be useful to implement further processing of contents (like implementing debugging markers).

Example: ``(content, file) => [`<!-- START ${path.basename(file)} -->`, content.trim(), `<!-- END ${path.basename(file)} -->`].join('\n')``

### Example

```js
const packback = require('packback');

packback.use({
test: (filename) => /\.html/.test(filename),
pattern: /<!--#include file="(.*?)\.html" -->/g,
matcher: (matches) => `${matches[1]}.html`,
decorator: (content, file) => [`<!-- START ${path.basename(file)} -->`, content.trim(), `<!-- END ${path.basename(file)} -->`].join('\n')
});

const packed = packback.packFile('./src/input.html');

fs.writeFileSync('./build/packed.html', packed);
```

## Changelog

* 2.0.0
* Initial version

## License

Copyright (c) 2018 [Thomas Rasshofer](http://thomasrasshofer.com/)
Licensed under the MIT license.

See LICENSE for more info.

0 comments on commit 130fcdd

Please sign in to comment.