├── .gitignore ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | npm-debug.log 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Transfer Webpack Plugin 2 | 3 | Transfer files to the build directory 4 | 5 | ### Getting started 6 | 7 | Install the plugin: 8 | 9 | ``` 10 | npm install --save-dev transfer-webpack-plugin 11 | ``` 12 | 13 | 14 | ### API 15 | ```javascript 16 | new TransferWebpackPlugin(patterns: array, [basePath: string]) 17 | ``` 18 | 19 | * `patterns` – array of patterns `{ from: 'path', to: 'path' }`, `from` – relative to `basePath` or to `context` of your config (if `basePath` is not exists), 20 | `to` – relative to the build directory 21 | * `basePath` (optional) – directory to be resolved to `from` parameter 22 | 23 | ### Usage 24 | 25 | ```javascript 26 | var TransferWebpackPlugin = require('transfer-webpack-plugin'); 27 | 28 | module.exports = { 29 | context: path.join(__dirname, 'app'), 30 | plugins: [ 31 | new TransferWebpackPlugin([ 32 | { from: 'i18n', to: 'i18n' }, 33 | { from: 'root' } 34 | ]) 35 | ] 36 | }; 37 | 38 | module.exports = { 39 | plugins: [ 40 | new TransferWebpackPlugin([ 41 | { from: 'i18n', to: 'i18n' }, 42 | { from: 'root' } 43 | ], path.join(__dirname, 'app')) 44 | ] 45 | }; 46 | ``` 47 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | var dir = require('node-dir'); 3 | var vow = require('vow'); 4 | var fs = require('fs'); 5 | 6 | function TransferWebpackPlugin(patterns, basePath) { 7 | this.patterns = patterns || []; 8 | this.basePath = basePath; 9 | } 10 | 11 | TransferWebpackPlugin.prototype.apply = function(compiler) { 12 | var _this = this; 13 | var basePath = this.basePath || compiler.options.context || null; 14 | 15 | compiler.plugin('emit', function(compilation, cb) { 16 | if (!basePath) { 17 | compilation.errors.push(new Error('TransferWebpackPlugin: no basePath provided')); 18 | cb(); 19 | } 20 | 21 | var promises = []; 22 | 23 | _this.patterns.forEach(function(pattern) { 24 | promises.push(_this.processDir(path.resolve(basePath, pattern.from), pattern.to, compilation)); 25 | }); 26 | 27 | vow.all(promises).then(function() { 28 | cb(); 29 | }).fail(function(text) { 30 | compilation.errors.push(new Error(text)); 31 | cb(); 32 | }); 33 | }); 34 | }; 35 | 36 | TransferWebpackPlugin.prototype.processDir = function(from, to, compilation) { 37 | var defer = vow.defer(); 38 | 39 | dir.files(from, function(err, files) { 40 | if (err) { 41 | defer.reject('TransferWebpackPlugin: ' + err); 42 | return; 43 | } 44 | 45 | var allFiles = files.map(function(fullPath) { 46 | var fileName = fullPath.replace(from, ''); 47 | var distName = to ? path.join(to, fileName) : fileName; 48 | 49 | compilation.assets[distName] = { 50 | size: function() { 51 | return fs.statSync(fullPath).size; 52 | }, 53 | source: function() { 54 | return fs.readFileSync(fullPath); 55 | }, 56 | }; 57 | }); 58 | 59 | defer.resolve(vow.all(allFiles)); 60 | }); 61 | 62 | return defer.promise(); 63 | }; 64 | 65 | module.exports = TransferWebpackPlugin; 66 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "transfer-webpack-plugin", 3 | "version": "0.1.4", 4 | "description": "transfer files to the build directory", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/molforp/transfer-webpack-plugin.git" 9 | }, 10 | "keywords": [ 11 | "webpack", 12 | "plugin", 13 | "transfer", 14 | "move", 15 | "copy" 16 | ], 17 | "author": "Dmitry Kokorev", 18 | "license": "MIT", 19 | "homepage": "https://github.com/molforp/transfer-webpack-plugin", 20 | "dependencies": { 21 | "node-dir": "^0.1.6", 22 | "vow": "^0.4.9" 23 | } 24 | } 25 | --------------------------------------------------------------------------------