Skip to content

Commit 51bd58b

Browse files
committed
.
0 parents  commit 51bd58b

File tree

9 files changed

+261
-0
lines changed

9 files changed

+261
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bower_components
2+
node_modules
3+
*.log
4+
.DS_Store
5+
bundle.js

.npmignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
bower_components
2+
node_modules
3+
*.log
4+
.DS_Store
5+
bundle.js
6+
test
7+
test.js
8+
demo/
9+
.npmignore
10+
LICENSE.md

LICENSE.md

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

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# ify-loader
2+
3+
[![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges)
4+
5+
[Webpack](https://github.com/webpack/webpack) loader to handle [browserify transforms]() as intended.
6+
7+
## Usage
8+
9+
Install the loader using [npm](https://npmjs.com/):
10+
11+
``` glsl
12+
npm install --save ify-loader
13+
```
14+
15+
You can then update your `webpack.config.js` in a similar fashion to the following to add browserify transform support to your project:
16+
17+
``` javascript
18+
module.exports = {
19+
module: {
20+
loaders: [
21+
// This applies the loader to all of your dependencies,
22+
// and not any of the source files in your project:
23+
{
24+
test: /node_modules/,
25+
loader: 'ify'
26+
}
27+
]
28+
}
29+
}
30+
```
31+
32+
## Why?
33+
34+
When given the choice, I lean more in favour of [browserify](http://browserify.org) for its simplicity and compatability with node.js — however from time to time I need to work on projects that use webpack. The thing I run into issues with most often when switching between the two is the difference in how webpack handles source transforms compared to browserify.
35+
36+
Webpack provides you with a "global" configuration where you specify how your project and its dependencies are transformed in a single place. Browserify, however, scopes transforms to the current package to avoid conflicts between different dependencies' sources using the [`browserify.transform` property](https://github.com/substack/node-browserify#browserifytransform) in `package.json`.
37+
38+
There are pros and cons to both approaches — Webpack gives you more control, at the expense of having to configure each transform used in your dependency tree. Unlike [transform-loader](https://github.com/webpack/transform-loader), *ify-loader* will determine which transforms to apply to your dependencies for you, making the process a lot more bearable in complex projects!
39+
40+
## See Also
41+
42+
* [browserify](https://github.com/substack/node-browserify)
43+
* [webpack](https://github.com/webpack/webpack)
44+
* [transform-loader](https://github.com/webpack/transform-loader)
45+
46+
## License
47+
48+
MIT, see [LICENSE.md](http://github.com/hughsk/ify-loader/blob/master/LICENSE.md) for details.

fixture/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const fs = require('fs')
2+
3+
console.log(fs.readFileSync(__dirname + '/package.json', 'utf8'))

fixture/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "fixture",
3+
"version": "1.0.0",
4+
"browserify": {
5+
"transform": [
6+
"brfs"
7+
]
8+
}
9+
}

index.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const readJSON = require('read-package-json')
2+
const multipipe = require('multipipe')
3+
const from2 = require('from2-array')
4+
const resolve = require('resolve')
5+
const map = require('map-limit')
6+
const findup = require('findup')
7+
const path = require('path')
8+
const bl = require('bl')
9+
10+
module.exports = loader
11+
12+
function loader (source) {
13+
const filename = this.resourcePath
14+
const dirname = this.context
15+
const done = this.async()
16+
const self = this
17+
18+
this.cacheable(true)
19+
20+
findup(dirname, 'package.json', foundPackage)
21+
22+
function foundPackage (err, pkgDir) {
23+
if (err) return done(err)
24+
if (!pkgDir) return done(null, source)
25+
26+
const pkgFile = path.join(pkgDir, 'package.json')
27+
28+
readJSON(pkgFile, function (err, json) {
29+
if (err) return done(err)
30+
31+
const pkgTransforms = [].concat(
32+
json.browserify && json.browserify.transform
33+
)
34+
35+
map(pkgTransforms, 10, function (transform, next) {
36+
transform = Array.isArray(transform) ? transform : [transform]
37+
38+
const name = transform[0]
39+
const opts = transform[1] || {}
40+
41+
if (typeof name === 'function') {
42+
return next(null, name(filename, opts))
43+
}
44+
45+
resolve(name, {
46+
basedir: dirname
47+
}, function (err, name) {
48+
if (err) return next(err)
49+
50+
const TransformStream = require(name)
51+
52+
if (typeof TransformStream !== 'function') {
53+
return next(new Error(
54+
'Browserify transform at ' + name + ' did not export a function'
55+
))
56+
}
57+
58+
next(null, TransformStream(filename, opts))
59+
})
60+
}, function (err, transforms) {
61+
if (err) return done(err)
62+
63+
transforms.forEach(function (tr) {
64+
tr.on('file', function (file) {
65+
self.addDependency(file)
66+
})
67+
})
68+
69+
transforms = []
70+
.concat(from2([source]))
71+
.concat(transforms)
72+
.concat(bl(done))
73+
74+
multipipe.apply(this, transforms)
75+
})
76+
})
77+
}
78+
}

package.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "ify-loader",
3+
"version": "1.0.0",
4+
"description": "Webpack loader to handle browserify transforms as intended",
5+
"main": "index.js",
6+
"license": "MIT",
7+
"author": {
8+
"name": "Hugh Kennedy",
9+
"email": "[email protected]",
10+
"url": "https://github.com/hughsk"
11+
},
12+
"dependencies": {
13+
"bl": "^1.0.0",
14+
"findup": "^0.1.5",
15+
"from2-array": "0.0.4",
16+
"map-limit": "0.0.1",
17+
"multipipe": "^0.3.0",
18+
"read-package-json": "^2.0.2",
19+
"resolve": "^1.1.6"
20+
},
21+
"devDependencies": {
22+
"brfs": "^1.4.2",
23+
"npm-which": "^2.0.0",
24+
"standard": "^5.4.1",
25+
"tap-spec": "^4.1.1",
26+
"tape": "^4.4.0",
27+
"webpack": "^1.12.9"
28+
},
29+
"scripts": {
30+
"test": "node test.js | tspec",
31+
"posttest": "standard"
32+
},
33+
"keywords": [
34+
"ify",
35+
"browserify",
36+
"loader",
37+
"webpack"
38+
],
39+
"repository": {
40+
"type": "git",
41+
"url": "git://github.com/hughsk/ify-loader.git"
42+
},
43+
"homepage": "https://github.com/hughsk/ify-loader",
44+
"bugs": {
45+
"url": "https://github.com/hughsk/ify-loader/issues"
46+
}
47+
}

test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const spawn = require('child_process').spawn
2+
const which = require('npm-which')
3+
const test = require('tape')
4+
const path = require('path')
5+
const fs = require('fs')
6+
const vm = require('vm')
7+
8+
test('ify-loader', function (t) {
9+
const wpack = which.sync('webpack', { cwd: __dirname })
10+
const input = path.join(__dirname, 'fixture', 'index.js')
11+
const output = path.join(__dirname, 'fixture', 'bundle.js')
12+
const pkg = path.join(__dirname, 'fixture', 'package.json')
13+
14+
t.plan(1)
15+
16+
try {
17+
fs.unlinkSync(output)
18+
} catch (e) {}
19+
20+
spawn(wpack, [
21+
input,
22+
output,
23+
'--module-bind', 'js=' + __dirname
24+
], {
25+
stdio: ['pipe', 'pipe', 2]
26+
}).once('exit', function () {
27+
const result = fs.readFileSync(output, { encoding: 'utf8' })
28+
29+
fs.unlinkSync(output)
30+
31+
vm.runInNewContext(result, {
32+
console: {
33+
log: function (src) {
34+
const expected = fs.readFileSync(pkg, { encoding: 'utf8' })
35+
t.equal(src, expected, 'processed brfs from package.json')
36+
}
37+
}
38+
})
39+
})
40+
})

0 commit comments

Comments
 (0)