Skip to content

Commit 5a1ebab

Browse files
author
Jeff Escalante
committed
Merge pull request #3 from static-dev/binary
Better binary file handling
2 parents 500ad41 + 0dd488b commit 5a1ebab

File tree

6 files changed

+39
-4
lines changed

6 files changed

+39
-4
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Webpack loader that exports the source directly
88
99
### Why should you care?
1010

11-
If you need to load up a file's source directly and have it available within your javascript, this loader is what you're after. For example, you could load up a svg file, parse out a path you want, and manipulate that with your javascript somehow. Or you could load up a json or text file. Or some other type of file that you came up with yourself. Whatever it is, as long as it's contents would be ok after being run through `JSON.stringify` (read: text files, not images or binary files), it will work just great with the source loader.
11+
If you need to load up a file's source directly and have it available within your javascript, this loader is what you're after. For example, you could load up a svg file, parse out a path you want, and manipulate that with your javascript somehow. Or you could load up a json or text file. Or some other type of file that you came up with yourself. Whatever it is, as long as it's contents would be ok after being run through `JSON.stringify` (read: text files, not images or binary files), it will work just great with the source loader. And if it is a binary file, it will work great as well, but you won't be able to require it in your client-side js, just manipulate it through a plugin.
1212

1313
### Installation
1414

@@ -39,7 +39,7 @@ As an added bonus, this loader makes the buffered raw source available on the `l
3939

4040
Let's break down how this could be done. Inside any plugin hook, you have a `compilation` object. You can get the `loaderContext` for any of the modules that webpack is processing through `compilation.modules` -- just find the one(s) you want by name. Now you have a large object which is an instance of the `DependenciesBlock` class, with a bunch of great information on it. You can find the raw buffered source under the `_src` property if the file was loaded with the source-loader.
4141

42-
Wondering what sets this loader apart from [raw-loader](https://github.com/webpack/raw-loader)? This is it. Both loaders expose the file's contents to be required by webpack, but this loader also exposes the raw source for plugin processing. It also has tests, and is actively maintained, as a bonus.
42+
Wondering what sets this loader apart from [raw-loader](https://github.com/webpack/raw-loader)? This is it. Both loaders expose the file's contents to be required by webpack, but this loader also exposes the raw source for plugin processing. It also does not try to stringify binary files (which can cause bugs), has tests, and is actively maintained, as a bonus.
4343

4444
### License & Contributing
4545

lib/index.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
const isBinaryPath = require('is-binary-path')
2+
const url = require('url')
3+
14
module.exports = function (source) {
25
this.cacheable && this.cacheable()
36
this._module._src = source
4-
return 'module.exports = ' + JSON.stringify(String(source))
7+
8+
const path = url.parse(this.request).pathname.match(/!(.*)$/)[1]
9+
let src = 'module.exports = '
10+
if (isBinaryPath(path)) {
11+
src += '"binary"'
12+
} else {
13+
src += JSON.stringify(String(source))
14+
}
15+
return src
516
}
617
module.exports.raw = true

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"bugs": "https://github.com/static-dev/source-loader/issues",
1010
"devDependencies": {
11-
"ava": "0.14.x",
11+
"ava": "^0.15.2",
1212
"coveralls": "2.x",
1313
"nyc": "6.x",
1414
"standard": "7.x",
@@ -26,5 +26,8 @@
2626
"lint": "standard",
2727
"coverage": "nyc ava",
2828
"coveralls": "nyc --reporter=lcov ava && cat ./coverage/lcov.info | coveralls"
29+
},
30+
"dependencies": {
31+
"is-binary-path": "^1.0.1"
2932
}
3033
}

test/fixtures/binary.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./bun.gif')

test/fixtures/bun.gif

1.69 MB
Loading

test/index.js

+20
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,23 @@ test.cb('raw source is added to loader context', (t) => {
4444
t.end()
4545
})
4646
})
47+
48+
test.cb('binary files not exported but are made availble for plugins', (t) => {
49+
const outputPath = path.join(fixturesPath, 'build-bin.js')
50+
51+
webpack({
52+
context: fixturesPath,
53+
entry: path.join(fixturesPath, 'binary'),
54+
output: { path: fixturesPath, filename: 'build-bin.js' },
55+
resolveLoader: {
56+
alias: { source: path.join(__dirname, '../lib/index.js') }
57+
},
58+
module: { loaders: [{ test: /\.gif$/, loader: 'source' }] }
59+
}, (err, res) => {
60+
if (err) { t.fail(err) }
61+
const src = fs.readFileSync(outputPath, 'utf8')
62+
t.truthy(src.match(/module\.exports = "binary"/))
63+
fs.unlinkSync(outputPath)
64+
t.end()
65+
})
66+
})

0 commit comments

Comments
 (0)