@xyh19/hot-module-cjs
TypeScript icon, indicating that this package has built-in type declarations

3.3.10 • Public • Published

Welcome to hot-module-cjs 👋

Version License: MIT

CommonJS hot module supports.

🏠 Homepage

install

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

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

declare const hotModule: (_module: NodeModule) => {
    module: HotModule;
    require: NodeRequire;
};
declare const toRawModuleExports: <T>(exports: T) => T;
declare const getModuleExportsPath: (exports: any) => string | undefined;
declare const removeModule: (modPath: string) => void;
declare const removeAllModules: () => void;
declare const getLoadedModules: () => IterableIterator<ModuleInfo>;
/**
 * @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

// Enabling conditions: `typeof module.exports === 'object'`
module.exports = {
  var1: '123',
  var2: '456'
};

main.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.

//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.

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator

Package Sidebar

Install

npm i @xyh19/hot-module-cjs

Weekly Downloads

7

Version

3.3.10

License

MIT

Unpacked Size

177 kB

Total Files

14

Last publish

Collaborators

  • xyh19