Skip to content

Commit e21cf86

Browse files
committed
update
1 parent 7302da6 commit e21cf86

File tree

8 files changed

+240
-11
lines changed

8 files changed

+240
-11
lines changed

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
/demo

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,62 @@
11
# image-ascii-loader
22
A webpack loader that load image as ascii text.
33

4+
## Demo
5+
6+
Generate this
7+
8+
![demo](./doc/demo.png)
9+
10+
from
11+
12+
![demo](./demo/demo.jpg)
13+
14+
feel free to clone and play with demo project.
15+
16+
## Guide
17+
18+
```bash
19+
npm i -D image-ascii-loader
20+
```
21+
22+
then edit your `webpack.config.js` .
23+
24+
```javascript
25+
module.exports = {
26+
// ...
27+
module: {
28+
rules: [
29+
{
30+
test: /\.jpe?g$/,
31+
use: ["image-ascii-loader"]
32+
}
33+
]
34+
},
35+
};
36+
```
37+
38+
With this config, all your jpeg file will be tranform to string, usually this is not what you want. You can skip configuration and do this instead:
39+
40+
```javascript
41+
const ascii = require('image-ascii-loader!./demo.jpg');
42+
43+
document.getElementById('demo').innerHTML = ascii;
44+
```
45+
46+
You can specify options use resourceQuery:
47+
48+
```javascript
49+
// 100 characters per line
50+
const ascii = require('./demo.jpg?width=100');
51+
52+
// use other characters to draw the image
53+
const ascii = require('./demo.jpg?alphabet=blocks');
54+
```
55+
56+
check [ascii-art-image](https://www.npmjs.com/package/ascii-art-image) for more options.
57+
58+
## More
59+
60+
![I like this one](./doc/demo1.png)
61+
62+
Image source: readme of https://github.com/khrome/ascii-art project

demo/bundle.js

Lines changed: 125 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "playwebpackloader",
2+
"name": "demo",
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
@@ -13,9 +13,7 @@
1313
"license": "MIT",
1414
"dependencies": {
1515
"webpack": "^4.41.5",
16-
"webpack-cli": "^3.3.10"
17-
},
18-
"devDependencies": {
16+
"webpack-cli": "^3.3.10",
1917
"webpack-dev-server": "^3.10.1"
2018
}
2119
}

doc/demo.png

65.9 KB
Loading

doc/demo1.png

92.3 KB
Loading

lib/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
3+
var _require = require('loader-utils'),
4+
parseQuery = _require.parseQuery;
5+
6+
var AsciiArtImage = require('ascii-art-image');
7+
8+
var stripAnsi = require('strip-ansi');
9+
10+
module.exports.pitch = function () {
11+
var callback = this.async();
12+
var image = new AsciiArtImage(getOptions.call(this));
13+
image.write(function (err, ascii) {
14+
if (err) {
15+
callback(err);
16+
} else {
17+
callback(null, processAscii(ascii));
18+
}
19+
});
20+
};
21+
22+
function getOptions() {
23+
var options = {
24+
width: 80,
25+
alphabet: 'hatching'
26+
};
27+
28+
if (this.resourceQuery) {
29+
var queryOptions = parseQuery(this.resourceQuery);
30+
31+
if (queryOptions.width) {
32+
queryOptions.width = parseInt(queryOptions.width);
33+
}
34+
35+
Object.assign(options, queryOptions);
36+
}
37+
38+
options.filepath = this.resourcePath;
39+
return options;
40+
}
41+
42+
function processAscii(ascii) {
43+
var html = stripAnsi(ascii).replace(/\n/g, '<br/>').replace(/ /g, '&nbsp;');
44+
return "export default `".concat(html, "`");
45+
}

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"name": "img-ascii-loader",
3-
"version": "1.0.0",
4-
"description": "",
2+
"name": "image-ascii-loader",
3+
"version": "1.0.3",
4+
"description": "A webpack loader that load image as ascii text.",
55
"main": "lib/index.js",
66
"scripts": {
7-
"build": "babel src -d lib",
7+
"build": "babel index.js -d lib",
88
"test": "echo \"Error: no test specified\" && exit 1"
99
},
10-
"keywords": [],
11-
"author": "",
12-
"license": "ISC",
10+
"keywords": ["webpack", "loader", "webpack-loader", "ascii", "image", "jpeg"],
11+
"author": "[email protected]",
12+
"license": "MIT",
1313
"dependencies": {
1414
"ascii-art-ansi": "^1.3.3",
1515
"ascii-art-image": "^1.2.2",

0 commit comments

Comments
 (0)