diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e7fc1c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.idea +.DS_Store + +node_modules +bower_components +dist +log diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ea94750 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Christoph von Gellhorn + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..0a4edcc --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# path-replace-loader for webpack + +Path replace loader for [webpack](http://webpack.github.io/). Replace a specific base path with another for dynamic module loading. Great for large applications with locally overridable modules. + +## Installation + +`npm install path-replace-loader` + +## Usage + +[Read more about using loaders](http://webpack.github.io/docs/using-loaders.html) + +#### Configuration + +- path: Absolute original path to replace, e.g __dirname/app/core +- replacePath: Absolute replacement path, e.g __dirname/app/local + +#### webpack config example + +``` js +module.exports = { + module: { + loaders: [ + { test: /\.js$/, loader: 'path-replace?path=ORIGINAL_PATH&replacePath=REPLACE_PATH' }, + ] + } +}; +``` + +#### Example + +``` js +var authModule = require('app/core/modules/auth'); +// Loader tries to load from local directory instead app/local/modules/auth +``` + +## Release History + +- 0.1.0 - Initial release + +## [MIT License](http://www.opensource.org/licenses/mit-license.php) diff --git a/index.js b/index.js new file mode 100644 index 0000000..0cc574d --- /dev/null +++ b/index.js @@ -0,0 +1,19 @@ +var fs = require('fs'); +var fileExists = require('file-exists'); +var loaderUtils = require('loader-utils'); + +module.exports = function(source) { + this.cacheable && this.cacheable(); + var options = loaderUtils.parseQuery(this.query); + + if (this.resourcePath.indexOf(options.path) > -1) { + var newPath = this.resourcePath.replace(options.path, options.replacePath); + if (fileExists(newPath)) { + return fs.readFileSync(newPath); + } + } + + return source; +}; + +module.exports.raw = true; diff --git a/package.json b/package.json new file mode 100644 index 0000000..12add55 --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "name": "path-replace-loader", + "version": "0.1.0", + "description": "Path replace loader for webpack", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/cvgellhorn/path-replace-loader.git" + }, + "keywords": [ + "webpack", + "loader", + "path", + "replace" + ], + "author": "Christoph von Gellhorn", + "license": "MIT", + "bugs": { + "url": "https://github.com/cvgellhorn/path-replace-loader/issues" + }, + "homepage": "https://github.com/cvgellhorn/path-replace-loader#readme", + "dependencies": { + "file-exists": "^1.0.0", + "loader-utils": "^0.2.14" + } +}