# hot-module-cjs **Repository Path**: xxXyh1908/hot-module-cjs ## Basic Information - **Project Name**: hot-module-cjs - **Description**: Node.js CommonJS-module hot replace - **Primary Language**: TypeScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-10-20 - **Last Updated**: 2022-05-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: require, hmr, Module ## README

Welcome to hot-module-cjs 👋

Version License: MIT

> CommonJS hot module supports. ### 🏠 [Homepage](https://gitee.com/xxXyh1908/hot-module-cjs/#readme) ## install ```bash npm i -D @xyh19/hot-module-cjs # or yarn add -D @xyh19/hot-module-cjs ``` ## Usage ### [Note] [1] This module does not support test frameworks such as `jest` [2] This module should not be considered an external module ### use hot-module ```js const { hotModule } = require('@xyh19/hot-module-cjs'); ({ module } = hotModule(module)); let counter = 0; const counterInterval = setInterval(() => { console.log('counter: ' + i) }, 1000); // Execute when the module is about to be unloaded module.hot.on('beforeUnload', (module) => { // ...do somethings module.hot.preventReload(); }); // Execute when the module has been unloaded module.hot.on('unload', (module) => { // clear counterInterval clearInterval(counterInterval); // ...do somethings }); ``` ### api ```ts declare const hotModule: (_module: NodeModule) => { module: HotModule; require: NodeRequire; }; declare const toRawModuleExports: (exports: T) => T; declare const getModuleExportsPath: (exports: any) => string | undefined; declare const removeModule: (modPath: string) => void; declare const removeAllModules: () => void; declare const getLoadedModules: () => IterableIterator; /** * @deprecated * @see addExcludeCondition */ declare const registerHotModuleFilter: (filter: (modulePath: string) => boolean) => void; /** * @deprecated * @see removeExcludeCondition */ declare const unregisterHotModuleFilter: (filter: (modulePath: string) => boolean) => void; declare const addExcludeCondition: (condition: (modulePath: string) => boolean) => void; declare const removeExcludeCondition: (condition: (modulePath: string) => boolean) => void; ``` ### import hot-module a.js ```js // Enabling conditions: `typeof module.exports === 'object'` module.exports = { var1: '123', var2: '456' }; ``` main.js ```js const { hotModule, toRawModuleExports } = require('@xyh19/hot-module-cjs'); ({ module, require } = hotModule(module)); // Prevent main module updates module.hot.on('beforeUnload', (module) => { // ...do somethings module.hot.preventReload(); }); const mod_a = require('./a'); // Proxy Object // error console.log('line 9', mod_a); // Proxy {} // success console.log('line 11', toRawModuleExports(mod_a) ); // {var1: '123', var2: '456'} const fs = require('fs'); fs.writeFileSync('./a.js', ` module.exports = { var1: 'abc', var2: 'def' }; `); module.hot.watch(mod_a, (new_mod_a) => { console.log('line 23', toRawModuleExports(mod_a) ); // {var1: 'abc', var2: 'def'} console.log('line 24', new_mod_a ); // {var1: 'abc', var2: 'def'} }); ``` ## rollup-plugin ### [Note] [1] This plugin is only available in development mode [2] This plugin be placed in plugins before other plugins. [3] This plugin should be used with @rollup/plugin-commonjs. ```js //rollup.config.ts import { defineConfig } from 'rollup' import hot from '@xyh19/hot-module-cjs/dist/rollup-plugin' export default defineConfig({ // ..., plugins: [ hot({ srcRoot: 'src', }), //... others plugins ] }) ``` ## 🤝 Contributing Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://gitee.com/xxXyh1908/hot-module-cjs/issues). ## Show your support Give a ⭐️ if this project helped you! *** _This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_