├── .gitignore ├── .jshintrc ├── .travis.yml ├── LICENSE ├── README.md ├── example ├── app.js ├── index.html └── vendor │ └── some-dependency.js ├── index.js ├── package.json └── test ├── browserify.js ├── fixtures ├── json-dep.json ├── simple.js └── with-json.js └── literalify.spec.js /.gitignore: -------------------------------------------------------------------------------- 1 | .*.swp 2 | *~ 3 | node_modules 4 | coverage 5 | npm-debug.log* 6 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "curly": true, 3 | "eqeqeq": true, 4 | "immed": true, 5 | "indent": 2, 6 | "noarg": true, 7 | "noempty": true, 8 | "nonew": true, 9 | "quotmark": "single", 10 | "undef": true, 11 | "unused": true, 12 | "trailing": true, 13 | "maxparams": 4, 14 | "maxdepth": 4, 15 | "white": false, 16 | "node": true 17 | } 18 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.12" 4 | - "0.10" 5 | after_script: 6 | - npm run coveralls 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Alan Plum 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a 4 | copy of this software and associated documentation files (the "Software"), 5 | to deal in the Software without restriction, including without limitation 6 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 7 | and/or sell copies of the Software, and to permit persons to whom the 8 | Software is furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **NOTE:** This package is no longer being maintained. If you are interested in taking over as maintainer or are interested in the npm package name, get in touch by creating an issue. 2 | 3 | # Synopsis 4 | 5 | **literalify** is a [browserify](https://github.com/substack/node-browserify) transform for replacing require calls with arbitrary code, e.g. to pretend browser globals are actually CommonJS modules. 6 | 7 | This library uses [browserify-transform-tools](https://github.com/benbria/browserify-transform-tools), so you can also supply the configuration by adding a `literalify` field to your project's `package.json` file. 8 | 9 | [![license - MIT](https://img.shields.io/npm/l/literalify.svg)](http://pluma.mit-license.org) [![Dependencies](https://img.shields.io/david/pluma/literalify.svg)](https://david-dm.org/pluma/literalify) 10 | 11 | [![NPM status](https://nodei.co/npm/literalify.png?compact=true)](https://npmjs.org/package/literalify) 12 | 13 | [![Build Status](https://img.shields.io/travis/pluma/literalify.svg)](https://travis-ci.org/pluma/literalify) [![Coverage Status](https://img.shields.io/coveralls/pluma/literalify.svg)](https://coveralls.io/r/pluma/literalify?branch=master) 14 | 15 | # Rationale 16 | 17 | If you only want to convert globals-polluting libraries into CommonJS modules, [moduleify](https://github.com/pluma/moduleify) would be sufficient. But in some cases you still want these libraries to be loaded via script-tags or have to use an existing non-browserified bundle. 18 | 19 | In these cases the recommended approach seems to be to create shim modules for each browser global you want to use. But compared to just using `window.myLibrary` the overhead of creating a new shim module seems a bit extreme. 20 | 21 | With `literalify` you don't have to choose: you can use `require` to load your legacy libraries *without* having to pollute your filesystem with those pesky shims. 22 | 23 | # Use Case: AngularJS 24 | 25 | In the 1.2 release AngularJS has begun to move some core features into separate files. If you want to use AngularJS with browserify `require` calls that means you now have the option of either bundling AngularJS with all the extensions you want to use before browserify is applied or to wrap each extension in a separate module and make sure to load all of them explicitly in your start script. In either case you need to run the AngularJS files through browserify in order to be able to `require` them. 26 | 27 | With `literalify` you can keep the AngularJS files out of browserify and let it simply replace all calls of `require("angular")` with references to `window.angular` without having to make manual changes to your code or AngularJS. 28 | 29 | # Install 30 | 31 | ## Node.js 32 | 33 | ### With NPM 34 | 35 | ```sh 36 | npm install literalify 37 | ``` 38 | 39 | ### From source 40 | 41 | ```sh 42 | git clone https://github.com/pluma/literalify.git 43 | cd literalify 44 | npm install 45 | make test 46 | ``` 47 | 48 | # Basic usage example 49 | 50 | ## example/vendor/some-dependency.js 51 | 52 | ```javascript 53 | window.$ = { 54 | makeAwesome: function(str) { 55 | return str.toUpperCase(); 56 | } 57 | }; 58 | ``` 59 | 60 | ## example/app.js 61 | 62 | ```javascript 63 | var makeAwesome = require('some-dependency').makeAwesome; 64 | console.log(makeAwesome('needs more caps')); 65 | ``` 66 | 67 | ## example/index.html 68 | ```html 69 | 70 | 71 | 72 | 73 | Look at the console! 74 | 75 | 76 | 77 | 78 | 79 | 80 | ``` 81 | 82 | ## Usage 83 | 84 | ```javascript 85 | var browserify = require('browserify'), 86 | literalify = require('literalify'), 87 | b = browserify(); 88 | 89 | b.transform(literalify.configure({ 90 | 'some-dependency': 'window.$' 91 | })); 92 | b.add('./app.js'); 93 | b.bundle().pipe(require('fs').createWriteStream('bundle.js')); 94 | ``` 95 | 96 | # Usage example with package.json 97 | 98 | ## package.json 99 | 100 | ```json 101 | { 102 | "name": "my-awesome-project", 103 | "devDependencies": { 104 | "browserify": "*", 105 | "literalify": "*" 106 | }, 107 | "literalify": {"some-dependency": "window.$"} 108 | } 109 | ``` 110 | 111 | ### Usage (API) 112 | 113 | ```javascript 114 | var browserify = require('browserify'), 115 | literalify = require('literalify'), 116 | b = browserify(); 117 | 118 | b.transform(literalify); 119 | b.add('./app.js'); 120 | b.bundle().pipe(require('fs').createWriteStream('bundle.js')); 121 | ``` 122 | 123 | ### Usage (Shell) 124 | 125 | ```sh 126 | browserify -t literalify ./app.js > bundle.js 127 | ``` 128 | 129 | # API 130 | 131 | ## literalify.configure(rules):transform 132 | 133 | Creates a browserify transform that will replace the given require calls with the given JavaScript expressions. 134 | 135 | # License 136 | 137 | The MIT/Expat license. For more information, see http://pluma.mit-license.org/ or the accompanying [LICENSE](https://github.com/pluma/counting/blob/master/LICENSE) file. 138 | -------------------------------------------------------------------------------- /example/app.js: -------------------------------------------------------------------------------- 1 | var $ = require('some-dependency'); 2 | console.log($.makeAwesome('needs more caps')); -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Look at the console! 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /example/vendor/some-dependency.js: -------------------------------------------------------------------------------- 1 | window.$ = { 2 | makeAwesome: function(str) { 3 | return str.toUpperCase(); 4 | } 5 | }; -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var transformTools = require('browserify-transform-tools'); 2 | 3 | module.exports = transformTools.makeRequireTransform('literalify', {excludeExtensions: ['json']}, function(args, opts, cb) { 4 | if (opts.config && args[0] in opts.config) { 5 | return cb(null, opts.config[args[0]]); 6 | } else { 7 | return cb(); 8 | } 9 | }); 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "literalify", 3 | "author": "Alan Plum ", 4 | "version": "0.4.0", 5 | "license": "MIT", 6 | "description": "A browserify transform for replacing require calls with arbitrary code.", 7 | "keywords": [ 8 | "browserify", 9 | "transform", 10 | "modules", 11 | "browser", 12 | "globals", 13 | "commonjs", 14 | "require" 15 | ], 16 | "main": "./index.js", 17 | "files": [ 18 | "README.md", 19 | "LICENSE", 20 | "index.js" 21 | ], 22 | "dependencies": { 23 | "browserify-transform-tools": "^1.3.0" 24 | }, 25 | "devDependencies": { 26 | "browserify": "^9.0.3", 27 | "coveralls": "^2.11.2", 28 | "expect.js": "^0.3.1", 29 | "istanbul": "^0.3.6", 30 | "jshint": "^2.6.3", 31 | "mocha": "^2.1.0" 32 | }, 33 | "repository": { 34 | "type": "git", 35 | "url": "https://github.com/pluma/literalify.git" 36 | }, 37 | "scripts": { 38 | "lint": "jshint index.js test", 39 | "test": "mocha --growl -R spec", 40 | "cover": "istanbul cover --report lcov _mocha -- -R spec", 41 | "coveralls": "npm run cover && cat ./coverage/lcov.info | coveralls ; rm -rf ./coverage" 42 | }, 43 | "engines": { 44 | "node": ">=0.10" 45 | } 46 | } -------------------------------------------------------------------------------- /test/browserify.js: -------------------------------------------------------------------------------- 1 | /*global describe, it */ 2 | var expect = require('expect.js'), 3 | literalify = require('../'), 4 | browserify = require('browserify'), 5 | vm = require('vm'); 6 | 7 | describe('browserify -t literalify', function () { 8 | it('handles JavaScript files', function (done) { 9 | var b = browserify({basedir: __dirname}); 10 | b.transform(literalify.configure({'some-dependency': 'alert'})); 11 | b.add('./fixtures/simple.js'); 12 | b.bundle(function (err, buf) { 13 | expect(err).not.to.be.ok(); 14 | var c = vm.createContext(); 15 | var timesCalled = 0; 16 | c.alert = function (arg) { 17 | expect(arg).to.eql('potato'); 18 | timesCalled++; 19 | }; 20 | vm.runInContext(buf.toString('utf-8'), c); 21 | expect(timesCalled).to.equal(1); 22 | done(); 23 | }); 24 | }); 25 | it('handles JSON files', function (done) { 26 | var b = browserify({basedir: __dirname}); 27 | b.transform(literalify.configure({'some-dependency': 'alert'})); 28 | b.add('./fixtures/with-json.js'); 29 | b.bundle(function (err, buf) { 30 | expect(err).not.to.be.ok(); 31 | var c = vm.createContext(); 32 | var timesCalled = 0; 33 | c.alert = function (arg) { 34 | expect(arg).to.eql(require('./fixtures/json-dep')); 35 | timesCalled++; 36 | }; 37 | vm.runInContext(buf.toString('utf-8'), c); 38 | expect(timesCalled).to.equal(1); 39 | done(); 40 | }); 41 | }); 42 | }); -------------------------------------------------------------------------------- /test/fixtures/json-dep.json: -------------------------------------------------------------------------------- 1 | { 2 | "hello": "world" 3 | } -------------------------------------------------------------------------------- /test/fixtures/simple.js: -------------------------------------------------------------------------------- 1 | module.exports = require('some-dependency')('potato'); -------------------------------------------------------------------------------- /test/fixtures/with-json.js: -------------------------------------------------------------------------------- 1 | var json = require('./json-dep'); 2 | module.exports = require('some-dependency')(json); -------------------------------------------------------------------------------- /test/literalify.spec.js: -------------------------------------------------------------------------------- 1 | /*global describe, it */ 2 | var expect = require('expect.js'), 3 | literalify = require('../'); 4 | 5 | describe('literalify', function () { 6 | it('is a function', function () { 7 | expect(literalify).to.be.a('function'); 8 | }); 9 | it('replaces matching require calls', function (done) { 10 | var tr = literalify.configure({'some-dependency': '25'})('foo.js'); 11 | var data = ''; 12 | tr.on('data', function (chunk) { 13 | data += chunk; 14 | }); 15 | tr.on('end', function () { 16 | expect(data).to.equal('var dep = 25;'); 17 | done(); 18 | }); 19 | tr.write('var dep = require(\'some-dependency\');'); 20 | tr.end(); 21 | }); 22 | it('does not replace non-matching require calls', function (done) { 23 | var tr = literalify.configure({'some-dependency': '25'})('foo.js'); 24 | var str = 'var dep = require(\'other-dependency\');'; 25 | var data = ''; 26 | tr.on('data', function (chunk) { 27 | data += chunk; 28 | }); 29 | tr.on('end', function () { 30 | expect(data).to.equal(str); 31 | done(); 32 | }); 33 | tr.write(str); 34 | tr.end(); 35 | }); 36 | it('does not touch JSON files (by default)', function (done) { 37 | var tr = literalify.configure({'some-dependency': '25'})('foo.json'); 38 | var str = 'var dep = require(\'some-dependency\');'; 39 | var data = ''; 40 | tr.on('data', function (chunk) { 41 | data += chunk; 42 | }); 43 | tr.on('end', function () { 44 | expect(data).to.equal(str); 45 | done(); 46 | }); 47 | tr.write(str); 48 | tr.end(); 49 | }); 50 | }); --------------------------------------------------------------------------------