Last active
August 29, 2015 14:17
-
-
Save pilwon/dc5d89e24253de76e385 to your computer and use it in GitHub Desktop.
ReactNativeResolverPlugin (WIP) https://github.com/facebook/react-native/issues/278#issuecomment-87197841
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'); | |
function ReactNativeResolverPlugin(reactNativeRoot) { | |
if (!reactNativeRoot) { | |
reactNativeRoot = path.resolve(__dirname, 'node_modules/react-native'); | |
} | |
this.reactNativeRoot = reactNativeRoot; | |
} | |
function _createAliasMapPromise(reactNativeRoot) { | |
var blacklist = require(path.resolve(reactNativeRoot, 'packager/blacklist')); | |
var ReactPackager = require(path.resolve(reactNativeRoot, 'packager/react-packager')); | |
var reactNativePackage = require(path.resolve(reactNativeRoot, 'package')); | |
return ReactPackager.getDependencies({ | |
assetRoots: [reactNativeRoot], | |
blacklistRE: blacklist(false), | |
projectRoots: [reactNativeRoot] | |
}, reactNativePackage.main) | |
.then(function (dependencies) { | |
return dependencies.filter(function (dependency) { | |
return !dependency.isPolyfill; | |
}); | |
}) | |
.then(function (dependencies) { | |
return dependencies.reduce(function (result, dependency) { | |
result[dependency.id] = dependency.path; | |
return result; | |
}, {}); | |
}); | |
}; | |
ReactNativeResolverPlugin.prototype.apply = function (resolver) { | |
var aliasMapPromise = _createAliasMapPromise(this.reactNativeRoot); | |
resolver.plugin('module', function (request, cb) { | |
aliasMapPromise.then(function (aliasMap) { | |
if (aliasMap[request.request]) { | |
return this.doResolve('result', { | |
path: request.path, | |
request: path.relative(request.path, aliasMap[request.request]), | |
file: true, | |
resolved: true | |
}, cb); | |
} | |
return cb(); | |
}); | |
}.bind(this)); | |
}; | |
module.exports = ReactNativeResolverPlugin; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'); | |
var webpack = require('webpack'); | |
var ReactNativeResolverPlugin = require('./ReactNativeResolverPlugin'); | |
module.exports = { | |
debug: true, | |
devtool: 'source-map', | |
entry: { | |
'index.ios.bundle': path.join(__dirname, 'index.ios') | |
}, | |
module: { | |
loaders: [ | |
{test: /\.js$/, loader: 'babel?blacklist[]=react', exclude: /node_modules\//}, | |
{test: /\.js$/, loader: 'jsx?harmony&stripTypes', include: /node_modules\/react-native\//}, | |
{test: /\.jsx$/, loader: 'imports?react=react-native!babel'}, | |
] | |
}, | |
output: { | |
filename: '[name]' | |
}, | |
plugins: [ | |
new webpack.DefinePlugin({ | |
__DEV__: true | |
}), | |
new webpack.ResolverPlugin([ | |
new ReactNativeResolverPlugin() | |
], ['normal', 'loader']) | |
], | |
resolve: { | |
extensions: [ | |
'', | |
'.js', | |
'.jsx' | |
] | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment