pax_global_header00006660000000000000000000000064132626334710014521gustar00rootroot0000000000000052 comment=d1575a49065eb7a49b86b4de963f04f1a14dfd60 require-from-string-2.0.2/000077500000000000000000000000001326263347100154435ustar00rootroot00000000000000require-from-string-2.0.2/.editorconfig000066400000000000000000000003471326263347100201240ustar00rootroot00000000000000root = 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.2/.gitattributes000066400000000000000000000000141326263347100203310ustar00rootroot00000000000000* text=auto require-from-string-2.0.2/.gitignore000066400000000000000000000000151326263347100174270ustar00rootroot00000000000000node_modules require-from-string-2.0.2/.jshintrc000066400000000000000000000002521326263347100172670ustar00rootroot00000000000000{ "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.2/.travis.yml000066400000000000000000000001071326263347100175520ustar00rootroot00000000000000sudo: false language: node_js node_js: - '7' - '6' - '5' - '4' require-from-string-2.0.2/index.js000066400000000000000000000015421326263347100171120ustar00rootroot00000000000000'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 && parent.children && parent.children.splice(parent.children.indexOf(m), 1); return exports; }; require-from-string-2.0.2/license000066400000000000000000000021501326263347100170060ustar00rootroot00000000000000The 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.2/package.json000066400000000000000000000007771326263347100177440ustar00rootroot00000000000000{ "name": "require-from-string", "version": "2.0.2", "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.2/readme.md000066400000000000000000000016251326263347100172260ustar00rootroot00000000000000# 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 `appendPaths`, but paths will be prepended. ## License MIT © [Vsevolod Strukchinsky](http://github.com/floatdrop) require-from-string-2.0.2/test/000077500000000000000000000000001326263347100164225ustar00rootroot00000000000000require-from-string-2.0.2/test/fixture/000077500000000000000000000000001326263347100201105ustar00rootroot00000000000000require-from-string-2.0.2/test/fixture/module.js000066400000000000000000000000511326263347100217270ustar00rootroot00000000000000module.exports = require('./submodule'); require-from-string-2.0.2/test/fixture/submodule.js000066400000000000000000000000311326263347100224370ustar00rootroot00000000000000module.exports = module; require-from-string-2.0.2/test/index.js000066400000000000000000000036461326263347100201000ustar00rootroot00000000000000/* 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); });