├── .gitignore ├── .idea └── jsLibraryMappings.xml ├── LICENSE ├── README.md ├── custom.js ├── index.js ├── package.json └── test ├── fixtures ├── fixture.css ├── fixture.csv ├── fixture.html ├── fixture.json ├── fixture.tpl.html └── fixture.xml ├── index.js └── runners ├── css.js ├── defaults.js ├── extras.js ├── html.js ├── json.js ├── opts.js ├── tpl.html.js └── unique.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.dat 5 | *.out 6 | *.pid 7 | *.gz 8 | 9 | pids 10 | logs 11 | results 12 | 13 | npm-debug.log 14 | node_modules 15 | .idea/ 16 | -------------------------------------------------------------------------------- /.idea/jsLibraryMappings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Ben Clinkinbeard 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | partialify 2 | ========== 3 | 4 | require() file contents of HTML, CSS and (potentially) more into a variable as a string. 5 | 6 | Supports HTML and CSS out of the box, enabling code like this. 7 | ```js 8 | var html = require('./some.html'), 9 | css = require('./some.css'); 10 | ``` 11 | 12 | To use, specify as a Browserify transform in your `package.json` or programmatically like so: 13 | ```js 14 | var b = require('browserify')(), 15 | fs = require('fs'), 16 | p = require('partialify'); 17 | 18 | b.add('./entry.js'); 19 | b.transform(p); 20 | b.bundle().pipe(fs.createWriteStream('./bundle.js')); 21 | ``` 22 | 23 | To support other file types use the custom version. You can either augment the default supported file types or specify a completely custom list. 24 | 25 | ```js 26 | var b = require('browserify')(), 27 | fs = require('fs'), 28 | p = require('partialify/custom'); 29 | 30 | b.add('./entry.js'); 31 | 32 | b.transform(p.alsoAllow('xml')); 33 | // or 34 | b.transform(p.alsoAllow(['xml', 'csv'])); 35 | // or 36 | b.transform(p.onlyAllow(['xml', 'csv'])); 37 | 38 | b.bundle().pipe(fs.createWriteStream('./bundle.js')); 39 | ``` 40 | 41 | ### Customizing from the CLI 42 | 43 | `browserify index.js -t [ partialify --alsoAllow svg --alsoAllow xml ] -o bundle.js` 44 | 45 | `browserify index.js -t [ partialify --onlyAllow svg --onlyAllow tsv ] -o bundle.js` 46 | -------------------------------------------------------------------------------- /custom.js: -------------------------------------------------------------------------------- 1 | var through = require('through'), 2 | str2js = require('string-to-js'), 3 | types = ['html', 'css']; 4 | 5 | function isValidFile (file, opts) { 6 | var validTypes = types; 7 | if (opts && opts.onlyAllow) validTypes = opts.onlyAllow; 8 | if (opts && opts.alsoAllow) validTypes = validTypes.concat(opts.alsoAllow); 9 | if (!Array.isArray(validTypes)) validTypes = [validTypes]; 10 | 11 | return validTypes.some(function (type) { 12 | return file.substr(-(type.length + 1)) === '.' + type; 13 | }); 14 | } 15 | 16 | function partialify (file, opts) { 17 | 18 | if (!isValidFile(file, opts)) return through(); 19 | 20 | var buffer = ""; 21 | 22 | return through(function (chunk) { 23 | buffer += chunk.toString(); 24 | }, 25 | function () { 26 | if (buffer.indexOf('module.exports') === 0) { 27 | this.queue(buffer); // prevent "double" transforms 28 | } else { 29 | this.queue(str2js(buffer)); 30 | } 31 | this.queue(null); 32 | }); 33 | 34 | }; 35 | 36 | exports.onlyAllow = function (extensions) { 37 | if (extensions) { 38 | if (!Array.isArray(extensions)) extensions = Array.prototype.slice.call(arguments, 0); 39 | 40 | types = extensions; 41 | } 42 | return partialify; 43 | } 44 | 45 | exports.alsoAllow = function (extensions) { 46 | if (!Array.isArray(extensions)) extensions = Array.prototype.slice.call(arguments, 0); 47 | types = types.concat(extensions); 48 | return partialify; 49 | } 50 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./custom').onlyAllow(); 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "partialify", 3 | "version": "3.1.6", 4 | "description": "require()-able HTML, CSS, and (potentially) more", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "tape test" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/bclinkinbeard/partialify" 12 | }, 13 | "keywords": [ 14 | "browser", 15 | "browserify", 16 | "browserify-transform", 17 | "require", 18 | "static", 19 | "asset", 20 | "bundle", 21 | "partial" 22 | ], 23 | "author": "Ben Clinkinbeard", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/bclinkinbeard/partialify/issues" 27 | }, 28 | "dependencies": { 29 | "string-to-js": "0.0.1", 30 | "through": "^2.3.4" 31 | }, 32 | "devDependencies": { 33 | "tape": "^3.2.0", 34 | "browserify": ">=3.24.1 <9.0.0" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /test/fixtures/fixture.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: red; 3 | color: #ccc; 4 | } 5 | -------------------------------------------------------------------------------- /test/fixtures/fixture.csv: -------------------------------------------------------------------------------- 1 | some,value -------------------------------------------------------------------------------- /test/fixtures/fixture.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Text in HTML 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /test/fixtures/fixture.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "partialify", 3 | "version": "1.0.0", 4 | "description": "require()-able HTML, CSS, JSON and (potentially) more", 5 | "main": "index.js" 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/fixture.tpl.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Text in HTML 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /test/fixtures/fixture.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var partialify = require('../'); 2 | var customPartialify = require('../custom'); 3 | 4 | var test = require('tape'); 5 | var browserify = require('browserify'); 6 | 7 | var vm = require('vm'); 8 | var fs = require('fs'); 9 | var path = require('path'); 10 | 11 | var html = fs.readFileSync(__dirname + '/fixtures/fixture.html', 'utf8'); 12 | var css = fs.readFileSync(__dirname + '/fixtures/fixture.css', 'utf8'); 13 | var json = fs.readFileSync(__dirname + '/fixtures/fixture.json', 'utf8'); 14 | var xml = fs.readFileSync(__dirname + '/fixtures/fixture.xml', 'utf8'); 15 | var csv = fs.readFileSync(__dirname + '/fixtures/fixture.csv', 'utf8'); 16 | var tplhtml = fs.readFileSync(__dirname + '/fixtures/fixture.tpl.html', 'utf8'); 17 | 18 | test('require() an HTML file', function (t) { 19 | t.plan(1); 20 | 21 | var b = browserify(); 22 | b.add(__dirname + '/runners/html.js'); 23 | b.transform(partialify); 24 | 25 | b.bundle(function (err, src) { 26 | if (err) t.fail(err); 27 | vm.runInNewContext(src, { console: { log: log } }); 28 | }); 29 | 30 | function log (msg) { 31 | t.equal(msg, html); 32 | } 33 | 34 | }); 35 | 36 | test('require() a CSS file', function (t) { 37 | t.plan(1); 38 | 39 | var b = browserify(); 40 | b.add(__dirname + '/runners/css.js'); 41 | b.transform(partialify); 42 | 43 | b.bundle(function (err, src) { 44 | if (err) t.fail(err); 45 | vm.runInNewContext(src, { console: { log: log } }); 46 | }); 47 | 48 | function log (msg) { 49 | t.equal(msg, css); 50 | } 51 | 52 | }); 53 | 54 | test('Default behavior accepts HTML and CSS', function (t) { 55 | t.plan(2); 56 | 57 | var output = {}; 58 | 59 | var b = browserify(); 60 | b.add(__dirname + '/runners/defaults.js'); 61 | b.transform(partialify); 62 | 63 | b.bundle(function (err, src) { 64 | if (err) t.fail(err); 65 | vm.runInNewContext(src, { output: output, finish: finish }); 66 | }); 67 | 68 | function finish () { 69 | t.equal(output.html, html); 70 | t.equal(output.css, css); 71 | } 72 | 73 | }); 74 | 75 | test('Support for extra file types can be added', function (t) { 76 | t.plan(4); 77 | 78 | var output = {}; 79 | 80 | var b = browserify(); 81 | b.add(__dirname + '/runners/extras.js'); 82 | b.transform(customPartialify.alsoAllow('json', 'xml')); 83 | 84 | b.bundle(function (err, src) { 85 | if (err) t.fail(err); 86 | vm.runInNewContext(src, { output: output, finish: finish }); 87 | }); 88 | 89 | function finish () { 90 | t.equal(output.html, html); 91 | t.equal(output.css, css); 92 | t.equal(output.json, json); 93 | t.equal(output.xml, xml); 94 | } 95 | 96 | }); 97 | 98 | test('Support for extra file types can be added via options', function (t) { 99 | t.plan(2); 100 | 101 | var output = {}; 102 | 103 | var b = browserify(); 104 | b.add(__dirname + '/runners/opts.js'); 105 | b.transform({ alsoAllow: ['xml'] }, partialify); 106 | 107 | b.bundle(function (err, src) { 108 | if (err) t.fail(err); 109 | vm.runInNewContext(src, { output: output, finish: finish }); 110 | }); 111 | 112 | function finish () { 113 | t.equal(output.html, html); 114 | t.equal(output.xml, xml); 115 | } 116 | 117 | }); 118 | 119 | test('Supported file types list can be completely custom', function (t) { 120 | t.plan(2); 121 | 122 | var output = {}; 123 | 124 | var b = browserify(); 125 | b.add(__dirname + '/runners/unique.js'); 126 | b.transform(customPartialify.onlyAllow(['xml', 'csv'])); 127 | 128 | b.bundle(function (err, src) { 129 | if (err) t.fail(err); 130 | vm.runInNewContext(src, { output: output, finish: finish }); 131 | }); 132 | 133 | function finish () { 134 | t.equal(output.xml, xml); 135 | t.equal(output.csv, csv); 136 | } 137 | 138 | }); 139 | 140 | test('Supported file types list can be completely customized via options', function (t) { 141 | t.plan(2); 142 | 143 | var output = {}; 144 | 145 | var b = browserify(); 146 | b.add(__dirname + '/runners/unique.js'); 147 | b.transform(partialify, {onlyAllow: ['xml', 'csv']}); 148 | 149 | b.bundle(function (err, src) { 150 | if (err) t.fail(err); 151 | vm.runInNewContext(src, { output: output, finish: finish }); 152 | }); 153 | 154 | function finish () { 155 | t.equal(output.xml, xml); 156 | t.equal(output.csv, csv); 157 | } 158 | 159 | }); 160 | 161 | test('Compound file extensions are supported', function (t) { 162 | t.plan(1); 163 | 164 | var b = browserify(); 165 | b.add(__dirname + '/runners/tpl.html.js'); 166 | b.transform(customPartialify.onlyAllow(['tpl.html'])); 167 | 168 | b.bundle(function (err, src) { 169 | if (err) t.fail(err); 170 | vm.runInNewContext(src, { console: { log: log } }); 171 | }); 172 | 173 | function log (msg) { 174 | t.equal(msg, tplhtml); 175 | } 176 | 177 | }); 178 | -------------------------------------------------------------------------------- /test/runners/css.js: -------------------------------------------------------------------------------- 1 | var css = require('../fixtures/fixture.css'); 2 | console.log(css); 3 | -------------------------------------------------------------------------------- /test/runners/defaults.js: -------------------------------------------------------------------------------- 1 | output.html = require('../fixtures/fixture.html'); 2 | output.css = require('../fixtures/fixture.css'); 3 | 4 | finish(); 5 | -------------------------------------------------------------------------------- /test/runners/extras.js: -------------------------------------------------------------------------------- 1 | output.html = require('../fixtures/fixture.html'); 2 | output.css = require('../fixtures/fixture.css'); 3 | output.json = require('../fixtures/fixture.json'); 4 | output.xml = require('../fixtures/fixture.xml'); 5 | 6 | finish(); 7 | -------------------------------------------------------------------------------- /test/runners/html.js: -------------------------------------------------------------------------------- 1 | var html = require('../fixtures/fixture.html'); 2 | console.log(html); 3 | -------------------------------------------------------------------------------- /test/runners/json.js: -------------------------------------------------------------------------------- 1 | var json = require('../fixtures/fixture.json'); 2 | console.log(json); 3 | -------------------------------------------------------------------------------- /test/runners/opts.js: -------------------------------------------------------------------------------- 1 | output.html = require('../fixtures/fixture.html'); 2 | output.xml = require('../fixtures/fixture.xml'); 3 | 4 | finish(); 5 | -------------------------------------------------------------------------------- /test/runners/tpl.html.js: -------------------------------------------------------------------------------- 1 | var html = require('../fixtures/fixture.tpl.html'); 2 | console.log(html); 3 | -------------------------------------------------------------------------------- /test/runners/unique.js: -------------------------------------------------------------------------------- 1 | output.xml = require('../fixtures/fixture.xml'); 2 | output.csv = require('../fixtures/fixture.csv'); 3 | 4 | finish(); 5 | --------------------------------------------------------------------------------