pax_global_header00006660000000000000000000000064137676512630014532gustar00rootroot0000000000000052 comment=f1dddc06da4f1cdfb44a3aaba1f2c353c00dea02 import-fresh-3.3.0/000077500000000000000000000000001376765126300141545ustar00rootroot00000000000000import-fresh-3.3.0/.editorconfig000066400000000000000000000002571376765126300166350ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 import-fresh-3.3.0/.gitattributes000066400000000000000000000000231376765126300170420ustar00rootroot00000000000000* text=auto eol=lf import-fresh-3.3.0/.github/000077500000000000000000000000001376765126300155145ustar00rootroot00000000000000import-fresh-3.3.0/.github/funding.yml000066400000000000000000000000601376765126300176650ustar00rootroot00000000000000github: sindresorhus tidelift: npm/import-fresh import-fresh-3.3.0/.github/security.md000066400000000000000000000002631376765126300177060ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. import-fresh-3.3.0/.github/workflows/000077500000000000000000000000001376765126300175515ustar00rootroot00000000000000import-fresh-3.3.0/.github/workflows/main.yml000066400000000000000000000007201376765126300212170ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 14 - 12 - 10 - 8 - 6 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test import-fresh-3.3.0/.gitignore000066400000000000000000000000461376765126300161440ustar00rootroot00000000000000node_modules yarn.lock *.heapsnapshot import-fresh-3.3.0/.npmrc000066400000000000000000000000231376765126300152670ustar00rootroot00000000000000package-lock=false import-fresh-3.3.0/fixture-importer.js000066400000000000000000000002051376765126300200340ustar00rootroot00000000000000const importFresh = require('.'); module.exports = what => { return importFresh(what); }; module.exports.__filename = __filename; import-fresh-3.3.0/fixture.js000066400000000000000000000000661376765126300162020ustar00rootroot00000000000000'use strict'; let i = 0; module.exports = () => ++i; import-fresh-3.3.0/heapdump.js000066400000000000000000000006151376765126300163170ustar00rootroot00000000000000'use strict'; const heapdump = require('heapdump'); // eslint-disable-line import/no-unresolved const importFresh = require('.'); for (let i = 0; i < 100000; i++) { require('./fixture.js')(); } heapdump.writeSnapshot(`require-${Date.now()}.heapsnapshot`); for (let i = 0; i < 100000; i++) { importFresh('./fixture.js')(); } heapdump.writeSnapshot(`import-fresh-${Date.now()}.heapsnapshot`); import-fresh-3.3.0/index.d.ts000066400000000000000000000006601376765126300160570ustar00rootroot00000000000000/** Import a module while bypassing the cache. @example ``` // foo.js let i = 0; module.exports = () => ++i; // index.js import importFresh = require('import-fresh'); require('./foo')(); //=> 1 require('./foo')(); //=> 2 importFresh('./foo')(); //=> 1 importFresh('./foo')(); //=> 1 const foo = importFresh('./foo'); ``` */ declare function importFresh(moduleId: string): T; export = importFresh; import-fresh-3.3.0/index.js000066400000000000000000000020641376765126300156230ustar00rootroot00000000000000'use strict'; const path = require('path'); const resolveFrom = require('resolve-from'); const parentModule = require('parent-module'); module.exports = moduleId => { if (typeof moduleId !== 'string') { throw new TypeError('Expected a string'); } const parentPath = parentModule(__filename); const cwd = parentPath ? path.dirname(parentPath) : __dirname; const filePath = resolveFrom(cwd, moduleId); const oldModule = require.cache[filePath]; // Delete itself from module parent if (oldModule && oldModule.parent) { let i = oldModule.parent.children.length; while (i--) { if (oldModule.parent.children[i].id === filePath) { oldModule.parent.children.splice(i, 1); } } } delete require.cache[filePath]; // Delete module from cache const parent = require.cache[parentPath]; // If `filePath` and `parentPath` are the same, cache will already be deleted so we won't get a memory leak in next step return parent === undefined ? require(filePath) : parent.require(filePath); // In case cache doesn't have parent, fall back to normal require }; import-fresh-3.3.0/index.test-d.ts000066400000000000000000000006561376765126300170410ustar00rootroot00000000000000import {expectType, expectError} from 'tsd'; import importFresh = require('.'); import path = require('path'); expectType(importFresh('hello')); expectError(importFresh()); expectType<(moduleId: string) => {}>(importFresh<(moduleId: string) => {}>('hello')); expectType(importFresh('path')); const foo = importFresh('path'); expectType(foo); import-fresh-3.3.0/license000066400000000000000000000021351376765126300155220ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) 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. import-fresh-3.3.0/package.json000066400000000000000000000014341376765126300164440ustar00rootroot00000000000000{ "name": "import-fresh", "version": "3.3.0", "description": "Import a module while bypassing the cache", "license": "MIT", "repository": "sindresorhus/import-fresh", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "engines": { "node": ">=6" }, "scripts": { "test": "xo && ava && tsd", "heapdump": "node heapdump.js" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "require", "cache", "uncache", "uncached", "module", "fresh", "bypass" ], "dependencies": { "parent-module": "^1.0.0", "resolve-from": "^4.0.0" }, "devDependencies": { "ava": "^1.0.1", "heapdump": "^0.3.12", "tsd": "^0.7.3", "xo": "^0.23.0" } } import-fresh-3.3.0/readme.md000066400000000000000000000026451376765126300157420ustar00rootroot00000000000000# import-fresh > Import a module while bypassing the [cache](https://nodejs.org/api/modules.html#modules_caching) Useful for testing purposes when you need to freshly import a module. ## Install ``` $ npm install import-fresh ``` ## Usage ```js // foo.js let i = 0; module.exports = () => ++i; ``` ```js const importFresh = require('import-fresh'); require('./foo')(); //=> 1 require('./foo')(); //=> 2 importFresh('./foo')(); //=> 1 importFresh('./foo')(); //=> 1 ``` ## import-fresh for enterprise Available as part of the Tidelift Subscription. The maintainers of import-fresh and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-import-fresh?utm_source=npm-import-fresh&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) ## Related - [clear-module](https://github.com/sindresorhus/clear-module) - Clear a module from the import cache - [import-from](https://github.com/sindresorhus/import-from) - Import a module from a given path - [import-cwd](https://github.com/sindresorhus/import-cwd) - Import a module from the current working directory - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import modules lazily import-fresh-3.3.0/test.js000066400000000000000000000025701376765126300154750ustar00rootroot00000000000000import path from 'path'; import test from 'ava'; import importFresh from '.'; test('main', t => { const id = './fixture'; t.is(require(id)(), 1); t.is(require(id)(), 2); t.is(require(id)(), 3); t.is(importFresh(id)(), 1); t.is(importFresh(id)(), 1); t.is(importFresh(id)(), 1); t.is(require(id)(), 2); }); test('proper parent value', t => { const id = './fixture'; importFresh(id); const childModule = require.cache[path.resolve(__dirname, `${id}.js`)]; t.is(childModule.parent, module); }); test('self import', t => { const id = './fixture-importer'; t.notThrows(() => { importFresh(id)(id); }); }); test('import when parent removed from cache', t => { const id = './fixture-importer'; const importer = importFresh(id); t.true(require.cache[importer.__filename] !== undefined); delete require.cache[importer.__filename]; t.notThrows(() => { importer(id); }); }); test('should not fail when there is no parent module', t => { const targetPath = require.resolve('parent-module'); const orignalModule = require.cache[targetPath]; delete require.cache[targetPath]; delete require.cache[require.resolve('.')]; require.cache[targetPath] = { loaded: true, id: targetPath, exports: () => undefined }; const id = './fixture-importer'; const importer = importFresh(id); t.notThrows(() => { importer(id); }); require.cache[targetPath] = orignalModule; });