import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'url'; import https from 'node:https'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); async function downloadFile(url, destPath) { return new Promise((resolve, reject) => { https.get(url, (response) => { if (response.statusCode === 200) { const fileStream = fs.createWriteStream(destPath); response.pipe(fileStream); fileStream.on('finish', () => { fileStream.close(); resolve(); }); } else { reject(new Error(`Failed to download ${url}: ${response.statusCode}`)); } }).on('error', reject); }); } const semiD2cFigmaAdapterPath = path.join(__dirname, 'compiled/semi-d2c-figma-adapter/index.js'); const filesToCopy = [ { from: path.join(__dirname, 'node_modules/@ies/semi-d2c-figma-adapter/dist/umd/index.js'), to: semiD2cFigmaAdapterPath, }, { from: path.join(__dirname, 'node_modules/react/umd/react.development.js'), to: path.join(__dirname, 'compiled/react/umd/react.development.js'), }, { from: path.join(__dirname, 'node_modules/react/umd/react.production.min.js'), to: path.join(__dirname, 'compiled/react/umd/react.production.min.js'), }, { from: path.join(__dirname, 'node_modules/react-dom/umd/react-dom.development.js'), to: path.join(__dirname, 'compiled/react-dom/umd/react-dom.development.js'), }, { from: path.join(__dirname, 'node_modules/react-dom/umd/react-dom.production.min.js'), to: path.join(__dirname, 'compiled/react-dom/umd/react-dom.production.min.js'), }, ] for (const file of filesToCopy) { fs.mkdirSync(path.dirname(file.to), { recursive: true }); fs.copyFileSync(file.from, file.to); } // Read React version from package.json const reactPackageJson = JSON.parse( fs.readFileSync(path.join(__dirname, 'node_modules/react/package.json'), 'utf8') ); const reactVersion = reactPackageJson.version; // Read ReactDOM version from package.json const reactDomPackageJson = JSON.parse( fs.readFileSync(path.join(__dirname, 'node_modules/react-dom/package.json'), 'utf8') ); const reactDomVersion = reactDomPackageJson.version; console.log(`Downloading React ${reactVersion} ESM modules from esm.sh...`); // Download React ESM module const reactEsmPath = path.join(__dirname, 'compiled/react/esm/react.mjs'); const reactEsmMapPath = path.join(__dirname, 'compiled/react/esm/react.mjs.map'); const reactDevEsmPath = path.join(__dirname, 'compiled/react/esm/react.development.mjs'); const reactDevEsmMapPath = path.join(__dirname, 'compiled/react/esm/react.development.mjs.map'); fs.mkdirSync(path.dirname(reactEsmPath), { recursive: true }); await downloadFile( `https://esm.sh/react@${reactVersion}/es2022/react.mjs`, reactEsmPath ); await downloadFile( `https://esm.sh/react@${reactVersion}/es2022/react.mjs.map`, reactEsmMapPath ); await downloadFile( `https://esm.sh/react@${reactVersion}/es2022/cjs/react.development.development.mjs`, reactDevEsmPath ); // skip // await downloadFile( // `https://esm.sh/react@${reactVersion}/es2022/cjs/react.development.development.mjs.map`, // reactDevEsmMapPath // ); console.log(`Downloading ReactDOM ${reactDomVersion} ESM modules from esm.sh...`); // Download ReactDOM ESM module const reactDomEsmPath = path.join(__dirname, 'compiled/react-dom/esm/react-dom.mjs'); const reactDomEsmMapPath = path.join(__dirname, 'compiled/react-dom/esm/react-dom.mjs.map'); const reactDomDevEsmPath = path.join(__dirname, 'compiled/react-dom/esm/react-dom.development.mjs'); const reactDomDevEsmMapPath = path.join(__dirname, 'compiled/react-dom/esm/react-dom.development.development.mjs.map'); fs.mkdirSync(path.dirname(reactDomEsmPath), { recursive: true }); await downloadFile( `https://esm.sh/react-dom@${reactDomVersion}/es2022/react-dom.mjs`, reactDomEsmPath ); await downloadFile( `https://esm.sh/react-dom@${reactDomVersion}/es2022/react-dom.mjs.map`, reactDomEsmMapPath ); await downloadFile( `https://esm.sh/react-dom@${reactDomVersion}/es2022/cjs/react-dom.development.development.mjs`, reactDomDevEsmPath ); await downloadFile( `https://esm.sh/react-dom@${reactDomVersion}/es2022/cjs/react-dom.development.development.mjs.map`, reactDomDevEsmMapPath ); // Fix ReactDOM imports to use bare specifiers for import map [reactDomEsmPath, reactDomDevEsmPath].forEach(reactDomEsmPath => { let reactDomContent = fs.readFileSync(reactDomEsmPath, 'utf8'); reactDomContent = reactDomContent.replace( /import\s*\*\s*as\s+([\w$]+)\s+from\s*"\/react@[^"]+"/g, 'import * as $1 from "react"' ); reactDomContent = reactDomContent.replace( /import\s*\*\s*as\s+([\w$]+)\s+from\s*"\/scheduler@[^"]+"/g, 'import * as $1 from "./scheduler.mjs"' ); fs.writeFileSync(reactDomEsmPath, reactDomContent); }) // Read scheduler version from package.json const schedulerPackageJson = JSON.parse( fs.readFileSync(path.join(__dirname, 'node_modules/scheduler/package.json'), 'utf8') ); const schedulerVersion = schedulerPackageJson.version; console.log(`Downloading Scheduler ${schedulerVersion} ESM modules from esm.sh...`); // Download Scheduler ESM module to react-dom/esm folder const schedulerEsmPath = path.join(__dirname, 'compiled/react-dom/esm/scheduler.mjs'); await downloadFile( `https://esm.sh/scheduler@${schedulerVersion}/es2022/scheduler.mjs`, schedulerEsmPath ); console.log('ESM modules downloaded successfully!'); // replace https://semiapi.bytedance.net (we don't use and cannot appear in ttp) string in file content to empty string const semiD2cFigmaAdapterContent = fs.readFileSync(semiD2cFigmaAdapterPath, 'utf8'); const semiD2cFigmaAdapterContentWithoutSemiApi = semiD2cFigmaAdapterContent.replace(/https:\/\/semiapi.bytedance.net/g, ''); fs.writeFileSync(semiD2cFigmaAdapterPath, semiD2cFigmaAdapterContentWithoutSemiApi);