pax_global_header00006660000000000000000000000064131574156610014523gustar00rootroot0000000000000052 comment=cbe9197bfb08e7354b2e84cd060ba36fc476e1d3 require-from-string-2.0.1/000077500000000000000000000000001315741566100154445ustar00rootroot00000000000000require-from-string-2.0.1/.editorconfig000066400000000000000000000003471315741566100201250ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 [*.md] trim_trailing_whitespace = false require-from-string-2.0.1/.gitattributes000066400000000000000000000000141315741566100203320ustar00rootroot00000000000000* text=auto require-from-string-2.0.1/.gitignore000066400000000000000000000000151315741566100174300ustar00rootroot00000000000000node_modules require-from-string-2.0.1/.jshintrc000066400000000000000000000002521315741566100172700ustar00rootroot00000000000000{ "node": true, "esnext": true, "bitwise": true, "curly": true, "immed": true, "newcap": true, "noarg": true, "undef": true, "unused": "vars", "strict": true } require-from-string-2.0.1/.travis.yml000066400000000000000000000001071315741566100175530ustar00rootroot00000000000000sudo: false language: node_js node_js: - '7' - '6' - '5' - '4' require-from-string-2.0.1/index.js000066400000000000000000000015301315741566100171100ustar00rootroot00000000000000'use strict'; var Module = require('module'); var path = require('path'); module.exports = function requireFromString(code, filename, opts) { if (typeof filename === 'object') { opts = filename; filename = undefined; } opts = opts || {}; filename = filename || ''; opts.appendPaths = opts.appendPaths || []; opts.prependPaths = opts.prependPaths || []; if (typeof code !== 'string') { throw new Error('code must be a string, not ' + typeof code); } var paths = Module._nodeModulePaths(path.dirname(filename)); var parent = module.parent; var m = new Module(filename, parent); m.filename = filename; m.paths = [].concat(opts.prependPaths).concat(paths).concat(opts.appendPaths); m._compile(code, filename); var exports = m.exports; parent.children && parent.children.splice(parent.children.indexOf(m), 1); return exports; }; require-from-string-2.0.1/license000066400000000000000000000021501315741566100170070ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Vsevolod Strukchinsky (github.com/floatdrop) 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. require-from-string-2.0.1/package.json000066400000000000000000000007771315741566100177450ustar00rootroot00000000000000{ "name": "require-from-string", "version": "2.0.1", "description": "Require module from string", "license": "MIT", "repository": "floatdrop/require-from-string", "author": { "name": "Vsevolod Strukchinsky", "email": "floatdrop@gmail.com", "url": "github.com/floatdrop" }, "engines": { "node": ">=0.10.0" }, "scripts": { "test": "mocha" }, "files": [ "index.js" ], "keywords": [ "" ], "dependencies": {}, "devDependencies": { "mocha": "*" } } require-from-string-2.0.1/readme.md000066400000000000000000000016241315741566100172260ustar00rootroot00000000000000# require-from-string [![Build Status](https://travis-ci.org/floatdrop/require-from-string.svg?branch=master)](https://travis-ci.org/floatdrop/require-from-string) Load module from string in Node. ## Install ``` $ npm install --save require-from-string ``` ## Usage ```js var requireFromString = require('require-from-string'); requireFromString('module.exports = 1'); //=> 1 ``` ## API ### requireFromString(code, [filename], [options]) #### code *Required* Type: `string` Module code. #### filename Type: `string` Default: `''` Optional filename. #### options Type: `object` ##### appendPaths Type: `Array` List of `paths`, that will be appended to module `paths`. Useful, when you want to be able require modules from these paths. ##### prependPaths Type: `Array` Same as `appendPath`, but paths will be prepended. ## License MIT © [Vsevolod Strukchinsky](http://github.com/floatdrop) require-from-string-2.0.1/test/000077500000000000000000000000001315741566100164235ustar00rootroot00000000000000require-from-string-2.0.1/test/fixture/000077500000000000000000000000001315741566100201115ustar00rootroot00000000000000require-from-string-2.0.1/test/fixture/module.js000066400000000000000000000000511315741566100217300ustar00rootroot00000000000000module.exports = require('./submodule'); require-from-string-2.0.1/test/fixture/submodule.js000066400000000000000000000000311315741566100224400ustar00rootroot00000000000000module.exports = module; require-from-string-2.0.1/test/index.js000066400000000000000000000036461315741566100201010ustar00rootroot00000000000000/* global it */ 'use strict'; var assert = require('assert'); var fs = require('fs'); var path = require('path'); var requireFromString = require('../'); it('should accept only string as code', function () { assert.throws(function () { requireFromString(); }, /code must be a string, not undefined/); }); it('should require from string', function () { assert.equal(requireFromString('module.exports = 1;'), 1); }); it('should accept filename', function () { assert.throws(function () { requireFromString('module.exports = ', 'bug.js'); }, /bug\.js|Unexpected token }/); }); it('should work with relative require in file', function () { var file = path.join(__dirname, '/fixture/module.js'); var code = fs.readFileSync(file, 'utf8'); var result = requireFromString(code, file); assert.ok(result); assert.ok(module === result.parent.parent); }); it('should have appended and preppended paths', function () { var file = path.join(__dirname, '/fixture/submodule.js'); var code = fs.readFileSync(file, 'utf8'); var result = requireFromString(code, file, { appendPaths: ['append'], prependPaths: ['prepend'] }); assert.ok(result); assert.equal(result.paths.indexOf('append'), result.paths.length - 1); assert.equal(result.paths.indexOf('prepend'), 0); }); it('should have meaningful error message', function () { try { requireFromString('throw new Error("Boom!");'); } catch (err) { assert.ok(/\(:1:69\)/.test(err.stack), 'should contain (:1:69) in stack'); } try { requireFromString('throw new Error("Boom!");', ''); } catch (err) { assert.ok(/\(:1:69\)/.test(err.stack), 'should contain (:1:69) in stack'); } }); it('should cleanup parent.children', function() { var file = path.join(__dirname, '/fixture/submodule.js'); var code = fs.readFileSync(file, 'utf8'); var result = requireFromString(code, file); assert.ok(module.children.indexOf(result) === -1); });