Skip to content

Commit 7bac09f

Browse files
committed
chore(repository): initial commit
0 parents  commit 7bac09f

File tree

12 files changed

+187
-0
lines changed

12 files changed

+187
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
npm-debug.log*

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- 4
4+
- 5
5+
- 6

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2016 Pine Mizune
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
ArrayBuffer loader for webpack
2+
------------------------------
3+
4+
## Getting Started
5+
6+
```
7+
$ npm install arraybuffer-loader --save-dev
8+
```
9+
10+
## Usage
11+
12+
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
13+
14+
```
15+
var buffer = require('arraybuffer!./data.dat')
16+
var array = new Uint8Array(buffer)
17+
18+
// Enjoy!!
19+
```
20+
21+
## License
22+
MIT

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict'
2+
module.exports = require('./lib/arraybuffer-loader')

karma.conf.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
5+
module.exports = function(config) {
6+
config.set({
7+
basePath: '',
8+
files: [
9+
'test/**/*.js',
10+
],
11+
exclude: [],
12+
preprocessors: {
13+
'test/**/*.js': ['webpack', 'sourcemap'],
14+
},
15+
16+
frameworks: ['mocha', 'chai'],
17+
reporters: ['mocha'],
18+
19+
port: 9876,
20+
colors: true,
21+
logLevel: config.LOG_INFO,
22+
autoWatch: true,
23+
browsers: ['PhantomJS2'],
24+
singleRun: true,
25+
26+
webpack: {
27+
devtool: 'inline-source-map',
28+
module: {
29+
loaders: [],
30+
},
31+
resolveLoader: {
32+
alias: {
33+
'arraybuffer': path.join(__dirname, './'),
34+
},
35+
},
36+
resolve: {
37+
extensions: ['', '.js', '.json']
38+
},
39+
},
40+
webpackMiddleware: {
41+
noInfo: true,
42+
},
43+
})
44+
}

lib/arraybuffer-loader.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const loaderUtils = require('loader-utils')
4+
5+
module.exports = function (content) {
6+
if (this.cacheable) { this.cacheable() }
7+
8+
const toArrayBufferPath =
9+
loaderUtils.stringifyRequest(this, require.resolve('./to-array-buffer.js'))
10+
11+
const base64Data = typeof content === 'string' ?
12+
new Buffer(content).toString('base64') : content.toString('base64')
13+
14+
return `module.exports = require(${toArrayBufferPath})("${base64Data}")`;
15+
}

lib/to-array-buffer.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
// * To be run on any browser
4+
module.exports = function (base64Data) {
5+
var binary = window.atob(base64Data)
6+
var bytes = new Uint8Array(binary.length)
7+
8+
for (var i = 0; i < binary.length; ++i) {
9+
bytes[i] = binary.charCodeAt(i)
10+
}
11+
12+
return bytes.buffer
13+
}

package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "arraybuffer-loader",
3+
"version": "0.1.0",
4+
"description": "Webpack loader that returns file contents as a ArrayBuffer",
5+
"scripts": {
6+
"test": "karma start"
7+
},
8+
"main": "index.js",
9+
"files": [
10+
"lib",
11+
"index.js",
12+
"*.md"
13+
],
14+
"engines": {
15+
"node": ">= 4.0.0"
16+
},
17+
"dependencies": {
18+
"loader-utils": "^0.2.15"
19+
},
20+
"devDependencies": {
21+
"chai": "^3.5.0",
22+
"karma": "^1.1.2",
23+
"karma-chai": "^0.1.0",
24+
"karma-mocha": "^1.1.1",
25+
"karma-mocha-reporter": "^2.0.4",
26+
"karma-phantomjs2-launcher": "^0.5.0",
27+
"karma-sourcemap-loader": "^0.3.7",
28+
"karma-webpack": "^1.7.0",
29+
"mocha": "^2.5.3",
30+
"text-encoding": "^0.6.0",
31+
"webpack": "^1.13.1"
32+
},
33+
"author": {
34+
"name": "Pine Mizune",
35+
"email": "pinemz@gmail.com",
36+
"url": "https://github.com/pine"
37+
},
38+
"license": "MIT"
39+
}

test/arraybuffer-loader.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
var TextDecoder = require('text-encoding').TextDecoder
4+
5+
describe('text files', function () {
6+
it('can bundle ascii text file', function () {
7+
var buffer = require('arraybuffer!./data/ascii.txt')
8+
var array = new Uint8Array(buffer)
9+
var text = new TextDecoder('utf-8').decode(array)
10+
11+
expect(text).to.equal('Hello, World!!\n')
12+
})
13+
14+
it('can bundle multi byte test file', function () {
15+
var buffer = require('arraybuffer!./data/multi-byte.txt')
16+
var array = new Uint8Array(buffer)
17+
var text = new TextDecoder('utf-8').decode(array)
18+
19+
expect(text).to.equal('\uD83D\uDE07\n') // :innocent:
20+
})
21+
})

test/data/ascii.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, World!!

test/data/multi-byte.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
😇

0 commit comments

Comments
 (0)