From dabff1c087254f8e96882a507c497e3cc80fd089 Mon Sep 17 00:00:00 2001 From: Bob Ippolito Date: Wed, 27 Mar 2024 18:22:52 -0700 Subject: [PATCH] Node fork module with async await --- scripts/build.js | 55 +++++++++++++++++++++++++++------------- scripts/updateVersion.js | 7 +++++ 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/scripts/build.js b/scripts/build.js index f3750e0841fc..f701658b58b1 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -612,17 +612,29 @@ async function moveTSDeclarationFilesIntoDist(packageName, outputPath) { await fs.copy(`./.ts-temp/${packageName}/src`, outputPath); } -function buildForkModule(outputPath, outputFileName, format, exports) { +function forkModuleContent( + {devFileName, exports, outputFileName, prodFileName}, + target, +) { const lines = [getComment()]; - const extension = getExtension(format); - const devFileName = `./${outputFileName}.dev${extension}`; - const prodFileName = `./${outputFileName}.prod${extension}`; - if (format === 'esm') { + if (target === 'cjs') { lines.push( - `import * as modDev from '${devFileName}';`, - `import * as modProd from '${prodFileName}';`, - `const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;`, + `'use strict'`, + `const ${outputFileName} = process.env.NODE_ENV === 'development' ? require('${devFileName}') : require('${prodFileName}');`, + `module.exports = ${outputFileName};`, ); + } else { + if (target === 'esm') { + lines.push( + `import * as modDev from '${devFileName}';`, + `import * as modProd from '${prodFileName}';`, + `const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;`, + ); + } else if (target === 'node') { + lines.push( + `const mod = await (process.env.NODE_ENV === 'development' ? import('${devFileName}') : import('${prodFileName}'));`, + ); + } for (const name of exports) { lines.push( name === 'default' @@ -630,18 +642,27 @@ function buildForkModule(outputPath, outputFileName, format, exports) { : `export const ${name} = mod.${name};`, ); } - } else { - lines.push( - `'use strict'`, - `const ${outputFileName} = process.env.NODE_ENV === 'development' ? require('${devFileName}') : require('${prodFileName}');`, - `module.exports = ${outputFileName};`, - ); } - const fileContent = lines.join('\n'); + return lines.join('\n'); +} + +function buildForkModules(outputPath, outputFileName, format, exports) { + const extension = getExtension(format); + const devFileName = `./${outputFileName}.dev${extension}`; + const prodFileName = `./${outputFileName}.prod${extension}`; + const opts = {devFileName, exports, outputFileName, prodFileName}; fs.outputFileSync( path.resolve(path.join(`${outputPath}${outputFileName}${extension}`)), - fileContent, + forkModuleContent(opts, format), ); + if (format === 'esm') { + fs.outputFileSync( + path.resolve( + path.join(`${outputPath}${outputFileName}.node${extension}`), + ), + forkModuleContent(opts, 'node'), + ); + } } async function buildAll() { @@ -693,7 +714,7 @@ async function buildAll() { false, format, ); - buildForkModule(outputPath, outputFileName, format, exports); + buildForkModules(outputPath, outputFileName, format, exports); } } } diff --git a/scripts/updateVersion.js b/scripts/updateVersion.js index ff7b6c02d1e1..6926d05980dd 100644 --- a/scripts/updateVersion.js +++ b/scripts/updateVersion.js @@ -58,12 +58,18 @@ function withEsmExtension(fileName) { return fileName.replace(/\.js$/, '.mjs'); } +function withNodeEsmExtension(fileName) { + return fileName.replace(/\.js$/, '.node.mjs'); +} + function exportEntry(file) { return { import: { types: `./${file.replace(/\.js$/, '.d.ts')}`, // webpack requires default to be the last entry per #5731 // eslint-disable-next-line sort-keys-fix/sort-keys-fix + node: `./${withNodeEsmExtension(file)}`, + // eslint-disable-next-line sort-keys-fix/sort-keys-fix default: `./${withEsmExtension(file)}`, }, require: `./${file}`, @@ -76,6 +82,7 @@ function updateModule(packageJSON, pkg) { } if (packageJSON.main) { packageJSON.module = withEsmExtension(packageJSON.main); + packageJSON.exports = {'.': exportEntry(packageJSON.main)}; } else if (fs.existsSync(`./packages/${pkg}/dist`)) { const exports = {}; for (const file of fs.readdirSync(`./packages/${pkg}/dist`)) {