|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const inputFile = path.join(__dirname, '..', 'ado-dist', 'index.js'); |
| 5 | +const outputFile = path.join(__dirname, '..', 'component-detection-github-submission-task', 'index.js'); |
| 6 | + |
| 7 | +// Read the ES module output from ncc |
| 8 | +let content = fs.readFileSync(inputFile, 'utf8'); |
| 9 | + |
| 10 | +// Convert ES module imports/exports to CommonJS |
| 11 | +// Replace import statements at the beginning |
| 12 | +content = content.replace( |
| 13 | + /^import\s+(['"].*?['"])\s*;/gm, |
| 14 | + 'require($1);' |
| 15 | +); |
| 16 | + |
| 17 | +// Replace import with destructuring |
| 18 | +content = content.replace( |
| 19 | + /^import\s*{\s*([^}]+)\s*}\s*from\s*(['"].*?['"])\s*;/gm, |
| 20 | + 'const { $1 } = require($2);' |
| 21 | +); |
| 22 | + |
| 23 | +// Replace default imports |
| 24 | +content = content.replace( |
| 25 | + /^import\s+(\w+)\s+from\s*(['"].*?['"])\s*;/gm, |
| 26 | + 'const $1 = require($2);' |
| 27 | +); |
| 28 | + |
| 29 | +// Replace createRequire import (this is specific to the ncc output) |
| 30 | +content = content.replace( |
| 31 | + /import\s*{\s*createRequire\s+as\s+__WEBPACK_EXTERNAL_createRequire\s*}\s*from\s*['"]module['"];/g, |
| 32 | + 'const { createRequire: __WEBPACK_EXTERNAL_createRequire } = require("module");' |
| 33 | +); |
| 34 | + |
| 35 | +// Replace any remaining import statements |
| 36 | +content = content.replace( |
| 37 | + /^import\s+(.*?)\s*;/gm, |
| 38 | + 'require($1);' |
| 39 | +); |
| 40 | + |
| 41 | +// Write the converted content |
| 42 | +fs.writeFileSync(outputFile, content, 'utf8'); |
| 43 | + |
| 44 | +console.log(`Converted ES module to CommonJS: ${outputFile}`); |
0 commit comments