tonysamperi

Untitled

Jul 10th, 2025
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.76 KB | Source Code | 0 0
  1. import path from 'node:path';
  2. import process from 'node:process';
  3. import { fileURLToPath, pathToFileURL } from 'node:url';
  4. import { createRequire } from 'node:module';
  5.  
  6. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  7. const require = createRequire(import.meta.url);
  8.  
  9. const cjsModulePath = path.resolve(__dirname, '../dist/cjs/index.cjs');
  10. const esmModulePath = path.resolve(__dirname, '../dist/esm/index.js');
  11.  
  12. console.log('Checking for export consistency between CJS and ESM modules...');
  13.  
  14. try {
  15.     const cjsModule = require(cjsModulePath);
  16.     const esmModule = await import(pathToFileURL(esmModulePath).href);
  17.     const cjsKeys = new Set(Object.getOwnPropertyNames(cjsModule));
  18.     const esmKeys = new Set(Object.keys(esmModule));
  19.     const keysToIgnore = ['default', '__esModule'];
  20.     for (const key of keysToIgnore) {
  21.         cjsKeys.delete(key);
  22.         esmKeys.delete(key);
  23.     }
  24.     const missingInCjs = [...esmKeys].filter(key => !cjsKeys.has(key));
  25.     const missingInEsm = [...cjsKeys].filter(key => !esmKeys.has(key));
  26.  
  27.     if (missingInCjs.length > 0 || missingInEsm.length > 0) {
  28.         console.error('❌ Error: CJS and ESM exports do not match!');
  29.  
  30.         if (missingInCjs.length > 0) {
  31.             console.error(`\nExports found in ESM but MISSING in CJS:\n - ${missingInCjs.join('\n - ')}`);
  32.         }
  33.  
  34.         if (missingInEsm.length > 0) {
  35.             console.error(`\nExports found in CJS but MISSING in ESM:\n - ${missingInEsm.join('\n - ')}`);
  36.         }
  37.  
  38.         process.exit(1);
  39.     }
  40.  
  41.     console.log('✅ Success: All exports are consistent across CJS and ESM modules.');
  42.     process.exit(0);
  43.  
  44. } catch (error) {
  45.     console.error('❌ An error occurred while checking exports:', error);
  46.     process.exit(1);
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment