用工小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.1 KiB

6 months ago
6 months ago
6 months ago
6 months ago
  1. # import-fresh
  2. > Import a module while bypassing the [cache](https://nodejs.org/api/modules.html#modules_caching)
  3. Useful for testing purposes when you need to freshly import a module.
  4. ## ESM
  5. For ESM, you can use this snippet:
  6. ```js
  7. const importFresh = moduleName => import(`${moduleName}?${Date.now()}`);
  8. const {default: foo} = await importFresh('foo');
  9. ```
  10. **This snippet causes a memory leak, so only use it for short-lived tests.**
  11. ## Install
  12. ```sh
  13. npm install import-fresh
  14. ```
  15. ## Usage
  16. ```js
  17. // foo.js
  18. let i = 0;
  19. module.exports = () => ++i;
  20. ```
  21. ```js
  22. const importFresh = require('import-fresh');
  23. require('./foo')();
  24. //=> 1
  25. require('./foo')();
  26. //=> 2
  27. importFresh('./foo')();
  28. //=> 1
  29. importFresh('./foo')();
  30. //=> 1
  31. ```
  32. ## Related
  33. - [clear-module](https://github.com/sindresorhus/clear-module) - Clear a module from the import cache
  34. - [import-from](https://github.com/sindresorhus/import-from) - Import a module from a given path
  35. - [import-cwd](https://github.com/sindresorhus/import-cwd) - Import a module from the current working directory
  36. - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import modules lazily